(8) 如何用Apache POI操作Excel文件-----POI-3.10.1 的一个黑白颜色颠倒的bug以及解决方案

在用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();

    }
}





  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值