在用POI-3.10.1 的版本设置Excel单元格的字体的颜色或者单元格背景色的时候,对于XSSF方式,如果设置的颜色为黑色,则实际在Excel中渲染出来的却是白色;反之,如果设置的颜色为白色,则实际在Excel中渲染出来的却是黑色。
这个是POI-3.10中XSSF中一个bug. 重现的代码如下:
import java.awt.Color;
import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
/**
* XSSF Color issue
*/
public class ColorIssues {
public static void main(String[] args) throws Exception {
<span style="white-space:pre"> </span>XSSFWorkbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet("Sheet1");
Row row = sheet.createRow((short) 0);
// 1.设置第一行第一列 (A1)
XSSFCellStyle style = wb.createCellStyle();
Cell cell = row.createCell((short) 0);
cell.setCellValue(new XSSFRichTextString("Hello"));
cell.setCellStyle(style);
//2.设置第一行第二列(A2)的字体为白色,结果却变成了黑色
style = wb.createCellStyle();
XSSFFont ztFont = wb.createFont();
ztFont.setColor(new XSSFColor(Color.WHITE)); // 将字体设置为“白色”
//ztFont.setColor(HSSFColor.WHITE.index);
style.setFont(ztFont);
style.setFillForegroundColor(new XSSFColor(Color.BLUE));
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
cell = row.createCell((short) 1);
cell.setCellValue("World");
cell.setCellStyle(style);
// 输出Excel文件
FileOutputStream fileOut = new FileOutputStream("colorissus.xlsx");
wb.write(fileOut);
fileOut.close();
}
}
如上图代码所示意,我设置了字体的颜色为白色,结果在输出的Excel文件中却是黑色。
ztFont.setColor(new XSSFColor(Color.WHITE)); // 将字体设置为“白色”
解决方案至少有两个.
(1) 方案一
把POI的版本从POI 3.10 换成 POI 3.12,则这个问题将会自动解决,不需要修改任何的代码。
(2) 方案二
保持POI的版本不变,把上面代码中的第30行代码
ztFont.setColor(new XSSFColor(Color.WHITE)); // 将字体设置为“白色”
替换成
ztFont.setColor(HSSFColor.WHITE.index);
具体代码如下:
import java.awt.Color;
import java.io.FileOutputStream;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
/**
* XSSF Color issue
*/
public class ColorIssues {
public static void main(String[] args) throws Exception {
XSSFWorkbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet("Sheet1");
Row row = sheet.createRow((short) 0);
// 1.设置第一行第一列 (A1)
XSSFCellStyle style = wb.createCellStyle();
Cell cell = row.createCell((short) 0);
cell.setCellValue(new XSSFRichTextString("Hello"));
cell.setCellStyle(style);
//2.设置第一行第二列(A2)的字体为白色,结果是正确的
style = wb.createCellStyle();
XSSFFont ztFont = wb.createFont();
ztFont.setColor(HSSFColor.WHITE.index);
style.setFont(ztFont);
style.setFillForegroundColor(new XSSFColor(Color.BLUE));
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
cell = row.createCell((short) 1);
cell.setCellValue("World");
cell.setCellStyle(style);
// 输出Excel文件
FileOutputStream fileOut = new FileOutputStream("colorissus.xlsx");
wb.write(fileOut);
fileOut.close();
}
}