且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

无法在 XSSFCell Apache POI 中设置自定义颜色

更新时间:2021-10-01 09:28:34

这是由于包 org.apache.poi.ss.util.

PropertyTemplate 以及 CellUtilRegionUtil 仅基于 ss.usermodel 级别而不是基于xssf.usermodel 级别.但是 org.apache.poi.ss.usermodel.CellStyle 直到现在还不知道 setFillForegroundColor(Color color) 的一些事情.它只知道setFillForegroundColor(short bg).所以 ss.usermodel 级别直到现在还不能将 Color 设置为填充前景色.只有 short(颜色索引)是可能的.

PropertyTemplate as well as CellUtil and RegionUtilare be based on ss.usermodel level only and not on xssf.usermodel level. But org.apache.poi.ss.usermodel.CellStyle does not know something about a setFillForegroundColor(Color color) until now. It only knows setFillForegroundColor(short bg). So ss.usermodel level simply cannot set a Color as fill foreground color until now. Only a short (a color index) is possible.

如果谈到为什么只需要使用 org.apache.poi.ss.util 设置边框时需要设置颜色的问题,那么答案是,这是必要的,因为两者,颜色和边框,在同一个 CellStyle 中.这就是为什么在将边框设置添加到 CellStyle 时,必须保持颜色设置并最终重新设置.

If it comes to the question why setting the color is necessary when only the border shall be set using org.apache.poi.ss.util then the answer is, it is necessary because both, color and border, are in the same CellStyle. Thats why when adding the border settings to the CellStyle, the color settings must be maintain and finally be set new.

所以总而言之,没有办法摆脱这种困境.如果你需要使用 org.apache.poi.ss.util 那么你不能同时使用 setFillForegroundColor(XSSFColor color).唯一的希望是 setFillForegroundColor(Color color) 将在 apache poi的后续版本中添加到 org.apache.poi.ss.usermodel.CellStyle>.

So in conclusion, there is not a way out of this dilemma. If you need using org.apache.poi.ss.util then you cannot use setFillForegroundColor(XSSFColor color) the same time. The only hope is setFillForegroundColor(Color color) will be added to org.apache.poi.ss.usermodel.CellStyle in later versions of apache poi.