如何阅读在apache poi中的单元格中的每个文本的字体颜色3.9

[单元格中的文本]
ABC(粉红色)
DEF(黑色)
GHI(红色)

我必须检查像上面的单元格中的文本的字体颜色。 (对不起,我不能上传图像)第一排的颜色是粉红色的。 下一排的颜色是黑色和红色。

正如你看到的,我不能使用getCellStyle()方法,因为单元格有3个字体属性。

我input如下的源代码。

XSSFCell cell = row.getCell(0);

XSSFRichTextString value = cell.getRichStringCellValue();

 String[] info = value.getString().split("\n"); 

for(int i = 0; i < info.length; i++) {

int index = value.getString().indexOf(info);
System.out.println(value.getFontAtIndex(index).getColor());

}

但是,我没有得到正确的结果。 我想知道如何获取每个文本的字体信息。

请告诉我你的好build议。 非常感谢。 祝你有美好的一天!

尝试以下操作:它将工作

  public static void differentColorInSingleCell(){ Workbook wb = new HSSFWorkbook(); Sheet sheet = wb.createSheet("Sheet1"); Cell cell = sheet.createRow(0).createCell(0); Font Font1 = wb.createFont(); Font1.setColor(HSSFColor.RED.index); Font Font2 = wb.createFont(); Font2.setColor(HSSFColor.BLUE.index); CellStyle style = wb.createCellStyle(); style.setFont(Font1); cell.setCellStyle(style); RichTextString richString = new HSSFRichTextString("RED, BLUE"); richString.applyFont(4, 9, Font2); cell.setCellValue(richString); //Write the file Now }