在 SXSSF (Apache POI) 中使用自定义颜色 [英] Using custom colors with SXSSF (Apache POI)

查看:141
本文介绍了在 SXSSF (Apache POI) 中使用自定义颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to write a huge excel file, my requirement allows me to write the row and forget, so i am using SXSSF which 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.

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

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 工作簿.

我无法创建新的 HSSF 调色板 因为构造函数是受保护的.

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

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

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)

推荐答案

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

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.

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);

这篇关于在 SXSSF (Apache POI) 中使用自定义颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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