本文介绍了如何将普通的java Color转换为IndexedColors的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在java 1.6中工作,我使用apache poi来生成xls样式表。
我让用户使用jcolorchooser选择颜色以设置单元格的样式。
我面临的问题是apache poi的cellstyle的setfillforegroundcolor只接受indexedColors值,我将如何使用普通的颜色对象呢?或者如何将颜色对象转换为indexedColors?
或是否有更好的方法来解决这一问题?

I am working in java 1.6 and I use the apache poi in order to generate xls stylesheets.I am letting the user pick a color using jcolorchooser in order to style the cells.The problem I am facing is the cellstyle's setfillforegroundcolor of apache poi accepts only indexedColors value, how would I go about using the ordinary color object instead? or how about converting the color object into indexedColors?or is there a better approach to all this?

推荐答案

您可以实例化一个 XSSFColor 基于java.awt.Color,然后在该对象上调用 getIndexed()进行转换。不确定整体方法是否更好 - 我不是POI图书馆专家 - 尽管似乎表示你可以在整个过程中使用 XSSFColor ,而indexedColor只是为了向后兼容。它可能取决于您使用的POI库的版本。

You could instantiate an XSSFColor based on java.awt.Color, then call getIndexed() on that object to convert. Not sure if there's a better approach overall - I'm not a POI library specialist- although the javadocs seem to indicate that you can use XSSFColor throughout and the indexedColor is just for backward compatibility. it might depend on what version of the POI library you are using.

Color selectedColor = myJColorChoose.getColor();
XSSFColor userColor = new XSSFColor(selectedColor);
myCell.setFillForegroundColor(userColor.getIndexed());

这篇关于如何将普通的java Color转换为IndexedColors的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 16:43