Excel复杂表头构建

final String[] title2 = {"序号", "商品编码", "商品编码映射", "商品名称", "商品单位", "商品规格", "供应商", "订购总数量"};
final int[] width2 = {
        (int) ((3 + 0.72) * 256), (int) ((12.5 + 0.72) * 256),
        (int) ((11.5 + 0.72) * 256), (int) ((30 + 0.72) * 256),
        (int) ((13.5 + 0.72) * 256), (int) ((10.5 + 0.72) * 256),
        (int) ((7.5 + 0.72) * 256), (int) ((3.5 + 0.72) * 256)
};
header2.put("merge", 7);  //首行合并 可根据自己的需求设计 设计指定行合并
header2.put("alternate", 0);  //指定奇偶行变色
header2.put("title", title2);  //设置行头
header2.put("headerCell", width2); //设置指定行款
head.put("header2", header2);
列举部分代码

private void setTopHeader(List<ExportModel> list, XSSFWorkbook workbook, XSSFSheet sheet, String header) {
    Map<String, Object> map = (Map<String, Object>) head.get(header);
    final String[] headers = (String[]) map.get("title");
    int mg = (int) map.get("merge");
    XSSFCellStyle cs = workbook.createCellStyle();
    cs.setAlignment(HorizontalAlignment.CENTER);
    cs.setVerticalAlignment(VerticalAlignment.CENTER);
    cs.setBorderBottom(BorderStyle.THIN);
    cs.setBorderTop(BorderStyle.THIN);
    cs.setBorderLeft(BorderStyle.THIN);
    cs.setBorderRight(BorderStyle.THIN);

    XSSFFont font = workbook.createFont();
    font.setFontHeight(14);
    font.setBold(true);
    cs.setFont(font);
    XSSFRow row0 = sheet.createRow(0);
    XSSFCell xs = row0.createCell(0);
    xs.setCellValue(deliveryList.get(0).getDate() + "-sheet名称");
    xs.setCellStyle(cs);
    CellRangeAddress ca = new CellRangeAddress(0, 0, 0, mg + deliveist.size());
    sheet.addMergedRegion(ca);
    RegionUtil.setBorderBottom(BorderStyle.THIN, ca, sheet); // 下边框
    RegionUtil.setBorderLeft(BorderStyle.THIN, ca, sheet); // 左边框
    RegionUtil.setBorderRight(BorderStyle.THIN, ca, sheet); // 有边框
    RegionUtil.setBorderTop(BorderStyle.THIN, ca, sheet); // 上边框

    XSSFRow row1 = sheet.createRow(1);
    XSSFCellStyle cs1 = workbook.createCellStyle();
    cs1.setAlignment(HorizontalAlignment.CENTER);
    cs1.setVerticalAlignment(VerticalAlignment.CENTER);
    cs1.setBorderBottom(BorderStyle.THIN);
    cs1.setBorderTop(BorderStyle.THIN);
    cs1.setBorderLeft(BorderStyle.THIN);
    cs1.setBorderRight(BorderStyle.THIN);
    XSSFFont font1 = workbook.createFont();
    font1.setFontHeight(10);
    font1.setBold(true);
    cs1.setFont(font1);
    cs1.setWrapText(true);
    final int[] width = (int[]) map.get("headerCell");
    for (int i = 0; i < headers.length; i++) {
        sheet.setColumnWidth(i, width[i]);
        setCellValue(cs1, row1, i, headers[i]);
    }
    final XSSFColor grayXssfColor = new XSSFColor(new java.awt.Color(220, 220, 220));
    // 白色
    final XSSFColor whiteXssfColor = new XSSFColor(new java.awt.Color(255, 255, 255));
    XSSFCellStyle cs2 = workbook.createCellStyle();
    cs2.cloneStyleFrom(cs1);
    cs2.setRotation((short) 255);
    cs2.setBorderBottom(BorderStyle.THIN);
    cs2.setBorderTop(BorderStyle.THIN);
    cs2.setBorderLeft(BorderStyle.THIN);
    cs2.setBorderRight(BorderStyle.THIN);
    cs2.setFillForegroundColor(grayXssfColor);
    cs2.setFillPattern(FillPatternType.SOLID_FOREGROUND);
    XSSFCellStyle cs3 = workbook.createCellStyle();
    cs3.cloneStyleFrom(cs2);
    cs3.setFillForegroundColor(whiteXssfColor);
    cs3.setFillPattern(FillPatternType.NO_FILL);
    AtomicInteger ai = new AtomicInteger(headers.length);
    int alternate = (int) map.get("alternate");
    for (ExportModel x : list) {
        sheet.setColumnWidth(ai.get(), (int) ((3.0 + 0.72) * 256));
        if (ai.get() % 2 == alternate) {
            setCellValue(cs2, row1, ai.get(), x.getBName());
        } else {
            setCellValue(cs3, row1, ai.get(), x.getAName());
        }
        ai.addAndGet(1);
    }
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
EasyExcel 是一个基于 Java 的 Excel 文件读写工具,可以方便地进行复杂表头的导出操作。 要实现复杂表头的导出,首先需要定义好表头的结构。可以通过创建一个对象来表示每个表头单元格的信息,包括单元格的值、合并的行数和列数等。然后,可以通过递归的方式构建整个表头的结构。 以下是一个示例代码,演示如何使用 EasyExcel 导出带有复杂表头Excel 文件: ```java // 创建 ExcelWriter 对象 ExcelWriter writer = EasyExcel.write("output.xlsx").build(); // 定义表头的结构 List<List<String>> headers = new ArrayList<>(); List<String> headerRow1 = Arrays.asList("A", "B", "C"); List<String> headerRow2 = Arrays.asList("D", "E", "F"); List<String> headerRow3 = Arrays.asList("G", "H", "I"); headers.add(headerRow1); headers.add(headerRow2); headers.add(headerRow3); // 构建表头 buildHeader(writer, headers, 0, 0, headers.size() - 1); // 写入数据 List<List<Object>> data = new ArrayList<>(); // 添加数据到 data 列表 // 将数据写入 Excel 文件 writer.write(data, new Sheet(1, 0)); // 关闭 ExcelWriter 对象 writer.finish(); ``` 在上述代码中,`buildHeader` 方法用于递归地构建表头。它接受表头的数据、起始行和起始列等参数,通过递归将表头写入到 Excel 文件中。 这只是一个简单的示例,实际情况中可能需要根据具体需求进行更复杂表头设计和数据处理。你可以根据自己的需求进行调整和扩展。 需要注意的是,使用 EasyExcel 进行导出,需要在项目的依赖中添加 EasyExcel 的相关库。你可以在 EasyExcel 的官方网站(https://www.yuque.com/easyexcel/doc/easyexcel)上找到更详细的使用文档和示例代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值