繁体   English   中英

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

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

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

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

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

我输入如下的源代码。

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

}

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

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

请尝试以下方法:它会起作用

   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
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM