java 生成excel 支持 xls、xlsx

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/fenfenguai/article/details/79642065

java小工具项目

最近由于工作需要写了个小工具项目,用来生产excel,其中参考了poi官网的demo,以及网络上的一些资料。:

  • 引入jar
<poi.version>3.17</poi.version>
 <!--poi相关-->
    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi</artifactId>
      <version>${poi.version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi-ooxml</artifactId>
      <version>${poi.version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi-ooxml-schemas</artifactId>
      <version>${poi.version}</version>
    </dependency>
    <!--poi相关-->
  • 核心代码
/**
     * 生成 xlsx格式的excel
     *
     * @param fileName   文件名称  如 xxx.xls
     * @param filePath   文件路径  如 D:\export\
     * @param fileSuffix 文件后缀
     * @param list       数据
     * @throws IOException
     */
    public static void exportExcelXlsx(String fileName, String filePath, String fileSuffix, List<Map<String, Object>> list, String showType) throws IOException {

        long start = System.currentTimeMillis();
        // 声明一个工作薄
        XSSFWorkbook workBook = new XSSFWorkbook();

        int s = 0;
        for (Map<String, Object> map : list) {
            String sheetName = map.get("sheetName").toString();
            String[] sheetHead = (String[]) map.get("sheetHead");
            List<String[]> dataList = (List<String[]>) map.get("sheetData");
            String serialNumber = map.get("serialNumber").toString();
            String serialNumberDesc = map.get("serialNumberDesc").toString();
            String calcTimeIndex = map.get("calcTimeIndex").toString();//取第几列的值来计算时间片段 -1 表示不计算  0 表示计算第一个 以此类推

            // 生成一个表格
            XSSFSheet sheet = workBook.createSheet();

            workBook.setSheetName(s, sheetName);
            // 创建表格标题行 第一行
            XSSFRow titleRow = sheet.createRow(0);
            //需要加入序号列
            if (StringUtils.equals("1", serialNumber)) {
                titleRow.createCell(0).setCellValue(serialNumberDesc);
                for (int i = 1; i < sheetHead.length+1; i++) {

                    titleRow.createCell(i).setCellValue(sheetHead[i - 1]);
                }
                //插入需导出的数据
                for (int i = 0; i < dataList.size(); i++) {
                    XSSFRow row = sheet.createRow(i + 1);
                    row.createCell(0).setCellValue((i + 1));
                    for (int j = 1; j < sheetHead.length + 1; j++) {
                        //计算时间片
                        if (!StringUtils.equals("-1", calcTimeIndex)) {
                            if (Integer.valueOf(calcTimeIndex) == (j - 1)) {
                                row.createCell(j).setCellValue(getStepIntexTime(Integer.valueOf(dataList.get(i)[j - 1])));
                            } else {
                                row.createCell(j).setCellValue(dataList.get(i)[j - 1]);
                            }
                        } else {
                            row.createCell(j).setCellValue(dataList.get(i)[j - 1]);
                        }
                    }
                }
            } else {
                //不需要加入序号列

                for (int i = 0; i < sheetHead.length; i++) {
                    titleRow.createCell(i).setCellValue(sheetHead[i]);
                }
                //插入需导出的数据
                for (int i = 0; i < dataList.size(); i++) {
                    XSSFRow row = sheet.createRow(i + 1);
                    for (int j = 0; j < sheetHead.length; j++) {

                        //计算时间片
                        if (!StringUtils.equals("-1", calcTimeIndex)) {
                            if (Integer.valueOf(calcTimeIndex) == j) {
                                row.createCell(j).setCellValue(getStepIntexTime(Integer.valueOf(dataList.get(i)[j])));
                            } else {
                                row.createCell(j).setCellValue(dataList.get(i)[j]);
                            }
                        } else {
                            row.createCell(j).setCellValue(dataList.get(i)[j]);
                        }

                    }
                }
            }

            s++;
        }

        //校验文件路径是否存在
        File checkPath = new File(filePath);
        if (!checkPath.exists()) {
            checkPath.mkdirs();
        }
        if (!filePath.endsWith(File.separator)) {

            filePath = filePath + File.separator;
        }
        File file = new File(filePath + fileName + "." + fileSuffix);
        //文件输出流
        FileOutputStream outStream = new FileOutputStream(file);
        workBook.write(outStream);
        outStream.flush();
        outStream.close();
        LogShowUtils.show(showType, log, "生成文件耗时:" + (System.currentTimeMillis() - start));
        LogShowUtils.show(showType, log, "导出文件成功!文件导出路径:--" + filePath + fileName);
    }
 /**
     * 生成 xls格式的excel
     *
     * @param fileName   文件名称  如 xxx.xls
     * @param filePath   文件路径  如 D:\export\
     * @param fileSuffix 文件后缀
     * @param list       数据
     * @throws IOException
     */
    public static void exportExcelXls(String fileName, String filePath, String fileSuffix, List<Map<String, Object>> list, String showType) throws IOException {

        long start = System.currentTimeMillis();
        try (HSSFWorkbook wb = new HSSFWorkbook()) {

            int s = 0;
            for (Map<String, Object> map : list) {
                String sheetName = map.get("sheetName").toString();
                String[] sheetHead = (String[]) map.get("sheetHead");
                String serialNumber = map.get("serialNumber").toString();
                List<String[]> dataList = (List<String[]>) map.get("sheetData");

                String serialNumberDesc = map.get("serialNumberDesc").toString();
                String calcTimeIndex = map.get("calcTimeIndex").toString();//取第几列的值来计算时间片段 -1 表示不计算  0 表示计算第一个 以此类推
                HSSFSheet sheet = wb.createSheet(sheetName);

                // Create a row and put some cells in it. Rows are 0 based.
                HSSFRow titleRow = sheet.createRow(0);
                //需要加入序号列
                if (StringUtils.equals("1", serialNumber)) {
                    titleRow.createCell(0).setCellValue(serialNumberDesc);
                    for (int i = 1; i < sheetHead.length+1; i++) {

                        titleRow.createCell(i).setCellValue(sheetHead[i - 1]);
                    }
                    //插入需导出的数据
                    for (int i = 0; i < dataList.size(); i++) {
                        HSSFRow row = sheet.createRow(i + 1);
                        row.createCell(0).setCellValue((i + 1));
                        for (int j = 1; j < sheetHead.length + 1; j++) {
                            //计算时间片
                            if (!StringUtils.equals("-1", calcTimeIndex)) {
                                if (Integer.valueOf(calcTimeIndex) == (j - 1)) {
                                    row.createCell(j).setCellValue(getStepIntexTime(Integer.valueOf(dataList.get(i)[j - 1])));
                                } else {
                                    row.createCell(j).setCellValue(dataList.get(i)[j - 1]);
                                }
                            } else {
                                row.createCell(j).setCellValue(dataList.get(i)[j - 1]);
                            }
                        }
                    }
                } else {
                    //不需要加入序号列

                    for (int i = 0; i < sheetHead.length; i++) {
                        titleRow.createCell(i).setCellValue(sheetHead[i]);
                    }
                    //插入需导出的数据
                    for (int i = 0; i < dataList.size(); i++) {
                        HSSFRow row = sheet.createRow(i + 1);
                        for (int j = 0; j < sheetHead.length; j++) {

                            //计算时间片
                            if (!StringUtils.equals("-1", calcTimeIndex)) {
                                if (Integer.valueOf(calcTimeIndex) == j) {
                                    row.createCell(j).setCellValue(getStepIntexTime(Integer.valueOf(dataList.get(i)[j])));
                                } else {
                                    row.createCell(j).setCellValue(dataList.get(i)[j]);
                                }
                            } else {
                                row.createCell(j).setCellValue(dataList.get(i)[j]);
                            }

                        }
                    }
                }

                s++;
            }

            //校验文件路径是否存在
            File checkPath = new File(filePath);
            if (!checkPath.exists()) {
                checkPath.mkdirs();
            }
            if (!filePath.endsWith(File.separator)) {

                filePath = filePath + File.separator;
            }
            // Write the output to a file
            try (FileOutputStream fileOut = new FileOutputStream(filePath + fileName + "." + fileSuffix)) {
                wb.write(fileOut);
                fileOut.close();
            }

            LogShowUtils.show(showType, log, "生成文件耗时:" + (System.currentTimeMillis() - start));
            LogShowUtils.show(showType, log, "导出文件成功!文件导出路径:--" + filePath + fileName);
        }
    }

具体可参考
https://gitee.com/wahnn/ExportExcelXlsxAndXlshttps://gitee.com/wahnn/DynamicExportExcelFileToOSSDisk

猜你喜欢

转载自blog.csdn.net/fenfenguai/article/details/79642065
0条评论
添加一条新回复