问题描述
我想写一个巨大的Excel文件时,我的要求让我写的行,忘了,所以我使用的它允许保留在内存中只排了几个号码,其余全部被写入文件。这有助于克服对大文件OutOfMemory例外。
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)
我能找到没有办法从。
我无法创建新的 HSSF调色板一>作为构造函数是受保护的。
I can not create a new HSSF palette as the constructor is protected.
这似乎是可行的,现在是以某种方式找到的所有predefined颜色列表相近的颜色,并使用它,而不是原来的(但这需要有一个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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!