help wanted
Description
异常代码
@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);
问题描述
我希望把某一個指定標頭的文字 "比例(%)(不可填%)"當中 "(不可填%)" 的部分替換成紅色文字 但是我自定義的其他樣式都有顯示出來但文字卻沒有變更顏色