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
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.examples;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import java.io.FileOutputStream;
/**
* Fills and Colors
*/
public class FillsAndColors {
public static void main(String[] args) throws Exception {
Workbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet("new sheet");
// Create a row and put some cells in it. Rows are 0 based.
Row row = sheet.createRow((short) 1);
// Aqua background
CellStyle style = wb.createCellStyle();
style.setFillBackgroundColor(HSSFColor.AQUA.index);
style.setFillPattern(CellStyle.BIG_SPOTS);
Cell cell = row.createCell((short) 1);
cell.setCellValue(new XSSFRichTextString("X"));
cell.setCellStyle(style);
// Orange "foreground", foreground being the fill foreground not the font color.
style = wb.createCellStyle();
style.setFillForegroundColor(HSSFColor.ORANGE.index);
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
cell = row.createCell((short) 2);
cell.setCellValue(new XSSFRichTextString("X"));
cell.setCellStyle(style);
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("fill_colors.xlsx");
wb.write(fileOut);
fileOut.close();
}
}
/* ====================================================================
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.examples;
import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.IndexedColors;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
/**
* Fills and Colors
*/
public class FillsAndColors {
public static void main(String[] args) throws Exception {
Workbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet("new sheet");
// Create a row and put some cells in it. Rows are 0 based.
Row row = sheet.createRow((short) 1);
// Aqua background
CellStyle style = wb.createCellStyle();
style.setFillBackgroundColor(IndexedColors.AQUA.getIndex());
style.setFillPattern(CellStyle.BIG_SPOTS);
Cell cell = row.createCell((short) 1);
cell.setCellValue(new XSSFRichTextString("X"));
cell.setCellStyle(style);
// Orange "foreground", foreground being the fill foreground not the font color.
style = wb.createCellStyle();
style.setFillForegroundColor(IndexedColors.ORANGE.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
cell = row.createCell((short) 2);
cell.setCellValue(new XSSFRichTextString("X"));
cell.setCellStyle(style);
// 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.
*
*
* @version $Id: SharedStringsTable.java 612495 2008-01-16 16:08:22Z ugo $
*/
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<CTXf> styleXfs = new LinkedList<CTXf>();
private final LinkedList<CTXf> xfs = new LinkedList<CTXf>();
private final LinkedList<CTDxf> dxfs = new LinkedList<CTDxf>();
/**
* The first style id available for use as a custom style
*/
public static final long FIRST_CUSTOM_STYLE_ID = 165;
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.
*
*
* @param is The input stream containing the XML document.
* @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.
*
*
* @param is The input stream containing the XML document.
* @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
// ===========================================================
public String getNumberFormatAt(long 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!");
}
// Find a spare key, and add that
long newKey = FIRST_CUSTOM_STYLE_ID;
while(numberFormats.containsKey(newKey)) {
@ -169,35 +169,35 @@ public class StylesTable implements StylesSource, XSSFModel {
numberFormats.put(newKey, fmt);
return newKey;
}
public Font getFontAt(long idx) {
return new XSSFFont(fonts.get((int) idx));
}
public synchronized long putFont(Font font) {
return putFont((XSSFFont)font, fonts);
}
public CellStyle getStyleAt(long idx) {
int styleXfId = 0;
// 0 is the empty default
if(xfs.get((int) idx).getXfId() > 0) {
styleXfId = (int) xfs.get((int) idx).getXfId();
}
return new XSSFCellStyle((int) idx, styleXfId, this);
}
public synchronized long putStyle(CellStyle style) {
XSSFCellStyle xStyle = (XSSFCellStyle)style;
CTXf mainXF = xStyle.getCoreXf();
if(! xfs.contains(mainXF)) {
xfs.add(mainXF);
}
return xfs.indexOf(mainXF);
}
public XSSFCellBorder getBorderAt(long idx) {
return new XSSFCellBorder(borders.get((int)idx));
}
@ -211,7 +211,7 @@ public class StylesTable implements StylesSource, XSSFModel {
public long putFill(XSSFCellFill fill) {
return putFill(fill, fills);
}
public CTXf getCellXfAt(long idx) {
return xfs.get((int) idx);
}
@ -219,7 +219,7 @@ public class StylesTable implements StylesSource, XSSFModel {
xfs.add(cellXf);
return xfs.size();
}
public CTXf getCellStyleXfAt(long idx) {
return styleXfs.get((int) idx);
}
@ -238,7 +238,7 @@ public class StylesTable implements StylesSource, XSSFModel {
*/
public int getNumberOfFonts(){
return this.fonts.size();
}
}
/**
* For unit testing only
*/
@ -281,11 +281,11 @@ public class StylesTable implements StylesSource, XSSFModel {
public CTStylesheet _getRawStylesheet() {
return doc.getStyleSheet();
}
/**
* Write this table out as XML.
*
*
* @param out The stream to write to.
* @throws IOException if an error occurs while writing.
*/
@ -293,17 +293,17 @@ public class StylesTable implements StylesSource, XSSFModel {
XmlOptions options = new XmlOptions();
options.setSaveOuter();
options.setUseDefaultNamespace();
// Requests use of whitespace for easier reading
options.setSavePrettyPrint();
// Work on the current one
// Need to do this, as we don't handle
// all the possible entries yet
// Formats
CTNumFmts formats = CTNumFmts.Factory.newInstance();
CTNumFmts formats = CTNumFmts.Factory.newInstance();
formats.setCount(numberFormats.size());
for (Entry<Long, String> fmt : numberFormats.entrySet()) {
CTNumFmt ctFmt = formats.addNewNumFmt();
@ -311,7 +311,7 @@ public class StylesTable implements StylesSource, XSSFModel {
ctFmt.setFormatCode(fmt.getValue());
}
doc.getStyleSheet().setNumFmts(formats);
// Fonts
CTFonts ctFonts = CTFonts.Factory.newInstance();
ctFonts.setCount(fonts.size());
@ -319,19 +319,19 @@ public class StylesTable implements StylesSource, XSSFModel {
fonts.toArray(new CTFont[fonts.size()])
);
doc.getStyleSheet().setFonts(ctFonts);
// Fills
CTFills ctFills = CTFills.Factory.newInstance();
ctFills.setCount(fills.size());
ctFills.setFillArray(fills.toArray(new CTFill[fills.size()]));
doc.getStyleSheet().setFills(ctFills);
// Borders
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) {
CTCellXfs ctXfs = CTCellXfs.Factory.newInstance();
@ -341,7 +341,7 @@ public class StylesTable implements StylesSource, XSSFModel {
);
doc.getStyleSheet().setCellXfs(ctXfs);
}
// Style xfs
if(styleXfs.size() > 0) {
CTCellStyleXfs ctSXfs = CTCellStyleXfs.Factory.newInstance();
@ -351,7 +351,7 @@ public class StylesTable implements StylesSource, XSSFModel {
);
doc.getStyleSheet().setCellStyleXfs(ctSXfs);
}
// Style dxfs
if(dxfs.size() > 0) {
CTDxfs ctDxfs = CTDxfs.Factory.newInstance();
@ -360,31 +360,31 @@ public class StylesTable implements StylesSource, XSSFModel {
);
doc.getStyleSheet().setDxfs(ctDxfs);
}
// Save
doc.save(out, options);
}
private long putBorder(XSSFCellBorder border, LinkedList<CTBorder> borders) {
return border.putBorder(borders);
}
private long putFill(XSSFCellFill fill, LinkedList<CTFill> fills) {
return fill.putFill(fills);
}
private long putFont(XSSFFont font, ArrayList<CTFont> fonts) {
return font.putFont(fonts);
private long putFont(XSSFFont font, ArrayList<CTFont> fonts) {
return font.putFont(fonts);
}
private void initialize() {
//CTFont ctFont = createDefaultFont();
XSSFFont xssfFont = createDefaultFont();
fonts.add(xssfFont.getCTFont());
CTFill[] ctFill = createDefaultFills();
fills.add(ctFill[0]);
/*
/*
fills.add(ctFill[1]);
*/
CTBorder ctBorder = createDefaultBorder();
@ -414,47 +414,37 @@ public class StylesTable implements StylesSource, XSSFModel {
ctBorder.addNewDiagonal();
return ctBorder;
}
private CTFill[] createDefaultFills() {
CTFill[] ctFill = new CTFill[]{CTFill.Factory.newInstance(),CTFill.Factory.newInstance()};
ctFill[0].addNewPatternFill().setPatternType(STPatternType.NONE);
ctFill[1].addNewPatternFill().setPatternType(STPatternType.DARK_GRAY);
return ctFill;
}
private XSSFFont 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);
XSSFFont font=new XSSFFont(ctFont);
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;
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) {
if(dxfs.size()==0)
return CTDxf.Factory.newInstance();
else
return dxfs.get((int) idx);
}
public long putDxf(CTDxf dxf) {
this.dxfs.add(dxf);
return this.dxfs.size();
this.dxfs.add(dxf);
return this.dxfs.size();
}

View File

@ -17,7 +17,10 @@
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.usermodel.extensions.XSSFCellAlignment;
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.CTXf;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STBorderStyle;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STPatternType;
public class XSSFCellStyle implements CellStyle {
@ -74,13 +78,12 @@ public class XSSFCellStyle implements CellStyle {
*/
public XSSFCellStyle(StylesSource stylesSource) {
this.stylesSource = stylesSource;
// We need a new CTXf for the main styles
// TODO decide on a style ctxf
cellXf = CTXf.Factory.newInstance();
cellStyleXf = null;
}
/**
* Verifies that this style belongs to the supplied Workbook
* Styles Source.
@ -173,12 +176,65 @@ public class XSSFCellStyle implements CellStyle {
return (short) getCellFill().getFillBackgroundColor().getIndexed();
}
public XSSFColor getFillBackgroundRgbColor() {
return getCellFill().getFillBackgroundColor();
}
public short getFillForegroundColor() {
return (short) getCellFill().getFillForegroundColor().getIndexed();
}
public XSSFColor getFillForegroundRgbColor() {
return getCellFill().getFillForegroundColor();
}
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) {
@ -288,17 +344,85 @@ public class XSSFCellStyle implements CellStyle {
cellXf.setNumFmtId((long)fmt);
}
public void setFillBackgroundRgbColor(XSSFColor color) {
cellFill=getCellFill();
cellFill.setFillBackgroundRgbColor(color);
}
public void setFillBackgroundColor(short bg) {
getCellFill().setFillBackgroundColor(bg);
}
public void setFillForegroundRgbColor(XSSFColor color) {
getCellFill().setFillForegroundRgbColor(color);
}
public void setFillForegroundColor(short bg) {
getCellFill().setFillForegroundColor(bg);
getCellFill().setFillForegroundColor(bg);
}
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) {
@ -306,6 +430,7 @@ public class XSSFCellStyle implements CellStyle {
long index=this.stylesSource.putFont(font);
this.cellXf.setFontId(index);
}
this.cellXf.setApplyFont(true);
}
public void setHidden(boolean hidden) {
@ -320,10 +445,17 @@ public class XSSFCellStyle implements CellStyle {
setBorderColorIndexed(BorderSide.LEFT, color);
}
private void setBorderColorIndexed(BorderSide side, XSSFColor color) {
this.cellBorder.setBorderColor(side, color);
}
public void setLocked(boolean locked) {
getCellProtection().setLocked(locked);
}
public void setRightBorderColor(short color) {
setBorderColorIndexed(BorderSide.RIGHT, color);
}
@ -355,34 +487,54 @@ public class XSSFCellStyle implements CellStyle {
public void setBorderColor(BorderSide side, XSSFColor color) {
getCellBorder().setBorderColor(side, color);
}
private XSSFCellBorder getCellBorder() {
if (cellBorder == null) {
// 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;
}
private int getBorderId() {
if (cellXf.isSetBorderId()) {
if (cellXf.isSetBorderId() && cellXf.getBorderId()>0) {
return (int) cellXf.getBorderId();
}
return (int) cellStyleXf.getBorderId();
return -1;
// return (int) cellStyleXf.getBorderId();
}
private XSSFCellFill getCellFill() {
if (cellFill == null) {
cellFill = ((StylesTable)stylesSource).getFillAt(getFillId());
}
return cellFill;
if (cellFill == null) {
int fillId=getFillId();
if(fillId == -1) {
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() {
if (cellXf.isSetFillId()) {
if (cellXf.isSetFillId() && cellXf.getFillId()>0) {
return (int) cellXf.getFillId();
}
return (int) cellStyleXf.getFillId();
//return (int) cellStyleXf.getFillId();
return -1;
}
private int getFontId() {

View File

@ -18,8 +18,8 @@
package org.apache.poi.xssf.usermodel;
import java.io.IOException;
import java.io.OutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.LinkedList;
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.CreationHelper;
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.PictureData;
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.CTWorkbook;
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.WorksheetDocument;
@ -250,16 +250,33 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
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() {
return new XSSFCellStyle(stylesSource);
}
*/
public DataFormat createDataFormat() {
return getCreationHelper().createDataFormat();
}
public XSSFFont createFont() {
return new XSSFFont();
XSSFFont font= new XSSFFont();
stylesSource.putFont(font);
return font;
}
public XSSFName createName() {
@ -308,7 +325,8 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
short fontNum=getNumberOfFonts();
for (short i = 0; i < fontNum; i++) {
XSSFFont xssfFont = getFontAt(i);
if ( xssfFont.getBold() == (boldWeight == XSSFFont.BOLDWEIGHT_BOLD)
if ( (xssfFont.getBold() == (boldWeight == XSSFFont.BOLDWEIGHT_BOLD))
&& xssfFont.getColor() == color
&& xssfFont.getFontHeightInPoints() == fontHeight
&& xssfFont.getFontName().equals(name)
@ -413,7 +431,6 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
}
public short getNumberOfFonts() {
// TODO Auto-generated method stub
return (short)((StylesTable)stylesSource).getNumberOfFonts();
}
@ -472,9 +489,9 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
public String getSheetName(int sheet) {
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)?
*/
public boolean isMacroEnabled() {
@ -518,14 +535,14 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
/**
* Sets the policy on what to do when
* 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 MissingCellPolicy}
*/
public void setMissingCellPolicy(MissingCellPolicy missingCellPolicy) {
this.missingCellPolicy = missingCellPolicy;
}
public void setBackupFlag(boolean backupValue) {
// TODO Auto-generated method stub
@ -543,7 +560,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
}
/**
* deprecated Aug 2008
* @deprecated - Misleading name - use setFirstVisibleTab()
* @deprecated - Misleading name - use setFirstVisibleTab()
*/
public void setDisplayedTab(short index) {
setFirstVisibleTab(index);
@ -616,7 +633,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
xmlOptions.setSavePrettyPrint();
xmlOptions.setSaveOuter();
xmlOptions.setUseDefaultNamespace();
// Write out our sheets, updating the references
// to them in the main workbook as we go
for (int i=0 ; i < this.getNumberOfSheets(); i++) {
@ -627,21 +644,21 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
PackageRelationship rel =
corePart.addRelationship(partName, TargetMode.INTERNAL, XSSFRelation.WORKSHEET.getRelation(), "rSheet" + sheetNumber);
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
xmlOptions.setSaveSyntheticDocumentElement(new QName(CTWorksheet.type.getName().getNamespaceURI(), "worksheet"));
sheet.save(part, xmlOptions);
// Update our internal reference for the package part
workbook.getSheets().getSheetArray(i).setId(rel.getId());
workbook.getSheets().getSheetArray(i).setSheetId(sheetNumber);
// If our sheet has comments, then write out those
if(sheet.hasComments()) {
CommentsTable ct = (CommentsTable)sheet.getCommentsSourceIfExists();
XSSFRelation.SHEET_COMMENTS.save(ct, part, sheetNumber);
}
// If our sheet has drawings, then write out those
if(sheet.getDrawings() != null) {
int drawingIndex = 1;
@ -654,7 +671,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
drawingIndex++;
}
}
// If our sheet has controls, then write out those
if(sheet.getControls() != null) {
int controlIndex = 1;
@ -668,7 +685,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
}
}
}
// Write shared strings and styles
if(sharedStringSource != null) {
SharedStringsTable sst = (SharedStringsTable)sharedStringSource;
@ -678,7 +695,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
StylesTable st = (StylesTable)stylesSource;
XSSFRelation.STYLES.save(st, corePart);
}
// Named ranges
if(namedRanges.size() > 0) {
CTDefinedNames names = CTDefinedNames.Factory.newInstance();
@ -693,7 +710,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
workbook.setDefinedNames(null);
}
}
// Macro related bits
if(isMacroEnabled) {
// 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"));
workbook.save(out, xmlOptions);
out.close();
// All done
pkg.close();
} catch (InvalidFormatException e) {

View File

@ -1,80 +1,95 @@
/* ====================================================================
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;
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.STPatternType.Enum;
public class XSSFCellFill {
private CTFill fill;
public XSSFCellFill(CTFill fill) {
this.fill = fill;
}
public XSSFCellFill() {
this.fill = CTFill.Factory.newInstance();
}
public XSSFColor getFillBackgroundColor() {
return new XSSFColor(getPatternFill().getBgColor());
}
public XSSFColor getFillForegroundColor() {
return new XSSFColor(getPatternFill().getFgColor());
}
public Enum getPatternType() {
return getPatternFill().getPatternType();
}
public long putFill(LinkedList<CTFill> fills) {
if (fills.contains(fill)) {
return fills.indexOf(fill);
}
fills.add(fill);
return fills.size() - 1;
}
private CTPatternFill getPatternFill() {
CTPatternFill patternFill = fill.getPatternFill();
if (patternFill == null) {
patternFill = fill.addNewPatternFill();
}
return patternFill;
}
public CTFill getCTFill() {
return this.fill;
}
public void setFillBackgroundColor(long index) {
CTColor ctColor=fill.getPatternFill().addNewBgColor();
ctColor.setIndexed(index);
fill.getPatternFill().setBgColor(ctColor);
}
public void setFillForegroundColor(long index) {
CTColor ctColor=fill.getPatternFill().addNewFgColor();
ctColor.setIndexed(index);
fill.getPatternFill().setFgColor(ctColor);
}
}
/* ====================================================================
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;
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.STPatternType.Enum;
public class XSSFCellFill {
private CTFill fill;
public XSSFCellFill(CTFill fill) {
this.fill = fill;
}
public XSSFCellFill() {
this.fill = CTFill.Factory.newInstance();
}
public XSSFColor getFillBackgroundColor() {
return new XSSFColor(getPatternFill().getBgColor());
}
public XSSFColor getFillForegroundColor() {
return new XSSFColor(getPatternFill().getFgColor());
}
public Enum getPatternType() {
return getPatternFill().getPatternType();
}
public long putFill(LinkedList<CTFill> fills) {
if (fills.contains(fill)) {
return fills.indexOf(fill);
}
fills.add(fill);
return fills.size() - 1;
}
private CTPatternFill getPatternFill() {
CTPatternFill patternFill = fill.getPatternFill();
if (patternFill == null) {
patternFill = fill.addNewPatternFill();
}
return patternFill;
}
public CTFill getCTFill() {
return this.fill;
}
public void setFillBackgroundColor(long index) {
CTColor ctColor=getPatternFill().addNewBgColor();
ctColor.setIndexed(index);
fill.getPatternFill().setBgColor(ctColor);
}
public void setFillForegroundColor(long index) {
CTColor ctColor=getPatternFill().addNewFgColor();
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 org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.xssf.model.StylesTable;
import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder;
import org.apache.poi.xssf.usermodel.extensions.XSSFCellFill;
@ -41,9 +42,9 @@ public class TestXSSFCellStyle extends TestCase {
public void setUp() {
stylesTable = new StylesTable();
ctStylesheet = stylesTable._getRawStylesheet();
// Until we do XSSFBorder properly, cheat
ctBorderA = CTBorder.Factory.newInstance();
XSSFCellBorder borderA = new XSSFCellBorder(ctBorderA);
@ -56,7 +57,7 @@ public class TestXSSFCellStyle extends TestCase {
ctFill = CTFill.Factory.newInstance();
XSSFCellFill fill = new XSSFCellFill(ctFill);
long fillId = stylesTable.putFill(fill);
assertEquals(1, fillId);
assertEquals(2, fillId);
ctFont = CTFont.Factory.newInstance();
XSSFFont font = new XSSFFont(ctFont);
@ -67,11 +68,15 @@ public class TestXSSFCellStyle extends TestCase {
cellStyleXf.setBorderId(1);
cellStyleXf.setFillId(1);
cellStyleXf.setFontId(1);
cellXfs = ctStylesheet.addNewCellXfs();
cellXf = cellXfs.addNewXf();
cellXf.setXfId(1);
cellXf.setBorderId(1);
cellXf.setFillId(1);
cellXf.setFontId(1);
stylesTable.putCellStyleXf(cellStyleXf);
stylesTable.putCellXf(cellXf);
long id=stylesTable.putCellXf(cellXf);
cellStyle = new XSSFCellStyle(1, 1, stylesTable);
}
@ -192,23 +197,77 @@ public class TestXSSFCellStyle extends TestCase {
}
public void testGetFillBackgroundColor() {
CTPatternFill ctPatternFill = ctFill.addNewPatternFill();
setUp();
CTPatternFill ctPatternFill = ctFill.addNewPatternFill();
CTColor ctBgColor = ctPatternFill.addNewBgColor();
ctBgColor.setIndexed(4);
assertEquals(4, cellStyle.getFillBackgroundColor());
ctBgColor.setIndexed(IndexedColors.BRIGHT_GREEN.getIndex());
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() {
CTPatternFill ctPatternFill = ctFill.addNewPatternFill();
setUp();
CTPatternFill ctPatternFill = ctFill.addNewPatternFill();
CTColor ctFgColor = ctPatternFill.addNewFgColor();
ctFgColor.setIndexed(5);
assertEquals(5, cellStyle.getFillForegroundColor());
ctFgColor.setIndexed(IndexedColors.BRIGHT_GREEN.getIndex());
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() {
setUp();
CTPatternFill ctPatternFill = ctFill.addNewPatternFill();
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() {