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的样式做一些自定义操作?