Repository navigation
标题行单独设置样式的方式
#490
|
discussions-467 请问如果这个固定的标题行需要单独设置一个样式(与下方表头单元格样式不同)有什么比较好的实现方法? 目前我的实现是自定义SheetWriteHandler,然后在afterSheetCreate方法中通过poi直接插入新行作为标题,后续再写入表头和数据。 public class CustomTitleSheetWriteHandler implements SheetWriteHandler {
@Override
public void afterSheetCreate(SheetWriteHandlerContext context) {
//使用poi的方式创建单元格并设置样式
Workbook workbook = context.getWriteWorkbookHolder().getWorkbook();
Sheet sheet = workbook.getSheetAt(sheetIndex);
Row row = sheet.createRow(titleRowIndex);
Cell cell = row.createCell(0);
cell.setCellStyle(style)
CellRangeAddress cellRangeAddress = new CellRangeAddress(titleRowIndex, titleRowEndIndex, 0, titleColEndIndex);
sheet.addMergedRegionUnsafe(cellRangeAddress);
}
}能否通过自行实现AbstractHeadStyleWriteHandler分别设置标题和表头的样式?我是需要直接对setHeadCellStyle中的relativeRowIndex进行判断吗?(导出时设置表头样式) 或者还有什么被我忽略掉的更好的方式? public class CustomHeadStyleWriteHandler extends AbstractHeadStyleWriteHandler {
@Override
protected void setHeadCellStyle(Cell cell, Head head, Integer relativeRowIndex) {
//判断relativeRowIndex?
CellStyle style = cell.getSheet().getWorkbook().createCellStyle();
style.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
cell.setCellStyle(style);
}
} |
Answered by
alaahong
Aug 22, 2025
Replies: 2 comments
|
how about fill with template? |
0 replies
|
As current head is based on HeadStyle only, you might raise one issue on the HeadStyles to seek the possible for mapping the styles from @ExcelProperty({"a","b"}) |
0 replies
Answer selected by
nyc1215
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment

As current head is based on HeadStyle only, you might raise one issue on the HeadStyles to seek the possible for mapping the styles from @ExcelProperty({"a","b"})
Then try to implement or waiting for others.