APACHE POI 4.1:从十六进制代码设置单元格背景颜色 [英] APACHE POI 4.1 : Set cell background color from hex code
问题描述
我尝试了在堆栈溢出上发布的不同解决方案来将背景颜色应用于 Apache POI 生成的单元格,但没有任何效果.
I've tried different solutions posted on stack overflow to apply a background color to an Apache POI generated cell, but nothing worked.
我正在做类似的事情:
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet(sheetName);
XSSFCellStyle cellStyle = ((XSSFCellStyle) workbook.createCellStyle());
if (styleObject.getBgColor() != null) {
java.awt.Color javaBdgColor = java.awt.Color.decode(voceStyle.getBgColor()); // this is #FFF000
XSSFColor bgColor = new XSSFColor(javaBdgColor, new DefaultIndexedColorMap());
cellStyle.setFillForegroundColor(bgColor.getIndex());
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
}
Row newRow = Rowsheet.createRow(0);
Cell newCell = newRow.createCell(0);
newCell.setCellStyle(cellStyle);
// write file
String pathFileExport = buildPathExportFile("test-export");
FileOutputStream fileOut = new FileOutputStream(pathFileExport);
workbook.write(fileOut);
fileOut.close();
//close workbook
workbook.close();
return Paths.get(pathFileExport);
我认为在我的代码中一切正常,但每个单元格样式都将导致黑色背景.
I think everything is ok in my code but every cell styled like that will result in a black background.
我对没有字段的调试结果中的DefaultIndexedColorMap"实例有一些疑问:
I have some doubts about "DefaultIndexedColorMap" instance that's during debugging results without fields:
此时,我不确定该怎么做才能解决.其他帖子中的每个人似乎都能正常工作,但我的背景仍然是深色而不是黄色.
At this point, I'm not sure about what to do to solve. Everyone in other posts seems to get things working but I'm still getting dark backgrounds instead of yellow.
有什么建议吗?提前致谢!
Any suggestions? Thanks in advance!
推荐答案
正如另一个答案所说,setFillForegroundColor(XSSFColor color) 而不是在 XSSFCellStyle 中使用索引颜色
当涉及到自定义颜色时.但是使用来自 org.apache.poi 的索引颜色.ss.usermodel.IndexedColors 在 XSSF
中也是可能的.如果不需要使用自定义颜色,这将是最兼容的方式.
As the other answer tells, usage of setFillForegroundColor(XSSFColor color) instead of using indexed colors is necessary in XSSFCellStyle
when it comes to customized colors. But usage of indexed colors from org.apache.poi.ss.usermodel.IndexedColors is possible in XSSF
too. And this will be the most compatible way if using customized colors is not necessary.
但也应避免从 java.awt.Color
创建 XSSFColor
.构造函数 XSSFColor(java.awt.Color clr, IndexedColorMap map) 被标记为仅测试".并且 java.awt.Color
在某些情况下将不可用.
But also creating the XSSFColor
from java.awt.Color
should be avoided. The constructor XSSFColor(java.awt.Color clr, IndexedColorMap map) is marked "TEST ONLY". And java.awt.Color
will not be available in some circumstances.
因此,如果需要从十六进制代码设置单元格背景颜色"并且十六进制代码在 String
中,则 org.apache.commons.codec.binary.Hex
可用于从该 String
获取一个 byte[]
数组.Apache commons codec
已经是 apache poi
的依赖项之一.然后构造函数 XSSFColor(byte[] rgb, IndexedColorMap colorMap) 可以使用.IndexedColorMap
直到现在还没有使用.所以可以设置null
.如果 IndexedColorMap
稍后使用,则无论如何都必须调整代码.
So if the need is "set cell background color from hex code" and the hex code is in a String
, then org.apache.commons.codec.binary.Hex
can be used to get an byte[]
array from that String
. Apache commons codec
is one of apache poi
's dependencies already. Then constructor XSSFColor(byte[] rgb, IndexedColorMap colorMap) can be used. IndexedColorMap
has no usage until now. So it can be set null
. If IndexedColorMap
gets any usage later, then the code has to be adjusted anyway.
示例:
import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import org.apache.commons.codec.binary.Hex;
class CreateXSSFColor {
public static void main(String[] args) throws Exception {
try (Workbook workbook = new XSSFWorkbook();
FileOutputStream fileout = new FileOutputStream("Excel.xlsx") ) {
String rgbS = "FFF000";
byte[] rgbB = Hex.decodeHex(rgbS); // get byte array from hex string
XSSFColor color = new XSSFColor(rgbB, null); //IndexedColorMap has no usage until now. So it can be set null.
XSSFCellStyle cellStyle = (XSSFCellStyle) workbook.createCellStyle();
cellStyle.setFillForegroundColor(color);
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
Sheet sheet = workbook.createSheet();
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("yellow");
cell.setCellStyle(cellStyle);
workbook.write(fileout);
}
}
}
这篇关于APACHE POI 4.1:从十六进制代码设置单元格背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!