[alibaba/easyexcel]单元格合并后边框样式没有被正确设置

2024-05-21 49 views
7

表头单元格合并,合并后的单元格边框没有被正确设置,会出现很淡的原始边框,和原有单元格的黑色边框看起来很明显。 我尝试使用了

public class MergedRegionStyleHandler implements SheetWriteHandler {

    @Override
    public void beforeSheetCreate (WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
        // do nothing
    }

    @Override
    public void afterSheetCreate (WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
        Sheet currentSheet = writeSheetHolder.getSheet();
        List<CellRangeAddress> cellRangeAddresses =  currentSheet.getMergedRegions();
        cellRangeAddresses.forEach(region->{
            RegionUtil.setBorderLeft(BorderStyle.THIN, region, currentSheet);
            RegionUtil.setBorderTop(BorderStyle.THIN, region, currentSheet);
            RegionUtil.setBorderRight(BorderStyle.THIN, region, currentSheet);
            RegionUtil.setBorderBottom(BorderStyle.THIN, region, currentSheet);
        });
    }
}

但是没有生效 debug看到afterSheetCreate的调用时机,早于设置Head的初始化设置

 private void initSheet(WriteTypeEnum writeType) {
        WriteHandlerUtils.beforeSheetCreate(this);
        Sheet currentSheet;
        try {
            if (writeSheetHolder.getSheetNo() != null) {
                currentSheet = writeWorkbookHolder.getWorkbook().getSheetAt(writeSheetHolder.getSheetNo());
                writeSheetHolder
                    .setCachedSheet(writeWorkbookHolder.getCachedWorkbook().getSheetAt(writeSheetHolder.getSheetNo()));
            } else {
                // sheet name must not null
                currentSheet = writeWorkbookHolder.getWorkbook().getSheet(writeSheetHolder.getSheetName());
                writeSheetHolder
                    .setCachedSheet(writeWorkbookHolder.getCachedWorkbook().getSheet(writeSheetHolder.getSheetName()));
            }
        } catch (Exception e) {
            currentSheet = createSheet();
        }
        if (currentSheet == null) {
            currentSheet = createSheet();
        }
        writeSheetHolder.setSheet(currentSheet);
        // 这里调用所有的SheetWriteHandler#afterSheetCreate
        WriteHandlerUtils.afterSheetCreate(this);
        if (WriteTypeEnum.ADD.equals(writeType)) {
            // Initialization head
            // 这里才去初始化head数据
            initHead(writeSheetHolder.excelWriteHeadProperty());
        }
        writeWorkbookHolder.getHasBeenInitializedSheetIndexMap().put(writeSheetHolder.getSheetNo(), writeSheetHolder);
        writeWorkbookHolder.getHasBeenInitializedSheetNameMap().put(writeSheetHolder.getSheetName(), writeSheetHolder);
    }

建议: 是否可以将SheetWriteHandler#afterSheetCreate的调用放到初始化head之后呢? 或者新增一类handler在整个sheet处理完后,包括数据写入完成后,整体对sheet的样式做一些自定义操作?

回答

3

调用RowWriteHandler

6

我也试了下,确实实现不了,放弃了

8

有实现的吗 我搞了半天也是不行调用RowWriteHandler直接整行加上了 有没有demo研究下的