Excel 如何通过ApachePOI确定单元格中文本的一部分的颜色
我有一张Excel表格。 在单元格中,我们可以有文本(Excel 如何通过ApachePOI确定单元格中文本的一部分的颜色,excel,apache-poi,Excel,Apache Poi,我有一张Excel表格。 在单元格中,我们可以有文本(,例如text1;text2),此文本的第1部分(text1)将为黑色, 但是另一个(text2)是红色的。 对于读取Excel文件,我使用ApachePOI 请您询问如何确定文本第二部分的颜色(,在我们的例子中是红色) 为了确定单元格中的整个文本,我现在使用以下代码: HSSFCellStyle cellStyle = (HSSFCellStyle) currentCell.getCellStyle(); sho
,例如text1;text2
),此文本的第1部分(text1
)将为黑色,
但是另一个(text2
)是红色的。
对于读取Excel文件,我使用ApachePOI
请您询问如何确定文本第二部分的颜色(,在我们的例子中是红色)
为了确定单元格中的整个文本,我现在使用以下代码:
HSSFCellStyle cellStyle = (HSSFCellStyle) currentCell.getCellStyle();
short cellColor = cellStyle.getFont(currentCell.getSheet().getWorkbook()).getColor();
我不知道它是否确定它是正确的您需要将单元格的内容作为
对于HSSF,您的代码应该是:
HSSFRichTextString richTextString = hssfCell.getRichStringCellValue();
for (int i=0; i<richTextString.0,r.numFormattingRuns(); i++) {
if (richTextString.getFontAtIndex(i) == HSSFRichTextString.NO_FONT) {
// Default cell formatting rules apply to this bit
} else {
HSSFFont font = workbook.getFontAt(richTextString.getFontAtIndex(i));
// This bit of the string has the above font
}
}
HSSFRichTextString richTextString=hssfCell.getRichStringCellValue();
对于(int i=0;i)您需要将其作为一个获取,然后按照您的方式获取每个块的格式。您尝试过了吗?您取得了多大的进展?感谢您的回答。我尝试过使用RichTextString,但我不知道如何获取零件的颜色。HSSFRichTextString RichTextString=(HSSFRichTextString)currentCell.getRichStringCellValue();
请参阅将特定字体应用于字符串部分的方法,但如何获取它?