patch #45881 from Sourcesense: implemented cell fills and borders, added examples

git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@698674 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Yegor Kozlov 2008-09-24 17:53:29 +00:00
parent 6130d5cb83
commit dca93e084f
6 changed files with 491 additions and 254 deletions

View File

@ -1,59 +1,63 @@
/* ==================================================================== /* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership. this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0 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 not use this file except in compliance with
the License. You may obtain a copy of the License at the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0 http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
==================================================================== */ ==================================================================== */
package org.apache.poi.xssf.usermodel.examples; package org.apache.poi.xssf.usermodel.examples;
import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.FileOutputStream;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.hssf.util.HSSFColor; import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import java.io.FileOutputStream; import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
/** import org.apache.poi.xssf.usermodel.IndexedColors;
* Fills and Colors import org.apache.poi.xssf.usermodel.XSSFRichTextString;
*/ import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class FillsAndColors {
public static void main(String[] args) throws Exception { /**
Workbook wb = new XSSFWorkbook(); * Fills and Colors
Sheet sheet = wb.createSheet("new sheet"); */
public class FillsAndColors {
// Create a row and put some cells in it. Rows are 0 based. public static void main(String[] args) throws Exception {
Row row = sheet.createRow((short) 1); Workbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet("new sheet");
// Aqua background
CellStyle style = wb.createCellStyle(); // Create a row and put some cells in it. Rows are 0 based.
style.setFillBackgroundColor(HSSFColor.AQUA.index); Row row = sheet.createRow((short) 1);
style.setFillPattern(CellStyle.BIG_SPOTS);
Cell cell = row.createCell((short) 1); // Aqua background
cell.setCellValue(new XSSFRichTextString("X")); CellStyle style = wb.createCellStyle();
cell.setCellStyle(style); style.setFillBackgroundColor(IndexedColors.AQUA.getIndex());
style.setFillPattern(CellStyle.BIG_SPOTS);
// Orange "foreground", foreground being the fill foreground not the font color. Cell cell = row.createCell((short) 1);
style = wb.createCellStyle(); cell.setCellValue(new XSSFRichTextString("X"));
style.setFillForegroundColor(HSSFColor.ORANGE.index); cell.setCellStyle(style);
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
cell = row.createCell((short) 2); // Orange "foreground", foreground being the fill foreground not the font color.
cell.setCellValue(new XSSFRichTextString("X")); style = wb.createCellStyle();
cell.setCellStyle(style); style.setFillForegroundColor(IndexedColors.ORANGE.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
// Write the output to a file cell = row.createCell((short) 2);
FileOutputStream fileOut = new FileOutputStream("fill_colors.xlsx"); cell.setCellValue(new XSSFRichTextString("X"));
wb.write(fileOut); cell.setCellStyle(style);
fileOut.close();
// Write the output to a file
} FileOutputStream fileOut = new FileOutputStream("fill_colors.xlsx");
} wb.write(fileOut);
fileOut.close();
}
}

View File

@ -57,7 +57,7 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.StyleSheetDocument;
/** /**
* Table of styles shared across all sheets in a workbook. * Table of styles shared across all sheets in a workbook.
* *
* @version $Id: SharedStringsTable.java 612495 2008-01-16 16:08:22Z ugo $ * @version $Id: SharedStringsTable.java 612495 2008-01-16 16:08:22Z ugo $
*/ */
public class StylesTable implements StylesSource, XSSFModel { public class StylesTable implements StylesSource, XSSFModel {
@ -67,20 +67,20 @@ public class StylesTable implements StylesSource, XSSFModel {
private final LinkedList<CTBorder> borders = new LinkedList<CTBorder>(); private final LinkedList<CTBorder> borders = new LinkedList<CTBorder>();
private final LinkedList<CTXf> styleXfs = new LinkedList<CTXf>(); private final LinkedList<CTXf> styleXfs = new LinkedList<CTXf>();
private final LinkedList<CTXf> xfs = new LinkedList<CTXf>(); private final LinkedList<CTXf> xfs = new LinkedList<CTXf>();
private final LinkedList<CTDxf> dxfs = new LinkedList<CTDxf>(); private final LinkedList<CTDxf> dxfs = new LinkedList<CTDxf>();
/** /**
* The first style id available for use as a custom style * The first style id available for use as a custom style
*/ */
public static final long FIRST_CUSTOM_STYLE_ID = 165; public static final long FIRST_CUSTOM_STYLE_ID = 165;
private StyleSheetDocument doc; private StyleSheetDocument doc;
/** /**
* Create a new StylesTable, by reading it from * Create a new StylesTable, by reading it from
* the InputStream of a a PackagePart. * the InputStream of a a PackagePart.
* *
* @param is The input stream containing the XML document. * @param is The input stream containing the XML document.
* @throws IOException if an error occurs while reading. * @throws IOException if an error occurs while reading.
*/ */
@ -99,7 +99,7 @@ public class StylesTable implements StylesSource, XSSFModel {
/** /**
* Read this shared styles table from an XML file. * Read this shared styles table from an XML file.
* *
* @param is The input stream containing the XML document. * @param is The input stream containing the XML document.
* @throws IOException if an error occurs while reading. * @throws IOException if an error occurs while reading.
*/ */
@ -145,7 +145,7 @@ public class StylesTable implements StylesSource, XSSFModel {
// =========================================================== // ===========================================================
// Start of style related getters and setters // Start of style related getters and setters
// =========================================================== // ===========================================================
public String getNumberFormatAt(long idx) { public String getNumberFormatAt(long idx) {
return numberFormats.get(idx); return numberFormats.get(idx);
} }
@ -160,7 +160,7 @@ public class StylesTable implements StylesSource, XSSFModel {
} }
throw new IllegalStateException("Found the format, but couldn't figure out where - should never happen!"); throw new IllegalStateException("Found the format, but couldn't figure out where - should never happen!");
} }
// Find a spare key, and add that // Find a spare key, and add that
long newKey = FIRST_CUSTOM_STYLE_ID; long newKey = FIRST_CUSTOM_STYLE_ID;
while(numberFormats.containsKey(newKey)) { while(numberFormats.containsKey(newKey)) {
@ -169,35 +169,35 @@ public class StylesTable implements StylesSource, XSSFModel {
numberFormats.put(newKey, fmt); numberFormats.put(newKey, fmt);
return newKey; return newKey;
} }
public Font getFontAt(long idx) { public Font getFontAt(long idx) {
return new XSSFFont(fonts.get((int) idx)); return new XSSFFont(fonts.get((int) idx));
} }
public synchronized long putFont(Font font) { public synchronized long putFont(Font font) {
return putFont((XSSFFont)font, fonts); return putFont((XSSFFont)font, fonts);
} }
public CellStyle getStyleAt(long idx) { public CellStyle getStyleAt(long idx) {
int styleXfId = 0; int styleXfId = 0;
// 0 is the empty default // 0 is the empty default
if(xfs.get((int) idx).getXfId() > 0) { if(xfs.get((int) idx).getXfId() > 0) {
styleXfId = (int) xfs.get((int) idx).getXfId(); styleXfId = (int) xfs.get((int) idx).getXfId();
} }
return new XSSFCellStyle((int) idx, styleXfId, this); return new XSSFCellStyle((int) idx, styleXfId, this);
} }
public synchronized long putStyle(CellStyle style) { public synchronized long putStyle(CellStyle style) {
XSSFCellStyle xStyle = (XSSFCellStyle)style; XSSFCellStyle xStyle = (XSSFCellStyle)style;
CTXf mainXF = xStyle.getCoreXf(); CTXf mainXF = xStyle.getCoreXf();
if(! xfs.contains(mainXF)) { if(! xfs.contains(mainXF)) {
xfs.add(mainXF); xfs.add(mainXF);
} }
return xfs.indexOf(mainXF); return xfs.indexOf(mainXF);
} }
public XSSFCellBorder getBorderAt(long idx) { public XSSFCellBorder getBorderAt(long idx) {
return new XSSFCellBorder(borders.get((int)idx)); return new XSSFCellBorder(borders.get((int)idx));
} }
@ -211,7 +211,7 @@ public class StylesTable implements StylesSource, XSSFModel {
public long putFill(XSSFCellFill fill) { public long putFill(XSSFCellFill fill) {
return putFill(fill, fills); return putFill(fill, fills);
} }
public CTXf getCellXfAt(long idx) { public CTXf getCellXfAt(long idx) {
return xfs.get((int) idx); return xfs.get((int) idx);
} }
@ -219,7 +219,7 @@ public class StylesTable implements StylesSource, XSSFModel {
xfs.add(cellXf); xfs.add(cellXf);
return xfs.size(); return xfs.size();
} }
public CTXf getCellStyleXfAt(long idx) { public CTXf getCellStyleXfAt(long idx) {
return styleXfs.get((int) idx); return styleXfs.get((int) idx);
} }
@ -238,7 +238,7 @@ public class StylesTable implements StylesSource, XSSFModel {
*/ */
public int getNumberOfFonts(){ public int getNumberOfFonts(){
return this.fonts.size(); return this.fonts.size();
} }
/** /**
* For unit testing only * For unit testing only
*/ */
@ -281,11 +281,11 @@ public class StylesTable implements StylesSource, XSSFModel {
public CTStylesheet _getRawStylesheet() { public CTStylesheet _getRawStylesheet() {
return doc.getStyleSheet(); return doc.getStyleSheet();
} }
/** /**
* Write this table out as XML. * Write this table out as XML.
* *
* @param out The stream to write to. * @param out The stream to write to.
* @throws IOException if an error occurs while writing. * @throws IOException if an error occurs while writing.
*/ */
@ -293,17 +293,17 @@ public class StylesTable implements StylesSource, XSSFModel {
XmlOptions options = new XmlOptions(); XmlOptions options = new XmlOptions();
options.setSaveOuter(); options.setSaveOuter();
options.setUseDefaultNamespace(); options.setUseDefaultNamespace();
// Requests use of whitespace for easier reading // Requests use of whitespace for easier reading
options.setSavePrettyPrint(); options.setSavePrettyPrint();
// Work on the current one // Work on the current one
// Need to do this, as we don't handle // Need to do this, as we don't handle
// all the possible entries yet // all the possible entries yet
// Formats // Formats
CTNumFmts formats = CTNumFmts.Factory.newInstance(); CTNumFmts formats = CTNumFmts.Factory.newInstance();
formats.setCount(numberFormats.size()); formats.setCount(numberFormats.size());
for (Entry<Long, String> fmt : numberFormats.entrySet()) { for (Entry<Long, String> fmt : numberFormats.entrySet()) {
CTNumFmt ctFmt = formats.addNewNumFmt(); CTNumFmt ctFmt = formats.addNewNumFmt();
@ -311,7 +311,7 @@ public class StylesTable implements StylesSource, XSSFModel {
ctFmt.setFormatCode(fmt.getValue()); ctFmt.setFormatCode(fmt.getValue());
} }
doc.getStyleSheet().setNumFmts(formats); doc.getStyleSheet().setNumFmts(formats);
// Fonts // Fonts
CTFonts ctFonts = CTFonts.Factory.newInstance(); CTFonts ctFonts = CTFonts.Factory.newInstance();
ctFonts.setCount(fonts.size()); ctFonts.setCount(fonts.size());
@ -319,19 +319,19 @@ public class StylesTable implements StylesSource, XSSFModel {
fonts.toArray(new CTFont[fonts.size()]) fonts.toArray(new CTFont[fonts.size()])
); );
doc.getStyleSheet().setFonts(ctFonts); doc.getStyleSheet().setFonts(ctFonts);
// Fills // Fills
CTFills ctFills = CTFills.Factory.newInstance(); CTFills ctFills = CTFills.Factory.newInstance();
ctFills.setCount(fills.size()); ctFills.setCount(fills.size());
ctFills.setFillArray(fills.toArray(new CTFill[fills.size()])); ctFills.setFillArray(fills.toArray(new CTFill[fills.size()]));
doc.getStyleSheet().setFills(ctFills); doc.getStyleSheet().setFills(ctFills);
// Borders // Borders
CTBorders ctBorders = CTBorders.Factory.newInstance(); CTBorders ctBorders = CTBorders.Factory.newInstance();
ctBorders.setCount(borders.size()); ctBorders.setCount(borders.size());
ctBorders.setBorderArray(borders.toArray(new CTBorder[borders.size()])); ctBorders.setBorderArray(borders.toArray(new CTBorder[borders.size()]));
doc.getStyleSheet().setBorders(ctBorders); doc.getStyleSheet().setBorders(ctBorders);
// Xfs // Xfs
if(xfs.size() > 0) { if(xfs.size() > 0) {
CTCellXfs ctXfs = CTCellXfs.Factory.newInstance(); CTCellXfs ctXfs = CTCellXfs.Factory.newInstance();
@ -341,7 +341,7 @@ public class StylesTable implements StylesSource, XSSFModel {
); );
doc.getStyleSheet().setCellXfs(ctXfs); doc.getStyleSheet().setCellXfs(ctXfs);
} }
// Style xfs // Style xfs
if(styleXfs.size() > 0) { if(styleXfs.size() > 0) {
CTCellStyleXfs ctSXfs = CTCellStyleXfs.Factory.newInstance(); CTCellStyleXfs ctSXfs = CTCellStyleXfs.Factory.newInstance();
@ -351,7 +351,7 @@ public class StylesTable implements StylesSource, XSSFModel {
); );
doc.getStyleSheet().setCellStyleXfs(ctSXfs); doc.getStyleSheet().setCellStyleXfs(ctSXfs);
} }
// Style dxfs // Style dxfs
if(dxfs.size() > 0) { if(dxfs.size() > 0) {
CTDxfs ctDxfs = CTDxfs.Factory.newInstance(); CTDxfs ctDxfs = CTDxfs.Factory.newInstance();
@ -360,31 +360,31 @@ public class StylesTable implements StylesSource, XSSFModel {
); );
doc.getStyleSheet().setDxfs(ctDxfs); doc.getStyleSheet().setDxfs(ctDxfs);
} }
// Save // Save
doc.save(out, options); doc.save(out, options);
} }
private long putBorder(XSSFCellBorder border, LinkedList<CTBorder> borders) { private long putBorder(XSSFCellBorder border, LinkedList<CTBorder> borders) {
return border.putBorder(borders); return border.putBorder(borders);
} }
private long putFill(XSSFCellFill fill, LinkedList<CTFill> fills) { private long putFill(XSSFCellFill fill, LinkedList<CTFill> fills) {
return fill.putFill(fills); return fill.putFill(fills);
} }
private long putFont(XSSFFont font, ArrayList<CTFont> fonts) { private long putFont(XSSFFont font, ArrayList<CTFont> fonts) {
return font.putFont(fonts); return font.putFont(fonts);
} }
private void initialize() { private void initialize() {
//CTFont ctFont = createDefaultFont();
XSSFFont xssfFont = createDefaultFont(); XSSFFont xssfFont = createDefaultFont();
fonts.add(xssfFont.getCTFont()); fonts.add(xssfFont.getCTFont());
CTFill[] ctFill = createDefaultFills(); CTFill[] ctFill = createDefaultFills();
fills.add(ctFill[0]); fills.add(ctFill[0]);
/* /*
fills.add(ctFill[1]); fills.add(ctFill[1]);
*/ */
CTBorder ctBorder = createDefaultBorder(); CTBorder ctBorder = createDefaultBorder();
@ -414,47 +414,37 @@ public class StylesTable implements StylesSource, XSSFModel {
ctBorder.addNewDiagonal(); ctBorder.addNewDiagonal();
return ctBorder; return ctBorder;
} }
private CTFill[] createDefaultFills() { private CTFill[] createDefaultFills() {
CTFill[] ctFill = new CTFill[]{CTFill.Factory.newInstance(),CTFill.Factory.newInstance()}; CTFill[] ctFill = new CTFill[]{CTFill.Factory.newInstance(),CTFill.Factory.newInstance()};
ctFill[0].addNewPatternFill().setPatternType(STPatternType.NONE); ctFill[0].addNewPatternFill().setPatternType(STPatternType.NONE);
ctFill[1].addNewPatternFill().setPatternType(STPatternType.DARK_GRAY); ctFill[1].addNewPatternFill().setPatternType(STPatternType.DARK_GRAY);
return ctFill; return ctFill;
} }
private XSSFFont createDefaultFont() { private XSSFFont createDefaultFont() {
/* CTFont ctFont = CTFont.Factory.newInstance();
CTFont ctFont = CTFont.Factory.newInstance(); XSSFFont xssfFont=new XSSFFont(ctFont);
ctFont.addNewSz().setVal(11); xssfFont.setFontHeightInPoints(XSSFFont.DEFAULT_FONT_SIZE);
ctFont.addNewColor().setTheme(1); xssfFont.setColor(XSSFFont.DEFAULT_FONT_COLOR);//setTheme
ctFont.addNewName().setVal("Calibri"); xssfFont.setFontName(XSSFFont.DEFAULT_FONT_NAME);
ctFont.addNewFamily().setVal(2); xssfFont.setFamily(FontFamily.SWISS);
ctFont.addNewScheme().setVal(STFontScheme.MINOR); xssfFont.setScheme(FontScheme.MINOR);
XSSFFont font=new XSSFFont(ctFont); return xssfFont;
return font;
*/
CTFont ctFont = CTFont.Factory.newInstance();
XSSFFont xssfFont=new XSSFFont(ctFont);
xssfFont.setFontHeightInPoints(XSSFFont.DEFAULT_FONT_SIZE);
xssfFont.setColor(XSSFFont.DEFAULT_FONT_COLOR);//setTheme
xssfFont.setFontName(XSSFFont.DEFAULT_FONT_NAME);
xssfFont.setFamily(FontFamily.SWISS);
xssfFont.setScheme(FontScheme.MINOR);
return xssfFont;
} }
public CTDxf getDxf(long idx) { public CTDxf getDxf(long idx) {
if(dxfs.size()==0) if(dxfs.size()==0)
return CTDxf.Factory.newInstance(); return CTDxf.Factory.newInstance();
else else
return dxfs.get((int) idx); return dxfs.get((int) idx);
} }
public long putDxf(CTDxf dxf) { public long putDxf(CTDxf dxf) {
this.dxfs.add(dxf); this.dxfs.add(dxf);
return this.dxfs.size(); return this.dxfs.size();
} }

View File

@ -17,7 +17,10 @@
package org.apache.poi.xssf.usermodel; package org.apache.poi.xssf.usermodel;
import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.StylesSource;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.model.StylesTable; import org.apache.poi.xssf.model.StylesTable;
import org.apache.poi.xssf.usermodel.extensions.XSSFCellAlignment; import org.apache.poi.xssf.usermodel.extensions.XSSFCellAlignment;
import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder; import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder;
@ -28,6 +31,7 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellAlignment;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellProtection; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellProtection;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTXf; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTXf;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STBorderStyle; import org.openxmlformats.schemas.spreadsheetml.x2006.main.STBorderStyle;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STPatternType;
public class XSSFCellStyle implements CellStyle { public class XSSFCellStyle implements CellStyle {
@ -74,13 +78,12 @@ public class XSSFCellStyle implements CellStyle {
*/ */
public XSSFCellStyle(StylesSource stylesSource) { public XSSFCellStyle(StylesSource stylesSource) {
this.stylesSource = stylesSource; this.stylesSource = stylesSource;
// We need a new CTXf for the main styles // We need a new CTXf for the main styles
// TODO decide on a style ctxf // TODO decide on a style ctxf
cellXf = CTXf.Factory.newInstance(); cellXf = CTXf.Factory.newInstance();
cellStyleXf = null; cellStyleXf = null;
} }
/** /**
* Verifies that this style belongs to the supplied Workbook * Verifies that this style belongs to the supplied Workbook
* Styles Source. * Styles Source.
@ -173,12 +176,65 @@ public class XSSFCellStyle implements CellStyle {
return (short) getCellFill().getFillBackgroundColor().getIndexed(); return (short) getCellFill().getFillBackgroundColor().getIndexed();
} }
public XSSFColor getFillBackgroundRgbColor() {
return getCellFill().getFillBackgroundColor();
}
public short getFillForegroundColor() { public short getFillForegroundColor() {
return (short) getCellFill().getFillForegroundColor().getIndexed(); return (short) getCellFill().getFillForegroundColor().getIndexed();
} }
public XSSFColor getFillForegroundRgbColor() {
return getCellFill().getFillForegroundColor();
}
public short getFillPattern() { public short getFillPattern() {
return (short) getCellFill().getPatternType().intValue(); int fp= getCellFill().getPatternType().intValue();
switch (fp) {
case STPatternType.INT_NONE:
return CellStyle.NO_FILL;
case STPatternType.INT_SOLID:
return CellStyle.SOLID_FOREGROUND;
case STPatternType.INT_LIGHT_GRAY:
return CellStyle.FINE_DOTS;
case STPatternType.INT_DARK_GRID:
return CellStyle.ALT_BARS;
case STPatternType.INT_DARK_GRAY:
return CellStyle.SPARSE_DOTS;
case STPatternType.INT_DARK_HORIZONTAL:
return CellStyle.THICK_HORZ_BANDS;
case STPatternType.INT_DARK_VERTICAL:
return CellStyle.THICK_VERT_BANDS;
case STPatternType.INT_DARK_UP:
return CellStyle.THICK_BACKWARD_DIAG;
case STPatternType.INT_DARK_DOWN:
return CellStyle.THICK_FORWARD_DIAG;
case STPatternType.INT_GRAY_0625:
return CellStyle.BIG_SPOTS;
case STPatternType.INT_DARK_TRELLIS:
return CellStyle.BRICKS;
case STPatternType.INT_LIGHT_HORIZONTAL:
return CellStyle.THIN_HORZ_BANDS;
case STPatternType.INT_LIGHT_VERTICAL:
return CellStyle.THIN_VERT_BANDS;
case STPatternType.INT_LIGHT_UP:
return CellStyle.THIN_BACKWARD_DIAG;
case STPatternType.INT_LIGHT_DOWN:
return CellStyle.THIN_FORWARD_DIAG;
case STPatternType.INT_LIGHT_GRID:
return CellStyle.SQUARES;
case STPatternType.INT_LIGHT_TRELLIS:
return CellStyle.DIAMONDS;
case STPatternType.INT_GRAY_125:
return CellStyle.LESS_DOTS;
/*
case STPatternType.INT_GRAY_0625:
return CellStyle.LEAST_DOTS;
*/
default:
return CellStyle.NO_FILL;
}
// return (short) getCellFill().getPatternType().intValue();
} }
public Font getFont(Workbook parentWorkbook) { public Font getFont(Workbook parentWorkbook) {
@ -288,17 +344,85 @@ public class XSSFCellStyle implements CellStyle {
cellXf.setNumFmtId((long)fmt); cellXf.setNumFmtId((long)fmt);
} }
public void setFillBackgroundRgbColor(XSSFColor color) {
cellFill=getCellFill();
cellFill.setFillBackgroundRgbColor(color);
}
public void setFillBackgroundColor(short bg) { public void setFillBackgroundColor(short bg) {
getCellFill().setFillBackgroundColor(bg); getCellFill().setFillBackgroundColor(bg);
} }
public void setFillForegroundRgbColor(XSSFColor color) {
getCellFill().setFillForegroundRgbColor(color);
}
public void setFillForegroundColor(short bg) { public void setFillForegroundColor(short bg) {
getCellFill().setFillForegroundColor(bg); getCellFill().setFillForegroundColor(bg);
} }
public void setFillPattern(short fp) { public void setFillPattern(short fp) {
// TODO Auto-generated method stub cellFill=getCellFill();
switch (fp) {
case CellStyle.NO_FILL:
cellFill.setPatternType(STPatternType.NONE);
break;
case CellStyle.SOLID_FOREGROUND:
cellFill.setPatternType(STPatternType.SOLID);
break;
case CellStyle.FINE_DOTS:
cellFill.setPatternType(STPatternType.LIGHT_GRAY);
break;
case CellStyle.ALT_BARS:
cellFill.setPatternType(STPatternType.DARK_GRID);
break;
case CellStyle.SPARSE_DOTS:
cellFill.setPatternType(STPatternType.DARK_GRAY);
break;
case CellStyle.THICK_HORZ_BANDS:
cellFill.setPatternType(STPatternType.DARK_HORIZONTAL);
break;
case CellStyle.THICK_VERT_BANDS:
cellFill.setPatternType(STPatternType.DARK_VERTICAL);
break;
case CellStyle.THICK_BACKWARD_DIAG:
cellFill.setPatternType(STPatternType.DARK_UP);
break;
case CellStyle.THICK_FORWARD_DIAG:
cellFill.setPatternType(STPatternType.DARK_DOWN);
break;
case CellStyle.BIG_SPOTS:
cellFill.setPatternType(STPatternType.GRAY_0625);
break;
case CellStyle.BRICKS:
cellFill.setPatternType(STPatternType.DARK_TRELLIS);
break;
case CellStyle.THIN_HORZ_BANDS:
cellFill.setPatternType(STPatternType.LIGHT_HORIZONTAL);
break;
case CellStyle.THIN_VERT_BANDS:
cellFill.setPatternType(STPatternType.LIGHT_VERTICAL);
break;
case CellStyle.THIN_BACKWARD_DIAG:
cellFill.setPatternType(STPatternType.LIGHT_UP);
break;
case CellStyle.THIN_FORWARD_DIAG:
cellFill.setPatternType(STPatternType.LIGHT_DOWN);
break;
case CellStyle.SQUARES:
cellFill.setPatternType(STPatternType.LIGHT_GRID);
break;
case CellStyle.DIAMONDS:
cellFill.setPatternType(STPatternType.LIGHT_TRELLIS);
break;
case CellStyle.LESS_DOTS:
cellFill.setPatternType(STPatternType.GRAY_125);
break;
case CellStyle.LEAST_DOTS:
cellFill.setPatternType(STPatternType.GRAY_0625);
break;
default: throw new RuntimeException("Fill type ["+fp+"] not accepted");
}
} }
public void setFont(Font font) { public void setFont(Font font) {
@ -306,6 +430,7 @@ public class XSSFCellStyle implements CellStyle {
long index=this.stylesSource.putFont(font); long index=this.stylesSource.putFont(font);
this.cellXf.setFontId(index); this.cellXf.setFontId(index);
} }
this.cellXf.setApplyFont(true);
} }
public void setHidden(boolean hidden) { public void setHidden(boolean hidden) {
@ -320,10 +445,17 @@ public class XSSFCellStyle implements CellStyle {
setBorderColorIndexed(BorderSide.LEFT, color); setBorderColorIndexed(BorderSide.LEFT, color);
} }
private void setBorderColorIndexed(BorderSide side, XSSFColor color) {
this.cellBorder.setBorderColor(side, color);
}
public void setLocked(boolean locked) { public void setLocked(boolean locked) {
getCellProtection().setLocked(locked); getCellProtection().setLocked(locked);
} }
public void setRightBorderColor(short color) { public void setRightBorderColor(short color) {
setBorderColorIndexed(BorderSide.RIGHT, color); setBorderColorIndexed(BorderSide.RIGHT, color);
} }
@ -355,34 +487,54 @@ public class XSSFCellStyle implements CellStyle {
public void setBorderColor(BorderSide side, XSSFColor color) { public void setBorderColor(BorderSide side, XSSFColor color) {
getCellBorder().setBorderColor(side, color); getCellBorder().setBorderColor(side, color);
} }
private XSSFCellBorder getCellBorder() { private XSSFCellBorder getCellBorder() {
if (cellBorder == null) { if (cellBorder == null) {
// TODO make a common Cell Border object // TODO make a common Cell Border object
cellBorder = ((StylesTable)stylesSource).getBorderAt(getBorderId()); int borderId=getBorderId();
if(borderId==-1){
cellBorder=new XSSFCellBorder();
long index=((StylesTable)stylesSource).putBorder(cellBorder);
this.cellXf.setBorderId(index);
this.cellXf.setApplyBorder(true);
}
else{
cellBorder = ((StylesTable)stylesSource).getBorderAt(borderId);
}
} }
return cellBorder; return cellBorder;
} }
private int getBorderId() { private int getBorderId() {
if (cellXf.isSetBorderId()) { if (cellXf.isSetBorderId() && cellXf.getBorderId()>0) {
return (int) cellXf.getBorderId(); return (int) cellXf.getBorderId();
} }
return (int) cellStyleXf.getBorderId(); return -1;
// return (int) cellStyleXf.getBorderId();
} }
private XSSFCellFill getCellFill() { private XSSFCellFill getCellFill() {
if (cellFill == null) { if (cellFill == null) {
cellFill = ((StylesTable)stylesSource).getFillAt(getFillId()); int fillId=getFillId();
} if(fillId == -1) {
return cellFill; cellFill=new XSSFCellFill();
long index=((StylesTable)stylesSource).putFill(cellFill);
this.cellXf.setFillId(index);
this.cellXf.setApplyFill(true);
}
else{
cellFill=((StylesTable)stylesSource).getFillAt(fillId);
}
}
return cellFill;
} }
private int getFillId() { private int getFillId() {
if (cellXf.isSetFillId()) { if (cellXf.isSetFillId() && cellXf.getFillId()>0) {
return (int) cellXf.getFillId(); return (int) cellXf.getFillId();
} }
return (int) cellStyleXf.getFillId(); //return (int) cellStyleXf.getFillId();
return -1;
} }
private int getFontId() { private int getFontId() {

View File

@ -18,8 +18,8 @@
package org.apache.poi.xssf.usermodel; package org.apache.poi.xssf.usermodel;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
@ -31,7 +31,6 @@ import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CommentsSource; import org.apache.poi.ss.usermodel.CommentsSource;
import org.apache.poi.ss.usermodel.CreationHelper; import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.DataFormat; import org.apache.poi.ss.usermodel.DataFormat;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Palette; import org.apache.poi.ss.usermodel.Palette;
import org.apache.poi.ss.usermodel.PictureData; import org.apache.poi.ss.usermodel.PictureData;
import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Row;
@ -68,6 +67,7 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDialogsheet;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbook; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbook;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTXf;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.WorkbookDocument; import org.openxmlformats.schemas.spreadsheetml.x2006.main.WorkbookDocument;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.WorksheetDocument; import org.openxmlformats.schemas.spreadsheetml.x2006.main.WorksheetDocument;
@ -250,16 +250,33 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
return null; return null;
} }
public XSSFCellStyle createCellStyle() {
CTXf xf=CTXf.Factory.newInstance();
xf.setNumFmtId(0);
xf.setFontId(0);
xf.setFillId(0);
xf.setBorderId(0);
xf.setXfId(0);
int xfSize=((StylesTable)stylesSource)._getStyleXfsSize();
long indexXf=((StylesTable)stylesSource).putCellXf(xf);
XSSFCellStyle style = new XSSFCellStyle(new Long(indexXf-1).intValue(), xfSize-1, (StylesTable)stylesSource);
return style;
}
/*
public XSSFCellStyle createCellStyle() { public XSSFCellStyle createCellStyle() {
return new XSSFCellStyle(stylesSource); return new XSSFCellStyle(stylesSource);
} }
*/
public DataFormat createDataFormat() { public DataFormat createDataFormat() {
return getCreationHelper().createDataFormat(); return getCreationHelper().createDataFormat();
} }
public XSSFFont createFont() { public XSSFFont createFont() {
return new XSSFFont(); XSSFFont font= new XSSFFont();
stylesSource.putFont(font);
return font;
} }
public XSSFName createName() { public XSSFName createName() {
@ -308,7 +325,8 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
short fontNum=getNumberOfFonts(); short fontNum=getNumberOfFonts();
for (short i = 0; i < fontNum; i++) { for (short i = 0; i < fontNum; i++) {
XSSFFont xssfFont = getFontAt(i); XSSFFont xssfFont = getFontAt(i);
if ( xssfFont.getBold() == (boldWeight == XSSFFont.BOLDWEIGHT_BOLD)
if ( (xssfFont.getBold() == (boldWeight == XSSFFont.BOLDWEIGHT_BOLD))
&& xssfFont.getColor() == color && xssfFont.getColor() == color
&& xssfFont.getFontHeightInPoints() == fontHeight && xssfFont.getFontHeightInPoints() == fontHeight
&& xssfFont.getFontName().equals(name) && xssfFont.getFontName().equals(name)
@ -413,7 +431,6 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
} }
public short getNumberOfFonts() { public short getNumberOfFonts() {
// TODO Auto-generated method stub
return (short)((StylesTable)stylesSource).getNumberOfFonts(); return (short)((StylesTable)stylesSource).getNumberOfFonts();
} }
@ -472,9 +489,9 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
public String getSheetName(int sheet) { public String getSheetName(int sheet) {
return this.workbook.getSheets().getSheetArray(sheet).getName(); return this.workbook.getSheets().getSheetArray(sheet).getName();
} }
/** /**
* Are we a normal workbook (.xlsx), or a * Are we a normal workbook (.xlsx), or a
* macro enabled workbook (.xlsm)? * macro enabled workbook (.xlsm)?
*/ */
public boolean isMacroEnabled() { public boolean isMacroEnabled() {
@ -518,14 +535,14 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
/** /**
* Sets the policy on what to do when * Sets the policy on what to do when
* getting missing or blank cells from a row. * getting missing or blank cells from a row.
* This will then apply to all calls to * This will then apply to all calls to
* {@link Row.getCell()}. See * {@link Row.getCell()}. See
* {@link MissingCellPolicy} * {@link MissingCellPolicy}
*/ */
public void setMissingCellPolicy(MissingCellPolicy missingCellPolicy) { public void setMissingCellPolicy(MissingCellPolicy missingCellPolicy) {
this.missingCellPolicy = missingCellPolicy; this.missingCellPolicy = missingCellPolicy;
} }
public void setBackupFlag(boolean backupValue) { public void setBackupFlag(boolean backupValue) {
// TODO Auto-generated method stub // TODO Auto-generated method stub
@ -543,7 +560,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
} }
/** /**
* deprecated Aug 2008 * deprecated Aug 2008
* @deprecated - Misleading name - use setFirstVisibleTab() * @deprecated - Misleading name - use setFirstVisibleTab()
*/ */
public void setDisplayedTab(short index) { public void setDisplayedTab(short index) {
setFirstVisibleTab(index); setFirstVisibleTab(index);
@ -616,7 +633,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
xmlOptions.setSavePrettyPrint(); xmlOptions.setSavePrettyPrint();
xmlOptions.setSaveOuter(); xmlOptions.setSaveOuter();
xmlOptions.setUseDefaultNamespace(); xmlOptions.setUseDefaultNamespace();
// Write out our sheets, updating the references // Write out our sheets, updating the references
// to them in the main workbook as we go // to them in the main workbook as we go
for (int i=0 ; i < this.getNumberOfSheets(); i++) { for (int i=0 ; i < this.getNumberOfSheets(); i++) {
@ -627,21 +644,21 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
PackageRelationship rel = PackageRelationship rel =
corePart.addRelationship(partName, TargetMode.INTERNAL, XSSFRelation.WORKSHEET.getRelation(), "rSheet" + sheetNumber); corePart.addRelationship(partName, TargetMode.INTERNAL, XSSFRelation.WORKSHEET.getRelation(), "rSheet" + sheetNumber);
PackagePart part = pkg.createPart(partName, XSSFRelation.WORKSHEET.getContentType()); PackagePart part = pkg.createPart(partName, XSSFRelation.WORKSHEET.getContentType());
// XXX This should not be needed, but apparently the setSaveOuter call above does not work in XMLBeans 2.2 // XXX This should not be needed, but apparently the setSaveOuter call above does not work in XMLBeans 2.2
xmlOptions.setSaveSyntheticDocumentElement(new QName(CTWorksheet.type.getName().getNamespaceURI(), "worksheet")); xmlOptions.setSaveSyntheticDocumentElement(new QName(CTWorksheet.type.getName().getNamespaceURI(), "worksheet"));
sheet.save(part, xmlOptions); sheet.save(part, xmlOptions);
// Update our internal reference for the package part // Update our internal reference for the package part
workbook.getSheets().getSheetArray(i).setId(rel.getId()); workbook.getSheets().getSheetArray(i).setId(rel.getId());
workbook.getSheets().getSheetArray(i).setSheetId(sheetNumber); workbook.getSheets().getSheetArray(i).setSheetId(sheetNumber);
// If our sheet has comments, then write out those // If our sheet has comments, then write out those
if(sheet.hasComments()) { if(sheet.hasComments()) {
CommentsTable ct = (CommentsTable)sheet.getCommentsSourceIfExists(); CommentsTable ct = (CommentsTable)sheet.getCommentsSourceIfExists();
XSSFRelation.SHEET_COMMENTS.save(ct, part, sheetNumber); XSSFRelation.SHEET_COMMENTS.save(ct, part, sheetNumber);
} }
// If our sheet has drawings, then write out those // If our sheet has drawings, then write out those
if(sheet.getDrawings() != null) { if(sheet.getDrawings() != null) {
int drawingIndex = 1; int drawingIndex = 1;
@ -654,7 +671,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
drawingIndex++; drawingIndex++;
} }
} }
// If our sheet has controls, then write out those // If our sheet has controls, then write out those
if(sheet.getControls() != null) { if(sheet.getControls() != null) {
int controlIndex = 1; int controlIndex = 1;
@ -668,7 +685,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
} }
} }
} }
// Write shared strings and styles // Write shared strings and styles
if(sharedStringSource != null) { if(sharedStringSource != null) {
SharedStringsTable sst = (SharedStringsTable)sharedStringSource; SharedStringsTable sst = (SharedStringsTable)sharedStringSource;
@ -678,7 +695,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
StylesTable st = (StylesTable)stylesSource; StylesTable st = (StylesTable)stylesSource;
XSSFRelation.STYLES.save(st, corePart); XSSFRelation.STYLES.save(st, corePart);
} }
// Named ranges // Named ranges
if(namedRanges.size() > 0) { if(namedRanges.size() > 0) {
CTDefinedNames names = CTDefinedNames.Factory.newInstance(); CTDefinedNames names = CTDefinedNames.Factory.newInstance();
@ -693,7 +710,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
workbook.setDefinedNames(null); workbook.setDefinedNames(null);
} }
} }
// Macro related bits // Macro related bits
if(isMacroEnabled) { if(isMacroEnabled) {
// Copy VBA Macros if present // Copy VBA Macros if present
@ -714,7 +731,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
xmlOptions.setSaveSyntheticDocumentElement(new QName(CTWorkbook.type.getName().getNamespaceURI(), "workbook")); xmlOptions.setSaveSyntheticDocumentElement(new QName(CTWorkbook.type.getName().getNamespaceURI(), "workbook"));
workbook.save(out, xmlOptions); workbook.save(out, xmlOptions);
out.close(); out.close();
// All done // All done
pkg.close(); pkg.close();
} catch (InvalidFormatException e) { } catch (InvalidFormatException e) {

View File

@ -1,80 +1,95 @@
/* ==================================================================== /* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership. this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0 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 not use this file except in compliance with
the License. You may obtain a copy of the License at the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0 http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
==================================================================== */ ==================================================================== */
package org.apache.poi.xssf.usermodel.extensions; package org.apache.poi.xssf.usermodel.extensions;
import java.util.LinkedList; import java.util.LinkedList;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFill; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFill;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPatternFill; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPatternFill;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STPatternType.Enum; import org.openxmlformats.schemas.spreadsheetml.x2006.main.STPatternType.Enum;
public class XSSFCellFill { public class XSSFCellFill {
private CTFill fill; private CTFill fill;
public XSSFCellFill(CTFill fill) { public XSSFCellFill(CTFill fill) {
this.fill = fill; this.fill = fill;
} }
public XSSFCellFill() { public XSSFCellFill() {
this.fill = CTFill.Factory.newInstance(); this.fill = CTFill.Factory.newInstance();
} }
public XSSFColor getFillBackgroundColor() { public XSSFColor getFillBackgroundColor() {
return new XSSFColor(getPatternFill().getBgColor()); return new XSSFColor(getPatternFill().getBgColor());
} }
public XSSFColor getFillForegroundColor() { public XSSFColor getFillForegroundColor() {
return new XSSFColor(getPatternFill().getFgColor()); return new XSSFColor(getPatternFill().getFgColor());
} }
public Enum getPatternType() { public Enum getPatternType() {
return getPatternFill().getPatternType(); return getPatternFill().getPatternType();
} }
public long putFill(LinkedList<CTFill> fills) { public long putFill(LinkedList<CTFill> fills) {
if (fills.contains(fill)) { if (fills.contains(fill)) {
return fills.indexOf(fill); return fills.indexOf(fill);
} }
fills.add(fill); fills.add(fill);
return fills.size() - 1; return fills.size() - 1;
} }
private CTPatternFill getPatternFill() { private CTPatternFill getPatternFill() {
CTPatternFill patternFill = fill.getPatternFill(); CTPatternFill patternFill = fill.getPatternFill();
if (patternFill == null) { if (patternFill == null) {
patternFill = fill.addNewPatternFill(); patternFill = fill.addNewPatternFill();
} }
return patternFill; return patternFill;
} }
public CTFill getCTFill() { public CTFill getCTFill() {
return this.fill; return this.fill;
} }
public void setFillBackgroundColor(long index) {
CTColor ctColor=fill.getPatternFill().addNewBgColor(); public void setFillBackgroundColor(long index) {
ctColor.setIndexed(index); CTColor ctColor=getPatternFill().addNewBgColor();
fill.getPatternFill().setBgColor(ctColor); ctColor.setIndexed(index);
} fill.getPatternFill().setBgColor(ctColor);
}
public void setFillForegroundColor(long index) {
CTColor ctColor=fill.getPatternFill().addNewFgColor(); public void setFillForegroundColor(long index) {
ctColor.setIndexed(index); CTColor ctColor=getPatternFill().addNewFgColor();
fill.getPatternFill().setFgColor(ctColor); ctColor.setIndexed(index);
} fill.getPatternFill().setFgColor(ctColor);
} }
public void setFillBackgroundRgbColor(XSSFColor color) {
fill.getPatternFill().setBgColor(color.getCTColor());
}
public void setFillForegroundRgbColor(XSSFColor color) {
fill.getPatternFill().setFgColor(color.getCTColor());
}
public void setPatternType(Enum patternType) {
getPatternFill().setPatternType(patternType);
}
}

View File

@ -19,6 +19,7 @@ package org.apache.poi.xssf.usermodel;
import junit.framework.TestCase; import junit.framework.TestCase;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.xssf.model.StylesTable; import org.apache.poi.xssf.model.StylesTable;
import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder; import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder;
import org.apache.poi.xssf.usermodel.extensions.XSSFCellFill; import org.apache.poi.xssf.usermodel.extensions.XSSFCellFill;
@ -41,9 +42,9 @@ public class TestXSSFCellStyle extends TestCase {
public void setUp() { public void setUp() {
stylesTable = new StylesTable(); stylesTable = new StylesTable();
ctStylesheet = stylesTable._getRawStylesheet(); ctStylesheet = stylesTable._getRawStylesheet();
// Until we do XSSFBorder properly, cheat // Until we do XSSFBorder properly, cheat
ctBorderA = CTBorder.Factory.newInstance(); ctBorderA = CTBorder.Factory.newInstance();
XSSFCellBorder borderA = new XSSFCellBorder(ctBorderA); XSSFCellBorder borderA = new XSSFCellBorder(ctBorderA);
@ -56,7 +57,7 @@ public class TestXSSFCellStyle extends TestCase {
ctFill = CTFill.Factory.newInstance(); ctFill = CTFill.Factory.newInstance();
XSSFCellFill fill = new XSSFCellFill(ctFill); XSSFCellFill fill = new XSSFCellFill(ctFill);
long fillId = stylesTable.putFill(fill); long fillId = stylesTable.putFill(fill);
assertEquals(1, fillId); assertEquals(2, fillId);
ctFont = CTFont.Factory.newInstance(); ctFont = CTFont.Factory.newInstance();
XSSFFont font = new XSSFFont(ctFont); XSSFFont font = new XSSFFont(ctFont);
@ -67,11 +68,15 @@ public class TestXSSFCellStyle extends TestCase {
cellStyleXf.setBorderId(1); cellStyleXf.setBorderId(1);
cellStyleXf.setFillId(1); cellStyleXf.setFillId(1);
cellStyleXf.setFontId(1); cellStyleXf.setFontId(1);
cellXfs = ctStylesheet.addNewCellXfs(); cellXfs = ctStylesheet.addNewCellXfs();
cellXf = cellXfs.addNewXf(); cellXf = cellXfs.addNewXf();
cellXf.setXfId(1); cellXf.setXfId(1);
cellXf.setBorderId(1);
cellXf.setFillId(1);
cellXf.setFontId(1);
stylesTable.putCellStyleXf(cellStyleXf); stylesTable.putCellStyleXf(cellStyleXf);
stylesTable.putCellXf(cellXf); long id=stylesTable.putCellXf(cellXf);
cellStyle = new XSSFCellStyle(1, 1, stylesTable); cellStyle = new XSSFCellStyle(1, 1, stylesTable);
} }
@ -192,23 +197,77 @@ public class TestXSSFCellStyle extends TestCase {
} }
public void testGetFillBackgroundColor() { public void testGetFillBackgroundColor() {
CTPatternFill ctPatternFill = ctFill.addNewPatternFill(); setUp();
CTPatternFill ctPatternFill = ctFill.addNewPatternFill();
CTColor ctBgColor = ctPatternFill.addNewBgColor(); CTColor ctBgColor = ctPatternFill.addNewBgColor();
ctBgColor.setIndexed(4); ctBgColor.setIndexed(IndexedColors.BRIGHT_GREEN.getIndex());
assertEquals(4, cellStyle.getFillBackgroundColor()); ctPatternFill.setBgColor(ctBgColor);
XSSFCellFill cellFill=new XSSFCellFill(ctFill);
long index=stylesTable.putFill(cellFill);
((XSSFCellStyle)cellStyle).getCoreXf().setFillId(index);
assertEquals(2,cellStyle.getCoreXf().getFillId());
assertEquals(IndexedColors.BRIGHT_GREEN.getIndex(), cellStyle.getFillBackgroundColor());
cellStyle.setFillBackgroundColor(IndexedColors.BLUE.getIndex());
assertEquals(IndexedColors.BLUE.getIndex(), ctFill.getPatternFill().getBgColor().getIndexed());
//test rgb color - XSSFColor
CTColor ctColor=CTColor.Factory.newInstance();
ctColor.setRgb("FFFFFF".getBytes());
ctPatternFill.setBgColor(ctColor);
assertEquals(ctColor.toString(), cellStyle.getFillBackgroundRgbColor().getCTColor().toString());
cellStyle.setFillBackgroundRgbColor(new XSSFColor(ctColor));
assertEquals(ctColor.getRgb()[0], ctPatternFill.getBgColor().getRgb()[0]);
assertEquals(ctColor.getRgb()[1], ctPatternFill.getBgColor().getRgb()[1]);
assertEquals(ctColor.getRgb()[2], ctPatternFill.getBgColor().getRgb()[2]);
assertEquals(ctColor.getRgb()[3], ctPatternFill.getBgColor().getRgb()[3]);
} }
public void testGetFillForegroundColor() { public void testGetFillForegroundColor() {
CTPatternFill ctPatternFill = ctFill.addNewPatternFill(); setUp();
CTPatternFill ctPatternFill = ctFill.addNewPatternFill();
CTColor ctFgColor = ctPatternFill.addNewFgColor(); CTColor ctFgColor = ctPatternFill.addNewFgColor();
ctFgColor.setIndexed(5); ctFgColor.setIndexed(IndexedColors.BRIGHT_GREEN.getIndex());
assertEquals(5, cellStyle.getFillForegroundColor()); ctPatternFill.setFgColor(ctFgColor);
XSSFCellFill cellFill=new XSSFCellFill(ctFill);
long index=stylesTable.putFill(cellFill);
((XSSFCellStyle)cellStyle).getCoreXf().setFillId(index);
assertEquals(2,cellStyle.getCoreXf().getFillId());
assertEquals(IndexedColors.BRIGHT_GREEN.getIndex(), cellStyle.getFillForegroundColor());
cellStyle.setFillForegroundColor(IndexedColors.BLUE.getIndex());
assertEquals(IndexedColors.BLUE.getIndex(), ctFill.getPatternFill().getFgColor().getIndexed());
//test rgb color - XSSFColor
CTColor ctColor=CTColor.Factory.newInstance();
ctColor.setRgb("FFFFFF".getBytes());
ctPatternFill.setFgColor(ctColor);
assertEquals(ctColor.toString(), cellStyle.getFillForegroundRgbColor().getCTColor().toString());
cellStyle.setFillForegroundRgbColor(new XSSFColor(ctColor));
assertEquals(ctColor.getRgb()[0], ctPatternFill.getFgColor().getRgb()[0]);
assertEquals(ctColor.getRgb()[1], ctPatternFill.getFgColor().getRgb()[1]);
assertEquals(ctColor.getRgb()[2], ctPatternFill.getFgColor().getRgb()[2]);
assertEquals(ctColor.getRgb()[3], ctPatternFill.getFgColor().getRgb()[3]);
} }
public void testGetFillPattern() { public void testGetFillPattern() {
setUp();
CTPatternFill ctPatternFill = ctFill.addNewPatternFill(); CTPatternFill ctPatternFill = ctFill.addNewPatternFill();
ctPatternFill.setPatternType(STPatternType.DARK_DOWN); ctPatternFill.setPatternType(STPatternType.DARK_DOWN);
assertEquals(8, cellStyle.getFillPattern()); XSSFCellFill cellFill=new XSSFCellFill(ctFill);
long index=stylesTable.putFill(cellFill);
((XSSFCellStyle)cellStyle).getCoreXf().setFillId(index);
assertEquals(CellStyle.THICK_FORWARD_DIAG, cellStyle.getFillPattern());
cellStyle.setFillPattern(CellStyle.BRICKS);
assertEquals(STPatternType.INT_DARK_TRELLIS,ctPatternFill.getPatternType().intValue());
} }
public void testGetFont() { public void testGetFont() {