本文介绍了在Apache POI 4.0中为XSSFWorkbook创建自定义颜色样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要在Apache POI 3.7及更低版本中为XSSFWorkbook应用自定义颜色,可以使用以下内容:

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

在版本4.0中,XSSFColor(java.awt.Color)被删除了。只有额外的'hackery'仍然可以
达到相同的效果:

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

但这样做的正确方法是什么?大多数XSSFColor方法都涉及一个IndexedColorMap,但我找不到任何关于如何在XSSFWorkbook中设置自定义颜色的示例。

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.

推荐答案

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

这篇关于在Apache POI 4.0中为XSSFWorkbook创建自定义颜色样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 14:53