use*_*817 5 java excel apache-poi
[1单元格内的文本]
ABC(粉红色)
DEF(黑色)
GHI(红色)
我必须检查像上面的单元格中的文本的字体颜色。(对不起,我无法上传图像)第一行的颜色是粉红色。接下来的行的颜色是黑色和红色。
如您所见,我不能使用getCellStyle()方法,因为单元格具有3字体属性。
我输入了如下的源代码。
XSSFCell cell = row.getCell(0);
XSSFRichTextString value = cell.getRichStringCellValue();
String[] info = value.getString().split("\n");
Run Code Online (Sandbox Code Playgroud)
for(int i = 0; i < info.length; i++) {
int index = value.getString().indexOf(info);
System.out.println(value.getFontAtIndex(index).getColor());
}
但是,我没有得到正确的结果。我想知道如何获取每个文本的字体信息。
请告诉我您的好建议。非常感谢。祝你有美好的一天!
尝试以下操作:它会起作用
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
}
Run Code Online (Sandbox Code Playgroud)