Quantcast
Channel: Java Programming Forum - Learn Java Programming - Apache POI
Viewing all articles
Browse latest Browse all 120

How to convert XSSFColor to java.awt.Color?

$
0
0
I working with Excel support for a software which uses both xls and xlsx file formats. A small part of the code is shown below.

I am writing code which gets java.awt.Color from org.apache.poi.ss.usermodel.Color. I haven't find any general way to do that.
That's why HSSFColor and XSSFColor have separate blocks of code. At least my XSSFColor implementation is hack even if it is working.

Is there a general way to convert org.apache.poi.ss.usermodel.Color to java.awt.Color? If not, is there a better way to get java.awt.Color from XSSFColor?

private Color getColor(org.apache.poi.ss.usermodel.Color col) {
Color color = null;
if (col instanceof HSSFColor) {
HSSFColor c = (HSSFColor) col;
short[] triplet = c.getTriplet();
color = new Color(triplet[0], triplet[1], triplet[2]);
} else if (col instanceof XSSFColor) {
XSSFColor c = (XSSFColor) col;
String rgbHex = c.getCTColor().getDomNode().getAttributes().getNam edItem("rgb").getNodeValue();
if (rgbHex.length() > 6) {
rgbHex = rgbHex.substring(2);
}
color = Color.decode("#" + rgbHex);
}
return color;
}

Thanks for Helping me! :)-:

Viewing all articles
Browse latest Browse all 120

Trending Articles