Return-Path: Delivered-To: apmail-poi-commits-archive@locus.apache.org Received: (qmail 60936 invoked from network); 24 Sep 2008 17:54:21 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 24 Sep 2008 17:54:21 -0000 Received: (qmail 47551 invoked by uid 500); 24 Sep 2008 17:54:19 -0000 Delivered-To: apmail-poi-commits-archive@poi.apache.org Received: (qmail 47520 invoked by uid 500); 24 Sep 2008 17:54:19 -0000 Mailing-List: contact commits-help@poi.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@poi.apache.org Delivered-To: mailing list commits@poi.apache.org Received: (qmail 47511 invoked by uid 99); 24 Sep 2008 17:54:19 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 24 Sep 2008 10:54:19 -0700 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 24 Sep 2008 17:53:26 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 10238238889C; Wed, 24 Sep 2008 10:53:30 -0700 (PDT) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r698674 - in /poi/branches/ooxml/src: examples/src/org/apache/poi/xssf/usermodel/examples/ ooxml/java/org/apache/poi/xssf/model/ ooxml/java/org/apache/poi/xssf/usermodel/ ooxml/java/org/apache/poi/xssf/usermodel/extensions/ ooxml/testcases/... Date: Wed, 24 Sep 2008 17:53:29 -0000 To: commits@poi.apache.org From: yegor@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20080924175330.10238238889C@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: yegor Date: Wed Sep 24 10:53:29 2008 New Revision: 698674 URL: http://svn.apache.org/viewvc?rev=698674&view=rev Log: patch #45881 from Sourcesense: implemented cell fills and borders, added examples Modified: poi/branches/ooxml/src/examples/src/org/apache/poi/xssf/usermodel/examples/FillsAndColors.java poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/extensions/XSSFCellFill.java poi/branches/ooxml/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCellStyle.java Modified: poi/branches/ooxml/src/examples/src/org/apache/poi/xssf/usermodel/examples/FillsAndColors.java URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/examples/src/org/apache/poi/xssf/usermodel/examples/FillsAndColors.java?rev=698674&r1=698673&r2=698674&view=diff ============================================================================== --- poi/branches/ooxml/src/examples/src/org/apache/poi/xssf/usermodel/examples/FillsAndColors.java (original) +++ poi/branches/ooxml/src/examples/src/org/apache/poi/xssf/usermodel/examples/FillsAndColors.java Wed Sep 24 10:53:29 2008 @@ -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(); + + } +} Modified: poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java?rev=698674&r1=698673&r2=698674&view=diff ============================================================================== --- poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java (original) +++ poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java Wed Sep 24 10:53:29 2008 @@ -57,7 +57,7 @@ /** * 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 @@ private final LinkedList borders = new LinkedList(); private final LinkedList styleXfs = new LinkedList(); private final LinkedList xfs = new LinkedList(); - + private final LinkedList dxfs = new LinkedList(); - + /** * 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 @@ /** * 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 @@ // =========================================================== // Start of style related getters and setters // =========================================================== - + public String getNumberFormatAt(long idx) { return numberFormats.get(idx); } @@ -160,7 +160,7 @@ } 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 @@ 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 long putFill(XSSFCellFill fill) { return putFill(fill, fills); } - + public CTXf getCellXfAt(long idx) { return xfs.get((int) idx); } @@ -219,7 +219,7 @@ xfs.add(cellXf); return xfs.size(); } - + public CTXf getCellStyleXfAt(long idx) { return styleXfs.get((int) idx); } @@ -238,7 +238,7 @@ */ public int getNumberOfFonts(){ return this.fonts.size(); - } + } /** * For unit testing only */ @@ -281,11 +281,11 @@ 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 @@ 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 fmt : numberFormats.entrySet()) { CTNumFmt ctFmt = formats.addNewNumFmt(); @@ -311,7 +311,7 @@ ctFmt.setFormatCode(fmt.getValue()); } doc.getStyleSheet().setNumFmts(formats); - + // Fonts CTFonts ctFonts = CTFonts.Factory.newInstance(); ctFonts.setCount(fonts.size()); @@ -319,19 +319,19 @@ 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 @@ ); doc.getStyleSheet().setCellXfs(ctXfs); } - + // Style xfs if(styleXfs.size() > 0) { CTCellStyleXfs ctSXfs = CTCellStyleXfs.Factory.newInstance(); @@ -351,7 +351,7 @@ ); doc.getStyleSheet().setCellStyleXfs(ctSXfs); } - + // Style dxfs if(dxfs.size() > 0) { CTDxfs ctDxfs = CTDxfs.Factory.newInstance(); @@ -360,31 +360,31 @@ ); doc.getStyleSheet().setDxfs(ctDxfs); } - + // Save doc.save(out, options); } - + private long putBorder(XSSFCellBorder border, LinkedList borders) { return border.putBorder(borders); } - + private long putFill(XSSFCellFill fill, LinkedList fills) { return fill.putFill(fills); } - private long putFont(XSSFFont font, ArrayList fonts) { - return font.putFont(fonts); + private long putFont(XSSFFont font, ArrayList 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 @@ 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(); } Modified: poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java?rev=698674&r1=698673&r2=698674&view=diff ============================================================================== --- poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java (original) +++ poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java Wed Sep 24 10:53:29 2008 @@ -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.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 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 @@ 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 @@ 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 @@ long index=this.stylesSource.putFont(font); this.cellXf.setFontId(index); } + this.cellXf.setApplyFont(true); } public void setHidden(boolean hidden) { @@ -320,10 +445,17 @@ 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 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() { Modified: poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java?rev=698674&r1=698673&r2=698674&view=diff ============================================================================== --- poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java (original) +++ poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java Wed Sep 24 10:53:29 2008 @@ -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.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.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; @@ -251,15 +251,32 @@ } 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 @@ 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 short getNumberOfFonts() { - // TODO Auto-generated method stub return (short)((StylesTable)stylesSource).getNumberOfFonts(); } @@ -472,9 +489,9 @@ 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 @@ /** * 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 @@ } /** * deprecated Aug 2008 - * @deprecated - Misleading name - use setFirstVisibleTab() + * @deprecated - Misleading name - use setFirstVisibleTab() */ public void setDisplayedTab(short index) { setFirstVisibleTab(index); @@ -616,7 +633,7 @@ 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 @@ 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 @@ drawingIndex++; } } - + // If our sheet has controls, then write out those if(sheet.getControls() != null) { int controlIndex = 1; @@ -668,7 +685,7 @@ } } } - + // Write shared strings and styles if(sharedStringSource != null) { SharedStringsTable sst = (SharedStringsTable)sharedStringSource; @@ -678,7 +695,7 @@ StylesTable st = (StylesTable)stylesSource; XSSFRelation.STYLES.save(st, corePart); } - + // Named ranges if(namedRanges.size() > 0) { CTDefinedNames names = CTDefinedNames.Factory.newInstance(); @@ -693,7 +710,7 @@ workbook.setDefinedNames(null); } } - + // Macro related bits if(isMacroEnabled) { // Copy VBA Macros if present @@ -714,7 +731,7 @@ xmlOptions.setSaveSyntheticDocumentElement(new QName(CTWorkbook.type.getName().getNamespaceURI(), "workbook")); workbook.save(out, xmlOptions); out.close(); - + // All done pkg.close(); } catch (InvalidFormatException e) { Modified: poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/extensions/XSSFCellFill.java URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/extensions/XSSFCellFill.java?rev=698674&r1=698673&r2=698674&view=diff ============================================================================== --- poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/extensions/XSSFCellFill.java (original) +++ poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/extensions/XSSFCellFill.java Wed Sep 24 10:53:29 2008 @@ -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 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 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); + } + + +} Modified: poi/branches/ooxml/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCellStyle.java URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCellStyle.java?rev=698674&r1=698673&r2=698674&view=diff ============================================================================== --- poi/branches/ooxml/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCellStyle.java (original) +++ poi/branches/ooxml/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCellStyle.java Wed Sep 24 10:53:29 2008 @@ -19,6 +19,7 @@ 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 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 @@ 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 @@ 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 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() { --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org For additional commands, e-mail: commits-help@poi.apache.org