如何读取细胞中的Apache POI 3.9每个文本的字体颜色 [英] How to read font color of each text in cell in apache poi 3.9

查看:421
本文介绍了如何读取细胞中的Apache POI 3.9每个文本的字体颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[1单元格文本]结果
美国广播公司(粉红色)结果
DEF(黑色)结果
GHI(红的颜色)结果

[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.

正如你所见,我无法使用getCellStyle()方法,因为该单元有3个字体属性。

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

我输入源$ C ​​$ C像下面。

I typed source code like below.

XSSFCell电池= row.getCell(0);

XSSFRichTextString值= cell.getRichStringCellValue();

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

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

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!

推荐答案

请尝试以下操作:它将工作

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
}

这篇关于如何读取细胞中的Apache POI 3.9每个文本的字体颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆