java导出Excel多个sheet、自定义列名

  • 【版权所有,文章允许转载,但须以链接方式注明源地址,否则追究法律责任】
  • 【创作不易,点个赞就是对我最大的支持】

前言

仅作为学习笔记,供大家参考
总结的不错的话,记得点赞收藏关注哦!
这里后台使用框架为若依

后台代码

Service层
    @Override
    public String exportHlxfxTj() {
        try {
            Workbook templateWorkbook = new XSSFWorkbook();//创建模板
            CellStyle style = templateWorkbook.createCellStyle();//定义风格
            style.setAlignment(HorizontalAlignment.CENTER);
            style.setVerticalAlignment(VerticalAlignment.CENTER);
            Sheet sheet1 = templateWorkbook.createSheet("资质等级");
            Sheet sheet2 = templateWorkbook.createSheet("所在城区");

            List<GcQyHlxfx> selectGcQyHlxfxListBydj = this.selectGcQyHlxfxListBydj();
            List<GcQyHlxfx> selectGcQyHlxfxListBycq = this.selectGcQyHlxfxListBycq();
            Field[] declaredFields = GcQyHlxfx.class.getDeclaredFields();
            Row row = sheet1.createRow(0); //创建第一行
            Cell cell = row.createCell(0); //创建第一列
            cell.setCellValue("序号"); //定义列名
            cell.setCellStyle(style);
            Cell cell1 = row.createCell(1); //创建第二列
            cell1.setCellValue("数据时间");
            cell1.setCellStyle(style);
            Cell cell2 = row.createCell(2);
            cell2.setCellValue("资质等级");
            cell2.setCellStyle(style);
            Cell cell3 = row.createCell(3);
            cell3.setCellValue("本次统计企业数");
            cell3.setCellStyle(style);
            Cell cell4 = row.createCell(4);
            cell4.setCellValue("有预估");
            cell4.setCellStyle(style);
            Cell cell5 = row.createCell(5);
            cell5.setCellValue("无预估");
            cell5.setCellStyle(style);
            Cell cell6 = row.createCell(6);
            cell6.setCellValue("数据错误");
            cell6.setCellStyle(style);
            Cell cell7 = row.createCell(7);
            cell7.setCellValue("不匹配");
            cell7.setCellStyle(style);
            Row row2 = sheet2.createRow(0); //创建第一行
            Cell cell0 = row2.createCell(0); //创建第一列
            cell0.setCellValue("序号"); //定义列名
            cell0.setCellStyle(style);
            Cell cell01 = row2.createCell(1); //创建第二列
            cell01.setCellValue("数据时间");
            cell01.setCellStyle(style);
            Cell cell02 = row2.createCell(2);
            cell02.setCellValue("所在城区");
            cell02.setCellStyle(style);
            Cell cell03 = row2.createCell(3);
            cell03.setCellValue("本次统计企业数");
            cell03.setCellStyle(style);
            Cell cell04 = row2.createCell(4);
            cell04.setCellValue("有预估");
            cell04.setCellStyle(style);
            Cell cell05 = row2.createCell(5);
            cell05.setCellValue("无预估");
            cell05.setCellStyle(style);
            Cell cell06 = row2.createCell(6);
            cell06.setCellValue("数据错误");
            cell06.setCellStyle(style);
            Cell cell07 = row2.createCell(7);
            cell07.setCellValue("不匹配");
            cell07.setCellStyle(style);
            for (int i = 0; i < selectGcQyHlxfxListBydj.size(); i++) {
                Row rows = sheet1.createRow(i + 1);
                rows.setHeight((short) (30 * 20));
                GcQyHlxfx hlxfx = selectGcQyHlxfxListBydj.get(i);
                sheet1.setColumnWidth(2, (int) ((30) * 256));
                int j = 0;
                Cell cells = rows.createCell(j);
                cells.setCellValue(i + 1);
                cells.setCellStyle(style);
                Cell cells1 = rows.createCell(++j);
                cells1.setCellValue(hlxfx.getYearMonths());
                cells1.setCellStyle(style);
                Cell cells2 = rows.createCell(++j);
                cells2.setCellValue(dictDataMapper.selectDictLabel("sys_qualifications_leave", hlxfx.getJzyzzdj().toString()));
                cells2.setCellStyle(style);

                Cell cells3 = rows.createCell(++j);
                cells3.setCellValue(hlxfx.getQysum());
                cells3.setCellStyle(style);
                Cell cells4 = rows.createCell(++j);
                cells4.setCellValue(hlxfx.getEstimated());
                cells4.setCellStyle(style);
                Cell cells5 = rows.createCell(++j);
                cells5.setCellValue(hlxfx.getNoEstimate());
                cells5.setCellStyle(style);
                Cell cells6 = rows.createCell(++j);
                cells6.setCellValue(hlxfx.getError());
                cells6.setCellStyle(style);
                Cell cells7 = rows.createCell(++j);
                cells7.setCellValue(hlxfx.getMismatching());
                cells7.setCellStyle(style);

            }
            for (int i = 0; i < selectGcQyHlxfxListBycq.size(); i++) {
                Row rows = sheet2.createRow(i + 1);
                rows.setHeight((short) (30 * 20));
                sheet2.setColumnWidth(2, (int) ((30) * 256));
                GcQyHlxfx hlxfx = selectGcQyHlxfxListBycq.get(i);
                int j = 0;
                Cell cells = rows.createCell(j);
                cells.setCellValue(i + 1);
                cells.setCellStyle(style);
                Cell cells1 = rows.createCell(++j);
                cells1.setCellValue(hlxfx.getYearMonths());
                cells1.setCellStyle(style);
                Cell cells2 = rows.createCell(++j);
                cells2.setCellValue(dictDataMapper.selectDictLabel("sys_city_code", hlxfx.getQyszcq().toString()));
                cells2.setCellStyle(style);
                Cell cells3 = rows.createCell(++j);
                cells3.setCellValue(hlxfx.getQysum());
                cells3.setCellStyle(style);
                Cell cells4 = rows.createCell(++j);
                cells4.setCellValue(hlxfx.getEstimated());
                cells4.setCellStyle(style);
                Cell cells5 = rows.createCell(++j);
                cells5.setCellValue(hlxfx.getNoEstimate());
                cells5.setCellStyle(style);
                Cell cells6 = rows.createCell(++j);
                cells6.setCellValue(hlxfx.getError());
                cells6.setCellStyle(style);
                Cell cells7 = rows.createCell(++j);
                cells7.setCellValue(hlxfx.getMismatching());
                cells7.setCellStyle(style);
            }
            String filename = UUID.randomUUID() + "_统计内容.xlsx";//定义文件名
            String downloadPath = TaisenConfig.getDownloadPath() + filename;//获取固定下载路径
            FileOutputStream outputStream = new FileOutputStream(new File(downloadPath));//写入文件
            templateWorkbook.write(outputStream);
            templateWorkbook.close();
            outputStream.close();
            return "/download/" + filename;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }
controller层
    /**
     * 导出统计内容
     * @return
     */
    @Log(title = "业绩管理", businessType = BusinessType.EXPORT)
    @PostMapping("/tjnrExport")
    @ResponseBody
    public AjaxResult yjglExport(){
        String s = gcQyHlxfxService.exportHlxfxTj();
        if(""!=s){
            return success(s);
        }
        return error("导出失败!!!");
    }

前端页面

html

   <a class="btn btn-warning" onclick="byLeaveExport()">
                    <i class="fa fa-download"></i> 统计内容导出
                </a>

js

         function byLeaveExport(){

            $.modal.confirm("确定导出统计内容吗?", function() {
                $.modal.loading("正在导出数据,请稍候...");
                $.post(prefix+"/tjnrExport", function(result) {
                    if (result.code == web_status.SUCCESS) {
                        window.location.href = ctx + "common/download?fileName=" + encodeURI(result.msg) + "&delete=" + true;
                    } else if (result.code == web_status.WARNING) {
                        $.modal.alertWarning(result.msg)
                    } else {
                        $.modal.alertError(result.msg);
                    }
                    $.modal.closeLoading();
                });
            });
        }

创作不易,点个赞就是对我最大的支持~


wxgzh:程序员温眉

CSDN:程序员温眉

每天进步一点点的程序员

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 好的,这是一个关于Java操作Excel的问题,我可以回答。要在Java导出多个Sheet页的Excel,你需要使用Apache POI这个Java库。以下是一个示例代码,可以在Java导出包含多个Sheet页的Excel文件: ``` import java.io.FileOutputStream; import java.io.IOException; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class ExcelExportExample { public static void main(String[] args) { try (Workbook workbook = new XSSFWorkbook()) { // 创建Sheet1 Sheet sheet1 = workbook.createSheet("Sheet1"); // 创建Sheet2 Sheet sheet2 = workbook.createSheet("Sheet2"); // 写入Sheet1数据 Cell cell11 = sheet1.createRow(0).createCell(0); cell11.setCellValue("Sheet1-Row1-Cell1"); // 写入Sheet2数据 Cell cell21 = sheet2.createRow(0).createCell(0); cell21.setCellValue("Sheet2-Row1-Cell1"); // 导出Excel文件 try (FileOutputStream outputStream = new FileOutputStream("example.xlsx")) { workbook.write(outputStream); } System.out.println("Excel导出成功!"); } catch (IOException e) { e.printStackTrace(); } } } ``` 在这个示例代码中,我们使用XSSFWorkbook创建了一个新的Excel文件。然后我们创建了两个Sheet页,分别是"Sheet1"和"Sheet2"。接着我们在每个Sheet页中创建了一个单元格,并写入了一些数据。最后我们使用FileOutputStream将Excel文件写入磁盘中。 希望这个示例能够帮助到你! ### 回答2: 在Java中,我们可以使用Apache POI库来导出多个sheet页的Excel文件。下面是一个简单的示例代码: ```java import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.FileOutputStream; import java.io.IOException; public class ExcelExporter { public static void main(String[] args) { try (Workbook workbook = new XSSFWorkbook()) { // 创建第一个sheetSheet sheet1 = workbook.createSheet("Sheet1"); // 创建第一个sheet页的第一行,并设置数据 Row row1 = sheet1.createRow(0); Cell cell1 = row1.createCell(0); cell1.setCellValue("Sheet1中的第一行,第一列的数据"); // 创建第二个sheetSheet sheet2 = workbook.createSheet("Sheet2"); // 创建第二个sheet页的第一行,并设置数据 Row row2 = sheet2.createRow(0); Cell cell2 = row2.createCell(0); cell2.setCellValue("Sheet2中的第一行,第一列的数据"); // 保存Excel文件 try (FileOutputStream outputStream = new FileOutputStream("多个sheet页的Excel文件.xlsx")) { workbook.write(outputStream); } System.out.println("Excel文件导出成功"); } catch (IOException e) { e.printStackTrace(); } } } ``` 以上代码使用了Apache POI库的`XSSFWorkbook`类来创建一个新的Excel文件。我们使用`createSheet`方法来创建多个sheet页,并使用`createRow`和`createCell`方法来创建行和单元格,并设置相应的数据。最后,使用`FileOutputStream`将Workbook对象写入到文件中。 以上代码只是一个简单的示例,您可以根据自己的需求来设置更多的行和单元格,并可以为每个sheet设置不同的数据。希望对您有帮助! ### 回答3: 在Java中,可以使用Apache POI库来导出Excel多个sheet页。首先,你需要在你的项目中包含Apache POI库的依赖。 接下来,你可以创建一个Workbook对象,该对象代表整个Excel文档。使用Workbook对象的createSheet方法可以创建一个新的sheet页。你可以使用sheet页的setName方法来设置sheet页的名称。 然后,你可以使用Row和Cell对象来创建和填充sheet页中的行和单元格。使用Row对象的createCell方法可以创建一个新的单元格,然后使用Cell对象的setCellValue方法来设置单元格的值。 最后,将生成的数据写入到Excel文件中。你可以使用Workbook对象的write方法将Workbook对象中的数据写入到文件中。 以下是一个简单的示例代码来导出多个sheet页的Excel文件: ```java import org.apache.poi.ss.usermodel.*; import java.io.FileOutputStream; import java.io.IOException; public class ExportExcelMultiSheet { public static void main(String[] args) { Workbook workbook = new XSSFWorkbook(); createSheet(workbook, "Sheet1"); createSheet(workbook, "Sheet2"); createSheet(workbook, "Sheet3"); try (FileOutputStream fileOut = new FileOutputStream("output.xlsx")) { workbook.write(fileOut); } catch (IOException e) { e.printStackTrace(); } System.out.println("Excel文件导出成功!"); } private static void createSheet(Workbook workbook, String sheetName) { Sheet sheet = workbook.createSheet(sheetName); Row headerRow = sheet.createRow(0); Cell headerCell = headerRow.createCell(0); headerCell.setCellValue(sheetName + " Header"); Row dataRow = sheet.createRow(1); Cell dataCell = dataRow.createCell(0); dataCell.setCellValue(sheetName + " Data"); } } ``` 上述代码创建了一个名为"output.xlsx"的Excel文件,其中包含了三个sheet页,分别为"Sheet1"、"Sheet2"和"Sheet3"。每个sheet页中都包含了一个标题行和一个数据行。 希望上述信息能够帮助到你!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值