本文介绍了在 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