【Excel导出--固定表头动态行数据--easyExcel】

  • 实现效果
    在这里插入图片描述
  • 代码实现
List<MesReportedWagesVo> list = reportedWages.getList();
        try {
            List<List<Object>> dataList = new ArrayList<>();

            BigDecimal totalPricePd = new BigDecimal(0);
            //Double totalPricePd = 0.0;
            Integer totalCount = 0;
            for (MesReportedWagesVo mesReportedWagesVo : list) {
                List<Object> rowData = new ArrayList<>();
                List<Object> rowDataTwo = new ArrayList<>();

                // 添加员工信息到行数据
                String name = "姓名:" + mesReportedWagesVo.getRealName() + ",工号:"+mesReportedWagesVo.getStaffId();
                String sum = "个人合计:" + mesReportedWagesVo.getSumCount() + "件,"+mesReportedWagesVo.getSumPricePd()+"元";
                rowData.add(name);
                //rowData.add(sum);
                totalPricePd.add(mesReportedWagesVo.getSumPricePd()) ;
                totalCount +=  mesReportedWagesVo.getSumCount();

                // 将包含员工信息的行数据添加到 dataList
                dataList.add(rowData);
                // 添加工资详情项到行数据(多行数据)
                for (MesEngineeringManagementDTO mesEngineeringManagementDTO : mesReportedWagesVo.getMesEngineeringManagementDTOList()) {
                    List<Object> itemData = new ArrayList<>();
                    itemData.add(mesEngineeringManagementDTO.getProduceNum());
                    itemData.add(mesEngineeringManagementDTO.getColorName());
                    itemData.add(mesEngineeringManagementDTO.getSizeCode());
                    itemData.add(mesEngineeringManagementDTO.getProductName());
                    itemData.add(mesEngineeringManagementDTO.getPricePd());
                    itemData.add(mesEngineeringManagementDTO.getCount());
                    itemData.add(mesEngineeringManagementDTO.getPrice());
                    dataList.add(itemData);
                }
                rowDataTwo.add(sum);
                dataList.add(rowDataTwo);
            }
            // 创建 ExcelWriter 对象
            String fileName ="报工工资统计表";
            fileName = URLEncoder.encode(fileName, "UTF-8");
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            ExcelWriter writer = EasyExcel.write(response.getOutputStream()).build();

            // 创建 Sheet 对象并设置表头
            Sheet sheet = new Sheet(1, 0);
            List<List<String>> data = new ArrayList<>();
            data.add(Arrays.asList("报工工资统计表","合计:"+totalCount+"件,金额:"+totalPricePd,"生产单号"));
            data.add(Arrays.asList("报工工资统计表","合计:"+totalCount+"件,金额:"+totalPricePd,"颜色"));
            data.add(Arrays.asList("报工工资统计表","合计:"+totalCount+"件,金额:"+totalPricePd,"尺码"));
            data.add(Arrays.asList("报工工资统计表","合计:"+totalCount+"件,金额:"+totalPricePd,"工序"));
            data.add(Arrays.asList("报工工资统计表","合计:"+totalCount+"件,金额:"+totalPricePd,"单价"));
            data.add(Arrays.asList("报工工资统计表","合计:"+totalCount+"件,金额:"+totalPricePd,"数量"));
            data.add(Arrays.asList("报工工资统计表","合计:"+totalCount+"件,金额:"+totalPricePd,"金额"));
            sheet.setHead(data);
            sheet.setAutoWidth(true);
            sheet.setSheetName("报工工资统计表");
            // 写入数据
            writer.write0(dataList, sheet);
            // 关闭 ExcelWriter 对象
            writer.finish();
        } catch (Exception e) {
            e.printStackTrace();
        }
  • 8
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
EasyExcel 支持导出合并和列的表头Excel,可以通过设置 `@HeadRowHeight`、`@HeadColumnWidth`、`@ContentRowHeight` 和 `@ContentColumnWidth` 注解来控制表头和内容的高和列宽。同时,可以通过设置 `@ExcelProperty` 注解的 `colspan` 和 `rowspan` 属性来合并和列。 以下是一个例子,导出一个合并了表头Excel: ```java @ExcelIgnoreUnannotated public class ExportData { @ExcelProperty(index = 0, value = "姓名", rowspan = 2, colspan = 2) @HeadRowHeight(30) @HeadColumnWidth(15) private String name; @ExcelProperty(index = 2, value = "性别", rowspan = 2) @HeadRowHeight(30) @HeadColumnWidth(15) private String gender; @ExcelProperty(index = 3, value = "联系方式", colspan = 2) @HeadRowHeight(30) @HeadColumnWidth(20) private String contact; @ExcelProperty(index = 4, value = "电话") @HeadRowHeight(30) @HeadColumnWidth(15) private String phone; @ExcelProperty(index = 5, value = "邮箱") @HeadRowHeight(30) @HeadColumnWidth(20) private String email; // 其他属性... // getter/setter 方法... } ``` 在这个例子中,我们使用 `@HeadRowHeight` 和 `@HeadColumnWidth` 注解设置表头高和列宽,使用 `@ContentRowHeight` 和 `@ContentColumnWidth` 注解设置内容的高和列宽。同时,我们使用 `@ExcelProperty` 注解的 `colspan` 和 `rowspan` 属性来合并和列。注意,需要将 `@ExcelIgnoreUnannotated` 注解添加到类上,以忽略未注解的属性。 然后,我们可以使用 EasyExcel 的 `ExcelWriter` 和 `Sheet` 类来写入数据。以下是一个示例代码: ```java public class ExcelExportUtil { public static void exportExcel(List<ExportData> dataList, String fileName, HttpServletResponse response) throws IOException { // 设置响应头 response.setContentType("application/vnd.ms-excel"); response.setCharacterEncoding("utf-8"); response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx"); // 创建 ExcelWriter ServletOutputStream outputStream = response.getOutputStream(); ExcelWriter excelWriter = new ExcelWriter(outputStream, ExcelTypeEnum.XLSX); // 创建 Sheet Sheet sheet = new Sheet(1, 0, ExportData.class); sheet.setTableStyle(createTableStyle()); // 写入数据 excelWriter.write(dataList, sheet); // 关闭 ExcelWriter excelWriter.finish(); } private static TableStyle createTableStyle() { TableStyle tableStyle = new TableStyle(); tableStyle.setTableContentBackGroundColor(IndexedColors.WHITE); tableStyle.setTableContentFontName("宋体"); tableStyle.setTableContentFontSize(12); tableStyle.setTableHeadBackGroundColor(IndexedColors.GREY_25_PERCENT); tableStyle.setTableHeadFontName("宋体"); tableStyle.setTableHeadFontSize(14); tableStyle.setTableHeadFontBold(true); tableStyle.setTableHeadFontColor(IndexedColors.WHITE); return tableStyle; } } ``` 在这个例子中,我们使用 `ExcelWriter` 和 `Sheet` 类来写入数据,使用 `createTableStyle()` 方法创建表格样式。注意,需要设置表格样式,否则导出Excel 可能会出现样式问题。 最后,在需要导出 Excel 的方法中调用 `ExcelExportUtil.exportExcel()` 方法即可。例如: ```java public class ExportController { @GetMapping("/export") public void export(HttpServletResponse response) throws IOException { List<ExportData> dataList = // 查询数据... ExcelExportUtil.exportExcel(dataList, "export", response); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值