Completed StylesTable initialization in order to create a new 'POIzed' file readable by Excel

git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@645234 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Paolo Mottadelli 2008-04-06 13:05:13 +00:00
parent b021ef1561
commit 2a1ee1eee3
6 changed files with 160 additions and 22 deletions

View File

@ -35,15 +35,19 @@ 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;
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;
@ -84,16 +88,8 @@ public class StylesTable implements StylesSource, XSSFModel {
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();
}
/**
@ -279,18 +275,24 @@ public class StylesTable implements StylesSource, XSSFModel {
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) {
@ -327,4 +329,51 @@ public class StylesTable implements StylesSource, XSSFModel {
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;
}
}

View File

@ -33,7 +33,6 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTXf;
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 {
@ -313,6 +312,14 @@ 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
@ -373,10 +380,6 @@ public class XSSFCellStyle implements CellStyle {
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);

View File

@ -22,7 +22,6 @@ import java.util.LinkedList;
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 {
@ -69,6 +68,10 @@ 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) {

View File

@ -1,3 +1,19 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.xssf.usermodel.extensions;
import java.util.LinkedList;

View File

@ -16,6 +16,7 @@
==================================================================== */
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 {
@ -65,4 +66,8 @@ public class XSSFColor {
public void setTint(double tint) {
color.setTint(tint);
}
public void setToBorder(CTBorderPr border) {
border.setColor(this.color);
}
}

View File

@ -22,6 +22,8 @@ import junit.framework.TestCase;
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;
@ -136,6 +138,66 @@ public class TestXSSFCellStyle extends TestCase {
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();