import org.apache.xmlbeans.XmlException;
import org.apache.xmlbeans.XmlOptions;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorder;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorders;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellStyleXfs;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellXfs;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFill;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFills;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFont;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFonts;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTNumFmt;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTNumFmts;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTStylesheet;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTXf;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.STFontScheme;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.STPatternType;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.StyleSheetDocument;
public StylesTable() {
doc = StyleSheetDocument.Factory.newInstance();
doc.addNewStyleSheet();
-
- // Add a single, default cell xf and cell style xf
- // Excel seems to require these
- CTXf[] ctxfs = new CTXf[2];
- for (int i = 0; i < ctxfs.length; i++) {
- ctxfs[i] = CTXf.Factory.newInstance();
- ctxfs[i].setNumFmtId(0);
- }
- xfs.add(ctxfs[0]);
- styleXfs.add(ctxfs[1]);
+ // Initialization required in order to make the document readable by MSExcel
+ initialize();
}
/**
doc.getStyleSheet().setNumFmts(formats);
// Fonts
- CTFonts fnts = CTFonts.Factory.newInstance();
- fnts.setCount(fonts.size());
- fnts.setFontArray(
+ CTFonts ctFonts = CTFonts.Factory.newInstance();
+ ctFonts.setCount(fonts.size());
+ ctFonts.setFontArray(
fonts.toArray(new CTFont[fonts.size()])
);
- doc.getStyleSheet().setFonts(fnts);
+ doc.getStyleSheet().setFonts(ctFonts);
// Fills
- // TODO
+ CTFills ctFills = CTFills.Factory.newInstance();
+ ctFills.setCount(fills.size());
+ ctFills.setFillArray(fills.toArray(new CTFill[fills.size()]));
+ doc.getStyleSheet().setFills(ctFills);
// Borders
- // TODO
+ CTBorders ctBorders = CTBorders.Factory.newInstance();
+ ctBorders.setCount(borders.size());
+ ctBorders.setBorderArray(borders.toArray(new CTBorder[borders.size()]));
+ doc.getStyleSheet().setBorders(ctBorders);
// Xfs
if(xfs.size() > 0) {
private long putFont(XSSFFont font, LinkedList<CTFont> fonts) {
return font.putFont(fonts);
}
+ private void initialize() {
+ CTFont ctFont = createDefaultFont();
+ fonts.add(ctFont);
+
+ CTFill ctFill = createDefaultFill();
+ fills.add(ctFill);
+
+ CTBorder ctBorder = createDefaultBorder();
+ borders.add(ctBorder);
+
+ CTXf styleXf = createDefaultXf();
+ styleXfs.add(styleXf);
+ CTXf xf = createDefaultXf();
+ xf.setXfId(0);
+ xfs.add(xf);
+ }
+ private CTXf createDefaultXf() {
+ CTXf ctXf = CTXf.Factory.newInstance();
+ ctXf.setNumFmtId(0);
+ ctXf.setFontId(0);
+ ctXf.setFillId(0);
+ ctXf.setBorderId(0);
+ return ctXf;
+ }
+ private CTBorder createDefaultBorder() {
+ CTBorder ctBorder = CTBorder.Factory.newInstance();
+ ctBorder.addNewBottom();
+ ctBorder.addNewTop();
+ ctBorder.addNewLeft();
+ ctBorder.addNewRight();
+ ctBorder.addNewDiagonal();
+ return ctBorder;
+ }
+ private CTFill createDefaultFill() {
+ CTFill ctFill = CTFill.Factory.newInstance();
+ ctFill.addNewPatternFill().setPatternType(STPatternType.NONE);
+ return ctFill;
+ }
+ private CTFont createDefaultFont() {
+ CTFont ctFont = CTFont.Factory.newInstance();
+ ctFont.addNewSz().setVal(11);
+ ctFont.addNewColor().setTheme(1);
+ ctFont.addNewName().setVal("Calibri");
+ ctFont.addNewFamily().setVal(2);
+ ctFont.addNewScheme().setVal(STFontScheme.MINOR);
+ return ctFont;
+ }
}
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STBorderStyle;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STHorizontalAlignment;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STVerticalAlignment;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.STVerticalAlignment.Enum;
public class XSSFCellStyle implements CellStyle {
getCellAlignment().setWrapText(wrapped);
}
+ public XSSFColor getBorderColor(BorderSide side) {
+ return getCellBorder().getBorderColor(side);
+ }
+
+ public void setBorderColor(BorderSide side, XSSFColor color) {
+ getCellBorder().setBorderColor(side, color);
+ }
+
private XSSFCellBorder getCellBorder() {
if (cellBorder == null) {
// TODO make a common Cell Border object
private short getBorderColorIndexed(BorderSide side) {
return (short) getBorderColor(side).getIndexed();
}
-
- private XSSFColor getBorderColor(BorderSide side) {
- return getCellBorder().getBorderColor(side);
- }
private void setBorderColorIndexed(BorderSide side, long color) {
getBorderColor(side).setIndexed(color);
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorder;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorderPr;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STBorderStyle;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.STBorderStyle.Enum;
public class XSSFCellBorder {
}
return new XSSFColor(getBorder(side).getColor());
}
+
+ public void setBorderColor(BorderSide side, XSSFColor color) {
+ color.setToBorder(getBorder(side));
+ }
private CTBorderPr getBorder(BorderSide side) {
switch (side) {
+/* ====================================================================\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
package org.apache.poi.xssf.usermodel.extensions;\r
\r
import java.util.LinkedList;\r
==================================================================== */
package org.apache.poi.xssf.usermodel.extensions;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorderPr;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor;
public class XSSFColor {
public void setTint(double tint) {
color.setTint(tint);
}
+
+ public void setToBorder(CTBorderPr border) {
+ border.setColor(this.color);
+ }
}
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.XSSFColor;
+import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder.BorderSide;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorder;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellXfs;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor;
assertEquals("hair", cellStyle.getBorderTopAsString());
}
+ public void testGetSetBottomBorderColor() {
+ CTColor ctColor = ctBorderA.addNewBottom().addNewColor();
+ ctColor.setIndexed(2);
+ XSSFColor color = new XSSFColor(ctColor);
+ assertEquals((short)2, cellStyle.getBottomBorderColor());
+ CTColor anotherCtColor = CTColor.Factory.newInstance();
+ anotherCtColor.setIndexed(4);
+ anotherCtColor.setTheme(3);
+ anotherCtColor.setRgb("1234".getBytes());
+ XSSFColor anotherColor = new XSSFColor(anotherCtColor);
+ cellStyle.setBorderColor(BorderSide.BOTTOM, anotherColor);
+ assertEquals((short)4, cellStyle.getBottomBorderColor());
+ assertEquals(new String("1234".getBytes()), new String(cellStyle.getBorderColor(BorderSide.BOTTOM).getRgb()));
+ }
+
+ public void testGetSetTopBorderColor() {
+ CTColor ctColor = ctBorderA.addNewTop().addNewColor();
+ ctColor.setIndexed(5);
+ XSSFColor color = new XSSFColor(ctColor);
+ assertEquals((short)5, cellStyle.getTopBorderColor());
+ CTColor anotherCtColor = CTColor.Factory.newInstance();
+ anotherCtColor.setIndexed(7);
+ anotherCtColor.setTheme(3);
+ anotherCtColor.setRgb("abcd".getBytes());
+ XSSFColor anotherColor = new XSSFColor(anotherCtColor);
+ cellStyle.setBorderColor(BorderSide.TOP, anotherColor);
+ assertEquals((short)7, cellStyle.getTopBorderColor());
+ assertEquals(new String("abcd".getBytes()), new String(cellStyle.getBorderColor(BorderSide.TOP).getRgb()));
+ }
+
+ public void testGetSetLeftBorderColor() {
+ CTColor ctColor = ctBorderA.addNewLeft().addNewColor();
+ ctColor.setIndexed(2);
+ XSSFColor color = new XSSFColor(ctColor);
+ assertEquals((short)2, cellStyle.getLeftBorderColor());
+ CTColor anotherCtColor = CTColor.Factory.newInstance();
+ anotherCtColor.setIndexed(4);
+ anotherCtColor.setTheme(3);
+ anotherCtColor.setRgb("1234".getBytes());
+ XSSFColor anotherColor = new XSSFColor(anotherCtColor);
+ cellStyle.setBorderColor(BorderSide.LEFT, anotherColor);
+ assertEquals((short)4, cellStyle.getLeftBorderColor());
+ assertEquals(new String("1234".getBytes()), new String(cellStyle.getBorderColor(BorderSide.LEFT).getRgb()));
+ }
+
+ public void testGetSetRightBorderColor() {
+ CTColor ctColor = ctBorderA.addNewRight().addNewColor();
+ ctColor.setIndexed(8);
+ XSSFColor color = new XSSFColor(ctColor);
+ assertEquals((short)8, cellStyle.getRightBorderColor());
+ CTColor anotherCtColor = CTColor.Factory.newInstance();
+ anotherCtColor.setIndexed(14);
+ anotherCtColor.setTheme(3);
+ anotherCtColor.setRgb("af67".getBytes());
+ XSSFColor anotherColor = new XSSFColor(anotherCtColor);
+ cellStyle.setBorderColor(BorderSide.RIGHT, anotherColor);
+ assertEquals((short)14, cellStyle.getRightBorderColor());
+ assertEquals(new String("af67".getBytes()), new String(cellStyle.getBorderColor(BorderSide.RIGHT).getRgb()));
+ }
+
public void testGetFillBackgroundColor() {
CTPatternFill ctPatternFill = ctFill.addNewPatternFill();
CTColor ctBgColor = ctPatternFill.addNewBgColor();