且构网

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

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

更新时间:2021-09-03 08:33:13

这是由于 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).唯一的希望是在更高版本的apache poi中将setFillForegroundColor(Color color)添加到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.