java poi表头多级合并

步骤一:添加相关依赖

		<dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.1.1</version>
        </dependency>

步骤二:自定义VO类,用于表头合并

@Data
public class ExcelTopVo {
    @ApiModelProperty(value="名称")
    private String name;
    
    @ApiModelProperty(value="单位")
    private String unit = "";
    
    @ApiModelProperty(value="子数据")
    private List<ExcelTopVo> child = new ArrayList<>();
}

步骤三:poi实现表头合并

 public static XSSFWorkbook genTemplateByTitle(String sheetNames, String title, List<ExcelTopVo> excelTopVos) {
        XSSFWorkbook workbook = new XSSFWorkbook();

        XSSFSheet sheet;
        if (StringUtils.isBlank(sheetNames)) {
            sheet = workbook.createSheet();
        }else {
            sheet = workbook.createSheet(sheetNames);
        }

        // 标题
        XSSFCellStyle titleStyle = genStyle(workbook, true, null, true);

        // 正常
        XSSFCellStyle normalStyle = genStyle(workbook, false, null, true);

        // 竖向排列
        XSSFCellStyle verticalStyle = genStyle(workbook, false, (short) 255, true);


        // 表单起始行
        int firstRow = 0;

        //表单起始列
        int firstCol = 0;

        if (StringUtils.isNotBlank(title)) {
            XSSFRow row = sheet.createRow(firstRow);
            row.setHeight((short) (3 * 256));
            XSSFCell cell = row.createCell(firstCol);
            cell.setCellValue(title);
            cell.setCellStyle(titleStyle);
            firstRow = 1;
        }

        List<ExcelMergeVo> excelMergeVos = new ArrayList<>();

        XSSFRow firstRowS = sheet.createRow(firstRow);
        XSSFRow secondRowS = sheet.createRow(firstRow + 1);

        for (ExcelTopVo excelTopVo : excelTopVos) {

            ExcelMergeVo excelMergeVo = new ExcelMergeVo();
            List<ExcelTopVo> child = excelTopVo.getChild();
            XSSFCell cell1 = firstRowS.createCell(firstCol);

            String label = excelTopVo.getName();
            String unit = excelTopVo.getUnit();

            if (StringUtils.isNotBlank(unit)) {
                label += unit;
            }

            // 列间隔设置背景颜色
            if (child.size() > 0) {
                excelMergeVo.setFirstRow(firstRow);
                excelMergeVo.setLastRow(firstRow);
                excelMergeVo.setFirstCol(firstCol);
                excelMergeVo.setLaseCol(firstCol + child.size() - 1);


                for (ExcelTopVo childExcel : child) {
                    XSSFCell childCell = secondRowS.createCell(firstCol);

                    String childLabel = childExcel.getName();
                    String childUnit = childExcel.getUnit();

                    if (StringUtils.isNotBlank(childUnit)) {
                        childLabel += childUnit;
                    }


                    childCell.setCellValue(childLabel);
                    childCell.setCellStyle(normalStyle);
                    sheet.setColumnWidth(firstCol, sheet.getColumnWidth(firstCol) * 17 / 10);

                    firstCol++;
                }

                cell1.setCellValue(label);
                cell1.setCellStyle(normalStyle);
				sheet.getColumnWidth(firstCol - child.size() +1) * 17 / 10);

            } else {
                cell1.setCellValue(label);
                cell1.setCellStyle(normalStyle);
                sheet.autoSizeColumn(firstCol, true);
                sheet.setColumnWidth(firstCol, sheet.getColumnWidth(firstCol) * 17 / 10);

                // 设置合并单元格框线
                XSSFCell cell = secondRowS.createCell(firstCol);
                cell.setCellStyle(normalStyle);

                excelMergeVo.setFirstRow(firstRow);
                excelMergeVo.setLastRow(firstRow + 1);
                excelMergeVo.setFirstCol(firstCol);
                excelMergeVo.setLaseCol(firstCol);
                firstCol++;
            }

            excelMergeVos.add(excelMergeVo);
        }

        if (StringUtils.isNotBlank(title)) {
            ExcelMergeVo excelMergeVo = new ExcelMergeVo();
            excelMergeVo.setFirstCol(0);
            excelMergeVo.setLaseCol(firstCol - 1);
            excelMergeVo.setFirstRow(0);
            excelMergeVo.setLastRow(0);
            excelMergeVos.add(excelMergeVo);
        }

        // 合并单元格
        for (ExcelMergeVo mergeVo : excelMergeVos) {
            CellRangeAddress callRangeAddress = new CellRangeAddress(mergeVo.getFirstRow(), mergeVo.getLastRow(), mergeVo.getFirstCol(), mergeVo.getLaseCol());
            sheet.addMergedRegion(callRangeAddress);
        }

        return workbook;
    }
	// 设定字体样式
    private static XSSFCellStyle genStyle(XSSFWorkbook hssfWorkbook, boolean isTitle, Short rotation, boolean isBold) {
        XSSFCellStyle cellStyle = hssfWorkbook.createCellStyle();
        cellStyle.setAlignment(HorizontalAlignment.CENTER);//水平居中
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
        // 设置框线
        cellStyle.setBorderBottom(BorderStyle.THIN);
        cellStyle.setBorderTop(BorderStyle.THIN);
        cellStyle.setBorderLeft(BorderStyle.THIN);
        cellStyle.setBorderRight(BorderStyle.THIN);

        XSSFFont font = hssfWorkbook.createFont();
        font.setFontHeightInPoints((short) (isTitle ? 14 : 11)); //字体高度
        if (isBold) font.setBold(true);;
        cellStyle.setFont(font);
        if (null != rotation) cellStyle.setRotation(rotation);
        return cellStyle;
    }

大功告成,上效果

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序猿小张丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值