Creating custom color styles for an XSSFWorkbook in Apache POI 4.0

流过昼夜 提交于 2019-12-22 08:29:27

问题


To apply a custom color for an XSSFWorkbook in Apache POI 3.7 and below the following was possible:

java.awt.Color c = new java.awt.Color (1,2,3)
XSSFCellStyle xcs = xssfWorkbook.createCellStyle();
XSSFFont headerFont = xssfWorkbook.createFont();
headerFont.setColor(new XSSFColor(c));
xcs.setFont(headerFont);
cell.setCellStyle(xcs);

In version 4.0 XSSFColor(java.awt.Color) got removed. It is still possible to achieve the same, just with additional 'hackery':

XSSFColor xc = new XSSFColor();
xc.setARGBHex(String.format("%02x%02x%02x",c.getRed(),c.getGreen(),c.getBlue())); 
headerFont.setColor(xc);

But what is the 'proper' way to do this? Most of the XSSFColor methods involve an IndexedColorMap but I could not find any example for how this could be used for setting custom colors in an XSSFWorkbook.


回答1:


byte[] rgb = {120, 100, (byte) 200};
headerFont.setColor(new XSSFColor(rgb, new DefaultIndexedColorMap()));


来源:https://stackoverflow.com/questions/52357448/creating-custom-color-styles-for-an-xssfworkbook-in-apache-poi-4-0

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!