Java 在 SXSSF (Apache POI) 中使用自定义颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20561710/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 02:49:19  来源:igfitidea点击:

Using custom colors with SXSSF (Apache POI)

javaexcelapache-poi

提问by gaurav5430

I am trying to write a huge excel file, my requirement allows me to write the row and forget, so i am using SXSSFwhich allows to keep only a few number of rows in memory and rest all are written to the document. this helps in overcoming outofmemory exception for large files.

我正在尝试编写一个巨大的 excel 文件,我的要求允许我写行并忘记,所以我使用SXSSF,它允许在内存中只保留几行,其余的都写入文档。这有助于克服大文件的内存不足异常。

but I also need to set styles to cells using sxssf workbook. i am not able to find a way to define and use custom colors in SXSSF (like in XSSF, we can define a custom color directly, and in HSSF we can replace an entry in the palette with a custom color)

但我还需要使用 sxssf 工作簿为单元格设置样式。我找不到在 SXSSF 中定义和使用自定义颜色的方法(就像在 XSSF 中一样,我们可以直接定义自定义颜色,而在 HSSF 中,我们可以用自定义颜色替换调色板中的条目)

i can find no way to access a palette from SXSSF workbook.

我找不到从SXSSF 工作簿访问调色板的方法

I can not create a new HSSF paletteas the constructor is protected.

由于构造函数受到保护,我无法创建新的HSSF 调色板

the only way that seems feasible right now is to somehow find a similar color from a list of all predefined colors and use it, instead of the original (but that would require having a rgb color matching algo, which would be another task)

现在似乎可行的唯一方法是以某种方式从所有预定义颜色的列表中找到类似的颜色并使用它,而不是原始颜色(但这需要有一个 rgb 颜色匹配算法,这将是另一项任务)

Can someone suggest a workaround (or maybe suggest a primitive rgb color matching algorithm)

有人可以建议一种解决方法(或者可能建议一种原始的 rgb 颜色匹配算法)

采纳答案by gaurav5430

So, after a bit of searching through the web and reading the docs, i got a hint that SXSSF is actually a wrapper around XSSF, so i typecasted the CellStyle returned by SXSSF workbook to XSSF and was able to use XSSFColor directly for generating colors.

因此,在通过网络搜索并阅读文档后,我得到了一个提示,即 SXSSF 实际上是 XSSF 的包装器,因此我将 SXSSF 工作簿返回的 CellStyle 类型转换为 XSSF,并且能够直接使用 XSSFColor 生成颜色。

SXSSFWorkbook workbook = new SXSSFWorkbook(50); 
Sheet sheet = workbook.createSheet("Image Data"); 
....
Cell cell = row.createCell(j);
cell.setCellValue(j);
XSSFCellStyle cs1 = (XSSFCellStyle) workbook.createCellStyle();
cs1.setFillForegroundColor(new XSSFColor(new java.awt.Color(red,green,blue)));          
cs1.setFillPattern(CellStyle.SOLID_FOREGROUND);
cell.setCellStyle(cs1);

回答by Marcelo Tocchetto

To avoid the need of typecast for the cellStyles, create first a XSSFWorkbook with cellStyles (XSSFCellStyle) applying the custom colors and then wrap it with a SXSSFWorkbook constructor like the sample below:

为了避免对 cellStyles 进行类型转换,首先创建一个带有 cellStyles (XSSFCellStyle) 的 XSSFWorkbook,应用自定义颜色,然后使用 SXSSFWorkbook 构造函数将其包裹起来,如下例所示:

/**
 * Sample based on POI <a href="http://poi.apache.org/spreadsheet/how-to.html#sxssf">Spreadsheet How-To</a>.
 * 
 * @see <a href="https://poi.apache.org/apidocs/org/apache/poi/xssf/streaming/SXSSFWorkbook.html">SXSSFWorkbook</a>
 */
public static void main(String[] args) throws Throwable {

    XSSFWorkbook xssfWorkbook = new XSSFWorkbook();

    XSSFColor colorGrey = new XSSFColor(new Color(210, 210, 210));
    XSSFCellStyle cellStyleGrey = xssfWorkbook.createCellStyle();
    cellStyleGrey.setFillPattern(CellStyle.SOLID_FOREGROUND);
    cellStyleGrey.setFillForegroundColor(colorGrey);

    // keep 100 rows in memory, exceeding rows will be flushed to disk
    SXSSFWorkbook sxssfWorkbook = new SXSSFWorkbook(xssfWorkbook, 100);
    Sheet sheet = sxssfWorkbook.createSheet();

    for (int rownum = 0; rownum < 1000; rownum++) {
        Row row = sheet.createRow(rownum);
        for (int cellnum = 0; cellnum < 10; cellnum++) {
            Cell cell = row.createCell(cellnum);
            String address = new CellReference(cell).formatAsString();
            cell.setCellValue(address);

            // for even rows apply the grey cellStyle
            if (rownum % 2 == 0) {
                cell.setCellStyle(cellStyleGrey);
            }
        }

    }

    // Omitted asserts block from original sample...

    FileOutputStream out = new FileOutputStream("/temp/sxssf.xlsx");
    sxssfWorkbook.write(out);
    out.close();

    // dispose of temporary files backing this workbook on disk
    sxssfWorkbook.dispose();
}

回答by Jerome Edward

CellStyle style = workbook.createCellStyle(); // workbook is of SXSSF type
byte orange[] = new byte[] { (byte) 248, (byte) 203, (byte) 173 };
byte thick_shade_blue[] = new byte[] { (byte) 142, (byte) 169, (byte) 219 };
byte blue[] = new byte[] { (byte) 180, (byte) 198, (byte) 231 };

((XSSFCellStyle) style).setFillForegroundColor(new XSSFColor(thick_shade_blue, null));
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);