Excel单元格内两种字体颜色
在导入Excel时,要求某一列为必填列 ,可能会存在这样的需求:项目名称 * , * 要设置为红色。
代码如下(示例):
private void setCellFornStyle(XSSFWorkbook workbook) {
//需要设置的单元格
XSSFCell cell = workbook.getSheetAt(0).getRow(0).getCell(1);
String title = cell.getStringCellValue();
//设置字体样式
Font rad = workbook.createFont();
rad.setColor((short) 2);
XSSFRichTextString richString = new XSSFRichTextString(title);
//通过索引指定哪些文字需要什么颜色
richString.applyFont(0, title.length(), rad); //字体显示哪部分字符设置新的字体样式Font
cell.setCellValue(richString);
}
``