-/* ====================================================================\r
- Licensed to the Apache Software Foundation (ASF) under one or more\r
- contributor license agreements. See the NOTICE file distributed with\r
- this work for additional information regarding copyright ownership.\r
- The ASF licenses this file to You under the Apache License, Version 2.0\r
- (the "License"); you may not use this file except in compliance with\r
- the License. You may obtain a copy of the License at\r
-\r
- http://www.apache.org/licenses/LICENSE-2.0\r
-\r
- Unless required by applicable law or agreed to in writing, software\r
- distributed under the License is distributed on an "AS IS" BASIS,\r
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- See the License for the specific language governing permissions and\r
- limitations under the License.\r
-==================================================================== */\r
-package org.apache.poi.xssf.usermodel.examples;\r
-\r
-import org.apache.poi.xssf.usermodel.XSSFWorkbook;\r
-import org.apache.poi.xssf.usermodel.XSSFRichTextString;\r
-import org.apache.poi.ss.usermodel.*;\r
-import org.apache.poi.hssf.util.HSSFColor;\r
-\r
-import java.io.FileOutputStream;\r
-\r
-/**\r
- * Fills and Colors\r
- */\r
-public class FillsAndColors {\r
- public static void main(String[] args) throws Exception {\r
- Workbook wb = new XSSFWorkbook();\r
- Sheet sheet = wb.createSheet("new sheet");\r
-\r
- // Create a row and put some cells in it. Rows are 0 based.\r
- Row row = sheet.createRow((short) 1);\r
-\r
- // Aqua background\r
- CellStyle style = wb.createCellStyle();\r
- style.setFillBackgroundColor(HSSFColor.AQUA.index);\r
- style.setFillPattern(CellStyle.BIG_SPOTS);\r
- Cell cell = row.createCell((short) 1);\r
- cell.setCellValue(new XSSFRichTextString("X"));\r
- cell.setCellStyle(style);\r
-\r
- // Orange "foreground", foreground being the fill foreground not the font color.\r
- style = wb.createCellStyle();\r
- style.setFillForegroundColor(HSSFColor.ORANGE.index);\r
- style.setFillPattern(CellStyle.SOLID_FOREGROUND);\r
- cell = row.createCell((short) 2);\r
- cell.setCellValue(new XSSFRichTextString("X"));\r
- cell.setCellStyle(style);\r
-\r
- // Write the output to a file\r
- FileOutputStream fileOut = new FileOutputStream("fill_colors.xlsx");\r
- wb.write(fileOut);\r
- fileOut.close();\r
-\r
- }\r
-}\r
+/* ====================================================================
+ 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();
+
+ }
+}
/**
* 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 {
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.
*/
/**
* 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.
*/
// ===========================================================
// Start of style related getters and setters
// ===========================================================
-
+
public String getNumberFormatAt(long idx) {
return numberFormats.get(idx);
}
}
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)) {
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));
}
public long putFill(XSSFCellFill fill) {
return putFill(fill, fills);
}
-
+
public CTXf getCellXfAt(long idx) {
return xfs.get((int) idx);
}
xfs.add(cellXf);
return xfs.size();
}
-
+
public CTXf getCellStyleXfAt(long idx) {
return styleXfs.get((int) idx);
}
*/
public int getNumberOfFonts(){
return this.fonts.size();
- }
+ }
/**
* For unit testing only
*/
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.
*/
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();
ctFmt.setFormatCode(fmt.getValue());
}
doc.getStyleSheet().setNumFmts(formats);
-
+
// Fonts
CTFonts ctFonts = CTFonts.Factory.newInstance();
ctFonts.setCount(fonts.size());
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();
);
doc.getStyleSheet().setCellXfs(ctXfs);
}
-
+
// Style xfs
if(styleXfs.size() > 0) {
CTCellStyleXfs ctSXfs = CTCellStyleXfs.Factory.newInstance();
);
doc.getStyleSheet().setCellStyleXfs(ctSXfs);
}
-
+
// Style dxfs
if(dxfs.size() > 0) {
CTDxfs ctDxfs = CTDxfs.Factory.newInstance();
);
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();
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();
}
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;
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 {
*/
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.
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) {
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) {
long index=this.stylesSource.putFont(font);
this.cellXf.setFontId(index);
}
+ this.cellXf.setApplyFont(true);
}
public void setHidden(boolean hidden) {
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);
}
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() {
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;
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;
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;
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() {
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)
}
public short getNumberOfFonts() {
- // TODO Auto-generated method stub
return (short)((StylesTable)stylesSource).getNumberOfFonts();
}
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() {
/**
* 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
}
/**
* deprecated Aug 2008
- * @deprecated - Misleading name - use setFirstVisibleTab()
+ * @deprecated - Misleading name - use setFirstVisibleTab()
*/
public void setDisplayedTab(short index) {
setFirstVisibleTab(index);
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++) {
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;
drawingIndex++;
}
}
-
+
// If our sheet has controls, then write out those
if(sheet.getControls() != null) {
int controlIndex = 1;
}
}
}
-
+
// Write shared strings and styles
if(sharedStringSource != null) {
SharedStringsTable sst = (SharedStringsTable)sharedStringSource;
StylesTable st = (StylesTable)stylesSource;
XSSFRelation.STYLES.save(st, corePart);
}
-
+
// Named ranges
if(namedRanges.size() > 0) {
CTDefinedNames names = CTDefinedNames.Factory.newInstance();
workbook.setDefinedNames(null);
}
}
-
+
// Macro related bits
if(isMacroEnabled) {
// Copy VBA Macros if present
xmlOptions.setSaveSyntheticDocumentElement(new QName(CTWorkbook.type.getName().getNamespaceURI(), "workbook"));
workbook.save(out, xmlOptions);
out.close();
-
+
// All done
pkg.close();
} catch (InvalidFormatException e) {
-/* ====================================================================\r
- Licensed to the Apache Software Foundation (ASF) under one or more\r
- contributor license agreements. See the NOTICE file distributed with\r
- this work for additional information regarding copyright ownership.\r
- The ASF licenses this file to You under the Apache License, Version 2.0\r
- (the "License"); you may not use this file except in compliance with\r
- the License. You may obtain a copy of the License at\r
-\r
- http://www.apache.org/licenses/LICENSE-2.0\r
-\r
- Unless required by applicable law or agreed to in writing, software\r
- distributed under the License is distributed on an "AS IS" BASIS,\r
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- See the License for the specific language governing permissions and\r
- limitations under the License.\r
-==================================================================== */\r
-package org.apache.poi.xssf.usermodel.extensions;\r
-\r
-import java.util.LinkedList;\r
-\r
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor;\r
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFill;\r
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPatternFill;\r
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.STPatternType.Enum;\r
-\r
-public class XSSFCellFill {\r
- \r
- private CTFill fill;\r
- \r
- public XSSFCellFill(CTFill fill) {\r
- this.fill = fill;\r
- }\r
- \r
- public XSSFCellFill() {\r
- this.fill = CTFill.Factory.newInstance();\r
- }\r
- \r
- public XSSFColor getFillBackgroundColor() {\r
- return new XSSFColor(getPatternFill().getBgColor());\r
- }\r
-\r
- public XSSFColor getFillForegroundColor() {\r
- return new XSSFColor(getPatternFill().getFgColor());\r
- }\r
-\r
- public Enum getPatternType() {\r
- return getPatternFill().getPatternType();\r
- }\r
- \r
- public long putFill(LinkedList<CTFill> fills) {\r
- if (fills.contains(fill)) {\r
- return fills.indexOf(fill);\r
- }\r
- fills.add(fill);\r
- return fills.size() - 1;\r
- }\r
-\r
- private CTPatternFill getPatternFill() {\r
- CTPatternFill patternFill = fill.getPatternFill();\r
- if (patternFill == null) {\r
- patternFill = fill.addNewPatternFill();\r
- }\r
- return patternFill;\r
- }\r
-\r
- public CTFill getCTFill() {\r
- return this.fill;\r
- }\r
- public void setFillBackgroundColor(long index) {\r
- CTColor ctColor=fill.getPatternFill().addNewBgColor();\r
- ctColor.setIndexed(index);\r
- fill.getPatternFill().setBgColor(ctColor);\r
- }\r
-\r
- public void setFillForegroundColor(long index) {\r
- CTColor ctColor=fill.getPatternFill().addNewFgColor();\r
- ctColor.setIndexed(index);\r
- fill.getPatternFill().setFgColor(ctColor);\r
- }\r
-}\r
+/* ====================================================================
+ 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);
+ }
+
+
+}
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;
public void setUp() {
stylesTable = new StylesTable();
-
+
ctStylesheet = stylesTable._getRawStylesheet();
-
+
// Until we do XSSFBorder properly, cheat
ctBorderA = CTBorder.Factory.newInstance();
XSSFCellBorder borderA = new XSSFCellBorder(ctBorderA);
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);
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);
}
}
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() {