如何获取XSSFSimpleShape对象的背景颜色?

时间:2017-06-15 04:03:10

标签: java apache-poi

POI-Framwork是否支持获取XSSFSimpleShape对象的背景颜色?我环顾了这堂课,但我找不到获得背景的方法? 这是我的代码:

XSSFSimpleShape simpleObj = ...
simpleObj.getCTShape().getSpPr()... get some things named color here
simpleObj.getCTShape().getStyle().getFillRef()... get some things named colors here

1 个答案:

答案 0 :(得分:1)

这与一项微不足道的任务相反。你是CTSolidColorFillProperties的正确方法。下一步是CTSRgbColor,我们会发现CTSchemeColor所有内容都很简单,因为这只是for (i=0;i< data.length ;i++) { var file = fs.openSync(listBlogsFile, 'r+'); var bufferedText = new Buffer(data[i] + modListData); fs.writeSync(file, bufferedText, 0, bufferedText.length, position); } 。但是你可以看到更多可能的颜色类型。

RGB经常使用的一种可能的颜色类型是HSL。此颜色是来自Excel的主题颜色,但可能是由ThemesTablelumMod的给定发光变化确定的。

示例XML:

lumOff

问题是这些发光变化是在Rob Camick中进行的。 <a:solidFill> <a:schemeClr val="accent4"> <a:lumMod val="60000"/> <a:lumOff val="40000"/> </a:schemeClr> </a:solidFill> Java仅支持java.awt.Color(又名HSB),但不支持HSV。因此,我们需要额外的代码来支持HSL。在我的示例中,我使用了enter image description here中的代码。

所以我们知道我们可以做到以下几点:

HSL

此代码将从import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFSimpleShape; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.poi.xssf.usermodel.XSSFColor; import org.apache.poi.xssf.model.ThemesTable; import java.io.InputStream; import java.io.FileInputStream; import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTShape; import org.openxmlformats.schemas.drawingml.x2006.main.CTShapeProperties; import org.openxmlformats.schemas.drawingml.x2006.main.CTSolidColorFillProperties; import org.openxmlformats.schemas.drawingml.x2006.main.STSchemeColorVal; import java.awt.Color; class ReadExcelShapeFillColors { //two methods for dealing with the HSL color model //helper method HueToRGB, see below private static float HueToRGB(float p, float q, float h) { if (h < 0) h += 1; if (h > 1 ) h -= 1; if (6 * h < 1) { return p + ((q - p) * 6 * h); } if (2 * h < 1 ) { return q; } if (3 * h < 2) { return p + ( (q - p) * 6 * ((2.0f / 3.0f) - h) ); } return p; } //get a new Color changed by given luminescence from lumMod and lumOff private static Color getColorLumModandOff(Color color, int lumMod, int lumOff) { float[] rgb = color.getRGBColorComponents( null ); float r = rgb[0]; float g = rgb[1]; float b = rgb[2]; float min = Math.min(r, Math.min(g, b)); float max = Math.max(r, Math.max(g, b)); float h = 0; if (max == min) h = 0; else if (max == r) h = ((60 * (g - b) / (max - min)) + 360) % 360; else if (max == g) h = (60 * (b - r) / (max - min)) + 120; else if (max == b) h = (60 * (r - g) / (max - min)) + 240; float l = (max + min) / 2; l = l * (float)lumMod/100000f + (float)lumOff/100000f; float s = 0; if (max == min) s = 0; else if (l <= .5f) s = (max - min) / (max + min); else s = (max - min) / (2 - max - min); h = h % 360.0f; h /= 360f; float q = 0; if (l < 0.5) q = l * (1 + s); else q = (l + s) - (s * l); float p = 2 * l - q; r = Math.max(0, HueToRGB(p, q, h + (1.0f / 3.0f))); g = Math.max(0, HueToRGB(p, q, h)); b = Math.max(0, HueToRGB(p, q, h - (1.0f / 3.0f))); r = Math.min(r, 1.0f); g = Math.min(g, 1.0f); b = Math.min(b, 1.0f); return new Color(r, g, b, 1.0f); } public static void main(String[] args) throws Exception { InputStream inp = new FileInputStream("ExcelWithSimpleShape.xlsx"); Workbook workbook = WorkbookFactory.create(inp); Sheet sheet = workbook.getSheetAt(0); Drawing<? extends Shape> drawing = sheet.getDrawingPatriarch(); for (Shape shape : drawing) { System.out.println(shape.getClass()); System.out.println(shape.getShapeName() + "_________________"); if (shape instanceof XSSFSimpleShape) { //we have a XSSFSimpleShape XSSFWorkbook xssfworkbook = (XSSFWorkbook)workbook; ThemesTable themesTable = xssfworkbook.getTheme(); XSSFSimpleShape xssfSimpleShape = (XSSFSimpleShape)shape; CTShape ctShape = xssfSimpleShape.getCTShape(); CTShapeProperties ctShapeProperties = ctShape.getSpPr(); byte[] bRGB; if (ctShapeProperties.isSetSolidFill()) { //we have a solid fill defined CTSolidColorFillProperties ctSolidColorFillProperties = ctShapeProperties.getSolidFill(); if (ctSolidColorFillProperties.isSetSrgbClr()) { //we have a explicit given RGB color bRGB = ctSolidColorFillProperties.getSrgbClr().getVal(); System.out.println((bRGB[0]&0xFF)+", "+(bRGB[1]&0xFF)+", "+(bRGB[2]&0xFF)); Color color = new Color(bRGB[0]&0xFF, bRGB[1]&0xFF, bRGB[2]&0xFF); System.out.println("explicit given RGB color: " + color); } else if (ctSolidColorFillProperties.isSetSchemeClr()) { //we have a scheme color defined in ThemesTable int iThemeColorIdx = ctSolidColorFillProperties.getSchemeClr().getVal().intValue()-1; System.out.println("theme color index: " + iThemeColorIdx); //get luminescence definition int lumMod = 100000; int lumOff = 0; if (ctSolidColorFillProperties.getSchemeClr().getLumModList().size() > 0) lumMod = ctSolidColorFillProperties.getSchemeClr().getLumModList().get(0).getVal(); if (ctSolidColorFillProperties.getSchemeClr().getLumOffList().size() > 0) lumOff = ctSolidColorFillProperties.getSchemeClr().getLumOffList().get(0).getVal(); System.out.println("lumMod: " + lumMod); System.out.println("lumOff: " + lumOff); XSSFColor xssfColor = themesTable.getThemeColor(iThemeColorIdx); bRGB = xssfColor.getRGB(); //RGB color from ThemesTable System.out.println((bRGB[0]&0xFF)+", "+(bRGB[1]&0xFF)+", "+(bRGB[2]&0xFF)); Color color = new Color(bRGB[0]&0xFF, bRGB[1]&0xFF, bRGB[2]&0xFF); color = getColorLumModandOff(color, lumMod, lumOff); //Color changed by given lumMod and lumOff System.out.println("scheme color: " + color); } } else { //we have accent1 scheme color as fill color XSSFColor xssfColor = themesTable.getThemeColor(STSchemeColorVal.INT_ACCENT_1-1); bRGB = xssfColor.getRGB(); System.out.println((bRGB[0]&0xFF)+", "+(bRGB[1]&0xFF)+", "+(bRGB[2]&0xFF)); Color color = new Color(bRGB[0]&0xFF, bRGB[1]&0xFF, bRGB[2]&0xFF); System.out.println("accent1 scheme color: " + color); } } } workbook.close(); } } 文件的第一张表中获取所有形状的填充颜色,只要它们是实心填充,XSSFWorkbookinstanceof XSSFSimpleShape CTSRgbColor {1}}。