Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read font color of each text in cell in apache poi 3.9

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

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.

I typed source code like below.

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());

}

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!

like image 312
user3109817 Avatar asked Nov 01 '22 08:11

user3109817


1 Answers

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
}
like image 79
Sankumarsingh Avatar answered Nov 14 '22 14:11

Sankumarsingh