We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
@Data @Slf4j @AllArgsConstructor @NoArgsConstructor public class FlexibleCellStyleHandler extends HeadStyleHandler { // Map<列數, Triple<開頭Index, 長度, colorindex>> private Map<Integer, Triple<Integer, Integer, Short>> rowColorMap; public void afterCellDispose( WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List<WriteCellData<?>> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) { log.info("呼叫監聽器"); // 先呼叫父層監聽器 super.afterCellDispose( writeSheetHolder, writeTableHolder, cellDataList, cell, head, relativeRowIndex, isHead); if (rowColorMap != null && rowColorMap.containsKey(cell.getColumnIndex())) { // 獲取對應列的顏色映射(Triple格式) Triple<Integer, Integer, Short> columnStartIndexToColor = rowColorMap.get(cell.getColumnIndex()); Integer startIndex = columnStartIndexToColor.getLeft(); // 獲取開始索引 Integer changSize = columnStartIndexToColor.getMiddle(); // 獲取長度 Short colorIndex = columnStartIndexToColor.getRight(); // 獲取顏色(RGB) // 檢查單元格內容是否超過開始索引,如果是,則改變顏色 if (cell.getStringCellValue().length() >= startIndex) { var oldCell = cellDataList.get(0); cellDataList.set( 0, changeCellColor(oldCell, writeSheetHolder, colorIndex, startIndex, changSize)); } } } private WriteCellData<String> changeCellColor( WriteCellData<?> oldCell, WriteSheetHolder writeSheetHolder, Short colorIndex, Integer startIndex, Integer changSize) { String cellText = oldCell.getStringValue(); var style = oldCell.getWriteCellStyle(); log.info("測試rich 回傳新格子 :{}", cellText); log.info("測試rich :{}", LocalDateTime.now()); WriteCellData<String> cell = new WriteCellData(); cell.setType(CellDataTypeEnum.RICH_TEXT_STRING); // 建立RichTextString RichTextStringData richText = new RichTextStringData(cellText); cell.setRichTextStringDataValue(richText); WriteFont font = new WriteFont(); font.setColor(colorIndex); int endIndex = Math.min(cellText.length(), startIndex + changSize); richText.applyFont(startIndex, endIndex, font); return cell; } }
ExcelWriter excelWriter = FastExcel.write(outputStream).inMemory(true).build(); WriteSheet tsetSheet = FastExcel.writerSheet(0, "測試") .head(QotPolicyInsuredBeneficiaryPoi.class) .registerWriteHandler(redText) .build(); excelWriter.write(demoPoi.getRight(), tsetSheet);
我希望把某一個指定標頭的文字 "比例(%)(不可填%)"當中 "(不可填%)" 的部分替換成紅色文字 但是我自定義的其他樣式都有顯示出來但文字卻沒有變更顏色
The text was updated successfully, but these errors were encountered:
/**
@author lolkt
标题第三行
红色* + 黑色标题 */ public class TplTitleThirdLineWriteHandler implements CellWriteHandler {
@OverRide public void afterCellDispose(CellWriteHandlerContext context) { Cell cell = context.getCell(); // 拿到poi的workbook Workbook workbook = context.getWriteWorkbookHolder().getWorkbook(); String cellValue = cell.getStringCellValue(); if (cellValue.startsWith("*")) { XSSFRichTextString richTextString = new XSSFRichTextString(cellValue); Font firstCharFont = workbook.createFont(); firstCharFont.setColor(Font.COLOR_RED); richTextString.applyFont(0, 1, firstCharFont); Font otherCharFont = workbook.createFont(); richTextString.applyFont(1, cellValue.length(), otherCharFont); // 再设置回每个单元格里 cell.setCellValue(richTextString); } }
}
ExcelWriterBuilder writerBuilder = EasyExcel.write(tempFile).inMemory(Boolean.TRUE); writerBuilder.registerWriteHandler(new TplTitleThirdLineWriteHandler()); 仅供参考
Sorry, something went wrong.
No branches or pull requests
异常代码
问题描述
我希望把某一個指定標頭的文字 "比例(%)(不可填%)"當中 "(不可填%)" 的部分替換成紅色文字
但是我自定義的其他樣式都有顯示出來但文字卻沒有變更顏色
The text was updated successfully, but these errors were encountered: