如果自定义样式的话 请参照demo。这个需要使用poi的写法,请确认 写法是否有问题?
@Test public void testRichTextString() throws IOException { XSSFWorkbook wb = new XSSFWorkbook(); XSSFSheet sheet = wb.createSheet(); XSSFRow row = sheet.createRow(0); XSSFCell cell = row.createCell(0); XSSFFont fontRed = wb.createFont(); fontRed.setColor(Font.COLOR_RED); XSSFRichTextString richString = new XSSFRichTextString("表头必填*"); richString.applyFont(richString.length() - 1, richString.length(), fontRed); cell.setCellValue(richString); FileOutputStream fos = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\test1.xlsx"); wb.write(fos); fos.close(); }
`
@Test
public void testRichTextStringEasyExcel() {
EasyExcel.write("C:\Users\Administrator\Desktop\test2.xlsx", DemoData.class).
registerWriteHandler(new CellWriteHandler() {
@Override
public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Head head, int relativeRowIndex, boolean isHead) {
}
@Override
public void afterCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, CellData cellData, Cell cell, Head head, int relativeRowIndex, boolean isHead) {
if(isHead) {
Font fontRed = writeSheetHolder.getSheet().getWorkbook().createFont();
XSSFRichTextString richString = new XSSFRichTextString(cell.getStringCellValue());
richString.applyFont(richString.length() - 1, richString.length(), fontRed);
cell.setCellValue(richString);
}
}
}).
sheet("模板").doWrite(data());
}
`
用poi的写法是生效的
但是easyexcel是不生效的,
是不是有其他的配置
自定义样式无法满足需求,这边只需要将*设置为红色