[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!
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With