import org.apache.poi.ss.usermodel.StylesSource;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder;
+import org.apache.poi.xssf.usermodel.extensions.XSSFCellFill;
import org.apache.xmlbeans.XmlException;
import org.apache.xmlbeans.XmlOptions;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorder;
borders.add(border);
return borders.size() - 1;
}
-
+
+ public XSSFCellFill getFillAt(long idx) {
+ return new XSSFCellFill(fills.get((int) idx));
+ }
+ public long putFill(XSSFCellFill fill) {
+ return putFill(fill.getCTFill());
+ }
+ public synchronized long putFill(CTFill fill) {
+ if (fills.contains(fill)) {
+ return fills.indexOf(fill);
+ }
+ fills.add(fill);
+ return fills.size() - 1;
+ }
/**
* For unit testing only
*/
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.model.StylesTable;
import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder;
+import org.apache.poi.xssf.usermodel.extensions.XSSFCellFill;
import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder.BorderSides;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTXf;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STBorderStyle.Enum;
private CTXf cellXf;
private CTXf cellStyleXf;
private XSSFCellBorder cellBorder;
+ private XSSFCellFill cellFill;
/**
* Creates a Cell Style from the supplied parts
}
public short getFillBackgroundColor() {
- // TODO Auto-generated method stub
- return 0;
+ return (short) getCellFill().getFillBackgroundColor().getIndexed();
}
public short getFillForegroundColor() {
- // TODO Auto-generated method stub
- return 0;
+ return (short) getCellFill().getFillForegroundColor().getIndexed();
}
public short getFillPattern() {
- // TODO Auto-generated method stub
- return 0;
+ return (short) getCellFill().getPatternType().intValue();
}
public Font getFont(Workbook parentWorkbook) {
}
return (int) cellStyleXf.getBorderId();
}
+
+ private XSSFCellFill getCellFill() {
+ if (cellFill == null) {
+ cellFill = ((StylesTable)stylesSource).getFillAt(getFillId());
+ }
+ return cellFill;
+ }
+
+ private int getFillId() {
+ if (cellXf.isSetFillId()) {
+ return (int) cellXf.getFillId();
+ }
+ return (int) cellStyleXf.getFillId();
+ }
private Enum getBorderStyle(BorderSides side) {
return getCellBorder().getBorderStyle(side);
--- /dev/null
+package org.apache.poi.xssf.usermodel.extensions;\r
+\r
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFill;\r
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPatternFill;\r
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.STPatternType.Enum;\r
+\r
+public class XSSFCellFill {\r
+ \r
+ private CTFill fill;\r
+ \r
+ public XSSFCellFill(CTFill fill) {\r
+ this.fill = fill;\r
+ }\r
+ \r
+ public XSSFCellFill() {\r
+ this.fill = CTFill.Factory.newInstance();\r
+ }\r
+ \r
+ public XSSFColor getFillBackgroundColor() {\r
+ return new XSSFColor(getPatternFill().getBgColor());\r
+ }\r
+\r
+ public XSSFColor getFillForegroundColor() {\r
+ return new XSSFColor(getPatternFill().getFgColor());\r
+ }\r
+\r
+ public Enum getPatternType() {\r
+ return getPatternFill().getPatternType();\r
+ }\r
+\r
+ private CTPatternFill getPatternFill() {\r
+ CTPatternFill patternFill = fill.getPatternFill();\r
+ if (patternFill == null) {\r
+ patternFill = fill.addNewPatternFill();\r
+ }\r
+ return patternFill;\r
+ }\r
+\r
+ public CTFill getCTFill() {\r
+ return this.fill;\r
+ }\r
+\r
+}\r
import org.apache.poi.xssf.model.StylesTable;
import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder;
+import org.apache.poi.xssf.usermodel.extensions.XSSFCellFill;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorder;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFill;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPatternFill;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTStylesheet;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTXf;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STBorderStyle;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.STPatternType;
public class TestXSSFCellStyle extends TestCase {
private StylesTable stylesTable;
private CTBorder ctBorderA;
private CTBorder ctBorderB;
+ private CTFill ctFill;
private CTXf cellStyleXf;
private CTXf cellXf;
private XSSFCellStyle cellStyle;
ctBorderB = borderB.getCTBorder();
assertEquals(1, stylesTable.putBorder(borderB));
+ ctFill = CTFill.Factory.newInstance();
+ XSSFCellFill fill = new XSSFCellFill(ctFill);
+ long fillId = stylesTable.putFill(fill);
+ assertEquals(0, fillId);
+
cellStyleXf = ctStylesheet.addNewCellStyleXfs().addNewXf();
cellStyleXf.setBorderId(0);
cellXf = ctStylesheet.addNewCellXfs().addNewXf();
ctBorderA.addNewTop().setStyle(STBorderStyle.HAIR);
assertEquals("hair", cellStyle.getBorderTopAsString());
}
+
+ public void testGetFillBackgroundColor() {
+ CTPatternFill ctPatternFill = ctFill.addNewPatternFill();
+ CTColor ctBgColor = ctPatternFill.addNewBgColor();
+ ctBgColor.setIndexed(4);
+ assertEquals(4, cellStyle.getFillBackgroundColor());
+ }
+
+ public void testGetFillForegroundColor() {
+ CTPatternFill ctPatternFill = ctFill.addNewPatternFill();
+ CTColor ctFgColor = ctPatternFill.addNewFgColor();
+ ctFgColor.setIndexed(5);
+ assertEquals(5, cellStyle.getFillForegroundColor());
+ }
+
+ public void testGetFillPattern() {
+ CTPatternFill ctPatternFill = ctFill.addNewPatternFill();
+ ctPatternFill.setPatternType(STPatternType.DARK_DOWN);
+ assertEquals(8, cellStyle.getFillPattern());
+ }
}
--- /dev/null
+/* ====================================================================\r
+ Licensed to the Apache Software Foundation (ASF) under one or more\r
+ contributor license agreements. See the NOTICE file distributed with\r
+ this work for additional information regarding copyright ownership.\r
+ The ASF licenses this file to You under the Apache License, Version 2.0\r
+ (the "License"); you may not use this file except in compliance with\r
+ the License. You may obtain a copy of the License at\r
+\r
+ http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+ Unless required by applicable law or agreed to in writing, software\r
+ distributed under the License is distributed on an "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ See the License for the specific language governing permissions and\r
+ limitations under the License.\r
+==================================================================== */\r
+\r
+package org.apache.poi.xssf.usermodel.extensions;\r
+\r
+\r
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor;\r
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFill;\r
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPatternFill;\r
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.STPatternType;\r
+\r
+import junit.framework.TestCase;\r
+\r
+\r
+public class TestXSSFCellFill extends TestCase {\r
+ \r
+ public void testGetFillBackgroundColor() {\r
+ CTFill ctFill = CTFill.Factory.newInstance();\r
+ XSSFCellFill cellFill = new XSSFCellFill(ctFill);\r
+ CTPatternFill ctPatternFill = ctFill.addNewPatternFill();\r
+ CTColor bgColor = ctPatternFill.addNewBgColor();\r
+ assertNotNull(cellFill.getFillBackgroundColor());\r
+ bgColor.setIndexed(2);\r
+ assertEquals(2, cellFill.getFillBackgroundColor().getIndexed());\r
+ }\r
+ \r
+ public void testGetFillForegroundColor() {\r
+ CTFill ctFill = CTFill.Factory.newInstance();\r
+ XSSFCellFill cellFill = new XSSFCellFill(ctFill);\r
+ CTPatternFill ctPatternFill = ctFill.addNewPatternFill();\r
+ CTColor fgColor = ctPatternFill.addNewFgColor();\r
+ assertNotNull(cellFill.getFillForegroundColor());\r
+ fgColor.setIndexed(8);\r
+ assertEquals(8, cellFill.getFillForegroundColor().getIndexed());\r
+ }\r
+ \r
+ public void testGetPatternType() {\r
+ CTFill ctFill = CTFill.Factory.newInstance();\r
+ XSSFCellFill cellFill = new XSSFCellFill(ctFill);\r
+ CTPatternFill ctPatternFill = ctFill.addNewPatternFill();\r
+ ctPatternFill.setPatternType(STPatternType.DARK_DOWN);\r
+ assertEquals(8, cellFill.getPatternType().intValue());\r
+ }\r
+}\r