RichtextString在hssfworkbook单元格中没有工作

时间:2014-10-08 17:08:29

标签: java excel apache-poi

我想部分地为我的excel表单元格文本着色,所以我使用richtextstring但是它不起作用...没有错误。我无法弄清楚什么是错的。 这是我的代码。

在这个答案中是字符串..我想将字符串着色为最后一个(,)为红色并保持黑色

    if (answer.contains(",")) {

                                String s = ",";

                                int second = answer.lastIndexOf(s);


                                HSSFCell cell7 = thisRow.createCell((short) i);
                                cell7.setCellStyle(style2);
                                HSSFRichTextString richTextString = new HSSFRichTextString(
                                        answer);
                                richTextString.applyFont(0, second, style5.getFontIndex());

                                cell7.setCellValue("" + richTextString);

When i try to use                               cell7.setCellValue(richTextString);
it says : 
The method setCellValue(String) in the type HSSFCell is not applicable for the arguments (HSSFRichTextString)

1 个答案:

答案 0 :(得分:3)

您通过将RichTextString与空字符串String连接,意外地将""转换回""。这有效地删除了所有格式。

不要与cell7.setValue(richTextString); 连接,因为直接存在重载setCellValue method that takes a RichTextString

{{1}}