设置背景自定义颜色不适用于Apache POI中的XSSF [英] Setting background custom color not working for XSSF in Apache POI

查看:619
本文介绍了设置背景自定义颜色不适用于Apache POI中的XSSF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了应该创建Excel文件(xlsx或xls)并为单元格设置自定义背景色的代码.创建xls文件时,背景颜色工作正常,但在xlsx的情况下,背景颜色未设置为正确的颜色.

I wrote code which should create an Excel file (xlsx or xls) and set a custom background color to cell. When creating the xls file the background color works fine, but in case of xlsx the background color is not set to the right color.

我的代码有什么问题?

public class PoiWriteExcelFile {
static Workbook workbook; 
static Sheet worksheet;

public static void main(String[] args) {
    try {
        String type = "xlsx"; //xls
        FileOutputStream fileOut = new FileOutputStream("D:\\poi-test." + type);            
        switch (type) {
        case "xls":
            workbook = new HSSFWorkbook();              
            break;              
        case "xlsx":
            workbook = new XSSFWorkbook();              
            break;          
        }

        CellStyle cellStyle = workbook.createCellStyle();
        switch (type) {
        case "xls":
            HSSFPalette palette = ((HSSFWorkbook) workbook).getCustomPalette();
             palette.setColorAtIndex(HSSFColor.LAVENDER.index, (byte)128, (byte)0, (byte)128);
             HSSFColor hssfcolor = palette.getColor(HSSFColor.LAVENDER.index);
             cellStyle.setFillForegroundColor(hssfcolor.getIndex());
            break;              
        case "xlsx":
            XSSFColor color = new XSSFColor(new java.awt.Color(128, 0, 128));
            cellStyle.setFillForegroundColor(color.getIndex());
            break;          
        }

        worksheet = workbook.createSheet("POI Worksheet");
        Row row1 = worksheet.createRow((short) 0);
        Cell cellA1 = row1.createCell((short) 0);
        cellA1.setCellValue("Hello");           
        cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);           
        cellA1.setCellStyle(cellStyle);

        workbook.write(fileOut);
        fileOut.flush();
        fileOut.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}
}

推荐答案

您正在尝试使用索引颜色,但是对于您的HSSF代码,找到了索引颜色,但未找到XSSF部分. Color.getIndex()将返回零,即黑色.

You are trying to use an indexed color, but with your code for HSSF the indexed color is found, but not for the XSSF part. There Color.getIndex() will return zero, which is black.

在颜色上有一种方法isIndexed(),您需要检查该颜色是否为索引颜色,然后才可以在POI-Color-object上使用getIndex().

There is a method isIndexed() on color which you need to check if the color is an indexed one and only then it makes sense to use getIndex() on the POI-Color-object.

您可以不使用索引色,而可以使用以下方法使用全色值,从而使其适用于XSSF:

You can make it work for XSSF by not using indexed colors, but the full color value by using the following:

((XSSFCellStyle)cellStyle).setFillForegroundColor(color);

通过这种方式设置实际颜色,结果工作簿将具有正确的背景.

This way you set the actual color and the resulting workbook will have the correct background.

这篇关于设置背景自定义颜色不适用于Apache POI中的XSSF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆