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
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值