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

时间:2022-05-06 19:13:51

[Text in 1 Cell]
ABC (Pink Color)
DEF (Black Color)
GHI (Red Color)

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

I have to check font color of text in cell like above. (I'm sorry that I can't upload image) Color of first row is pink. Color of next rows are black and red.

我需要像上面那样检查单元格中文本的字体颜色。(对不起,我上传不了图片)第一行的颜色是粉色。下一行的颜色是黑色和红色。

As you see, I can't use getCellStyle() method because the cell has 3 font attribute.

如您所见,我不能使用getCellStyle()方法,因为单元格有3个字体属性。

I typed source code like below.

我输入的源代码如下所示。

XSSFCell cell = row.getCell(0);

XSSFCell细胞= row.getCell(0);

XSSFRichTextString value = cell.getRichStringCellValue();

XSSFRichTextString value = cell.getRichStringCellValue();

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

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

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

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

int指数= value.getString().indexOf(信息);System.out.println(value.getFontAtIndex(指数).getColor());

}

}

But, I didn't get correct result. I want to know how I can get font information for each text.

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

Please inform me your great advice. Thank a lots. Have a good day!

请把你的忠告告诉我。感谢很多。有一个美好的一天!

1 个解决方案

#1


0  

Try the following: It will work

试试下面的方法:它会起作用。

   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
}

#1


0  

Try the following: It will work

试试下面的方法:它会起作用。

   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
}