From: Yegor Kozlov Date: Thu, 18 Sep 2008 07:42:40 +0000 (+0000) Subject: 1. initial support for rich text in xssf" X-Git-Tag: REL_3_5_BETA3~8 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=eed31d47c1b3723e6747992ec4b255740d6d4e75;p=poi.git 1. initial support for rich text in xssf" 2. cleaned common ooxml-ole2 interfaces, removed ole2-specific stuff 3. added new examples from the quick guide 4. misc xssf refactoring, tending to use enums intstead of final static constants 5. refactored XSSFFont 6. included test-ooxml and jar-ooxml in the dist target, they are part of release and should be there git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@696584 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/build.xml b/build.xml index d2472c2dc3..b9c94f171c 100644 --- a/build.xml +++ b/build.xml @@ -555,8 +555,8 @@ under the License. - + @@ -1206,7 +1206,7 @@ FORREST_HOME environment variable! - + diff --git a/src/examples/src/org/apache/poi/xssf/usermodel/examples/CreateCell.java b/src/examples/src/org/apache/poi/xssf/usermodel/examples/CreateCell.java new file mode 100755 index 0000000000..3e49aa2f05 --- /dev/null +++ b/src/examples/src/org/apache/poi/xssf/usermodel/examples/CreateCell.java @@ -0,0 +1,76 @@ +/* ==================================================================== + 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 java.util.Date; + +import org.apache.poi.ss.usermodel.*; +import org.apache.poi.xssf.usermodel.XSSFRichTextString; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; + +/** + * + */ +public class CreateCell { + + + public static void main(String[]args) throws Exception { + Workbook wb = new XSSFWorkbook(); + CreationHelper creationHelper = wb.getCreationHelper(); + Sheet sheet = wb.createSheet("new sheet"); + + // Create a row and put some cells in it. Rows are 0 based. + Row row = sheet.createRow((short)0); + // Create a cell and put a value in it. + Cell cell = row.createCell((short)0); + cell.setCellValue(1); + + //numeric value + row.createCell(1).setCellValue(1.2); + + //plain string value + row.createCell(2).setCellValue("This is a string cell"); + + //rich text string + RichTextString str = creationHelper.createRichTextString("Apache"); + Font font = wb.createFont(); + font.setItalic(true); + font.setUnderline(Font.U_SINGLE); + str.applyFont(font); + row.createCell(3).setCellValue(str); + + //boolean value + row.createCell(4).setCellValue(true); + + //formula + row.createCell(5).setCellFormula("SUM(A1:B1)"); + + //date + CellStyle style = wb.createCellStyle(); + style.setDataFormat(creationHelper.createDataFormat().getFormat("m/d/yy h:mm")); + cell = row.createCell(6); + cell.setCellValue(new Date()); + cell.setCellStyle(style); + + // Write the output to a file + FileOutputStream fileOut = new FileOutputStream("ooxml-cell.xlsx"); + wb.write(fileOut); + fileOut.close(); + } +} diff --git a/src/examples/src/org/apache/poi/xssf/usermodel/examples/CreateNewSpreadsheet.java b/src/examples/src/org/apache/poi/xssf/usermodel/examples/CreateNewSpreadsheet.java index afef752715..62664f2dca 100644 --- a/src/examples/src/org/apache/poi/xssf/usermodel/examples/CreateNewSpreadsheet.java +++ b/src/examples/src/org/apache/poi/xssf/usermodel/examples/CreateNewSpreadsheet.java @@ -46,7 +46,7 @@ public class CreateNewSpreadsheet { s2.getRow(2).createCell(1).setCellValue(createHelper.createRichTextString("Sheet 2")); - +/* // Comment Comment comment = ((XSSFSheet)s1).createComment(); // HSSFPatriarch patriach = (HSSFPatriarch)s1.createDrawingPatriarch(); @@ -62,7 +62,7 @@ public class CreateNewSpreadsheet { hyperlink.setLabel("Link to POI"); s1.getRow(1).createCell(1).setHyperlink(hyperlink); s1.getRow(1).getCell(1).setCellValue(createHelper.createRichTextString("Link to POI")); - +*/ // Save FileOutputStream fout = new FileOutputStream("NewFile.xlsx"); wb.write(fout); diff --git a/src/examples/src/org/apache/poi/xssf/usermodel/examples/CreateUserDefinedDataFormats.java b/src/examples/src/org/apache/poi/xssf/usermodel/examples/CreateUserDefinedDataFormats.java new file mode 100755 index 0000000000..9905a9af31 --- /dev/null +++ b/src/examples/src/org/apache/poi/xssf/usermodel/examples/CreateUserDefinedDataFormats.java @@ -0,0 +1,65 @@ +/* ==================================================================== + 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.DataFormat; +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.XSSFWorkbook; + +/** + * How to set user-defined date formats + */ +public class CreateUserDefinedDataFormats { + + + public static void main(String[]args) throws Exception { + Workbook wb = new XSSFWorkbook(); + Sheet sheet = wb.createSheet("format sheet"); + CellStyle style; + DataFormat format = wb.createDataFormat(); + Row row; + Cell cell; + short rowNum = 0; + short colNum = 0; + + row = sheet.createRow(rowNum++); + cell = row.createCell(colNum); + cell.setCellValue(11111.25); + style = wb.createCellStyle(); + style.setDataFormat(format.getFormat("0.0")); + cell.setCellStyle(style); + + row = sheet.createRow(rowNum++); + cell = row.createCell(colNum); + cell.setCellValue(11111.25); + style = wb.createCellStyle(); + style.setDataFormat(format.getFormat("#,##0.0000")); + cell.setCellStyle(style); + + FileOutputStream fileOut = new FileOutputStream("ooxml_dataFormat.xlsx"); + wb.write(fileOut); + fileOut.close(); + } + +} diff --git a/src/examples/src/org/apache/poi/xssf/usermodel/examples/WorkingWithBorders.java b/src/examples/src/org/apache/poi/xssf/usermodel/examples/WorkingWithBorders.java index f70596f949..baca54c2ca 100755 --- a/src/examples/src/org/apache/poi/xssf/usermodel/examples/WorkingWithBorders.java +++ b/src/examples/src/org/apache/poi/xssf/usermodel/examples/WorkingWithBorders.java @@ -17,7 +17,7 @@ package org.apache.poi.xssf.usermodel.examples; import org.apache.poi.xssf.usermodel.XSSFWorkbook; -import org.apache.poi.xssf.util.IndexedColors; +import org.apache.poi.xssf.usermodel.IndexedColors; import org.apache.poi.ss.usermodel.*; import java.io.FileOutputStream; @@ -40,13 +40,13 @@ public class WorkingWithBorders { // Style the cell with borders all around. CellStyle style = wb.createCellStyle(); style.setBorderBottom(CellStyle.BORDER_THIN); - style.setBottomBorderColor((short)IndexedColors.BLACK); + style.setBottomBorderColor(IndexedColors.BLACK.getIndex()); style.setBorderLeft(CellStyle.BORDER_THIN); - style.setLeftBorderColor((short)IndexedColors.GREEN); + style.setLeftBorderColor(IndexedColors.GREEN.getIndex()); style.setBorderRight(CellStyle.BORDER_THIN); - style.setRightBorderColor((short)IndexedColors.BLUE); + style.setRightBorderColor(IndexedColors.BLUE.getIndex()); style.setBorderTop(CellStyle.BORDER_MEDIUM_DASHED); - style.setTopBorderColor((short)IndexedColors.BLACK); + style.setTopBorderColor(IndexedColors.BLACK.getIndex()); cell.setCellStyle(style); // Write the output to a file diff --git a/src/examples/src/org/apache/poi/xssf/usermodel/examples/WorkingWithFonts.java b/src/examples/src/org/apache/poi/xssf/usermodel/examples/WorkingWithFonts.java index e86e56eced..c2276ca855 100755 --- a/src/examples/src/org/apache/poi/xssf/usermodel/examples/WorkingWithFonts.java +++ b/src/examples/src/org/apache/poi/xssf/usermodel/examples/WorkingWithFonts.java @@ -21,8 +21,8 @@ import org.apache.poi.hssf.util.HSSFColor; import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.poi.xssf.usermodel.XSSFRichTextString; +import org.apache.poi.xssf.usermodel.IndexedColors; import org.apache.poi.xssf.usermodel.extensions.XSSFColor; -import org.apache.poi.xssf.util.IndexedColors; import java.io.FileOutputStream; @@ -42,7 +42,7 @@ public class WorkingWithFonts { font.setFontHeightInPoints((short)24); font.setFontName("Courier New"); - font.setColor((short)IndexedColors.RED); + font.setColor(IndexedColors.RED.getIndex()); font.setItalic(true); font.setStrikeout(true); diff --git a/src/examples/src/org/apache/poi/xssf/usermodel/examples/WorkingWithRichText.java b/src/examples/src/org/apache/poi/xssf/usermodel/examples/WorkingWithRichText.java new file mode 100755 index 0000000000..c8bb2f0223 --- /dev/null +++ b/src/examples/src/org/apache/poi/xssf/usermodel/examples/WorkingWithRichText.java @@ -0,0 +1,62 @@ +/* ==================================================================== + 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.*; +import org.apache.poi.ss.usermodel.*; +import org.apache.poi.hssf.util.HSSFColor; +import org.apache.xmlbeans.XmlOptions; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.*; + +import java.io.FileOutputStream; +import java.io.Writer; +import java.io.StringWriter; + +/** + * Demonstrates how to work with rich text + */ +public class WorkingWithRichText { + + public static void main(String[] args) + throws Exception + { + XSSFWorkbook wb = new XSSFWorkbook(); + + XSSFSheet sheet = wb.createSheet(); + XSSFRow row = sheet.createRow((short) 2); + + XSSFCell cell = row.createCell(1); + XSSFRichTextString rt = new XSSFRichTextString("The quick"); + + XSSFFont font1 = wb.createFont(); + font1.setBold(true); + rt.append(" brown fox", font1); + + XSSFFont font2 = wb.createFont(); + font2.setItalic(true); + font2.setColor(IndexedColors.RED.getIndex()); + rt.applyFont((short)0); + cell.setCellValue(rt); + + // Write the output to a file + FileOutputStream fileOut = new FileOutputStream("rich_text.xlsx"); + wb.write(fileOut); + fileOut.close(); + + } + + } diff --git a/src/ooxml/interfaces-jdk14/org/apache/poi/ss/usermodel/Comment.java b/src/ooxml/interfaces-jdk14/org/apache/poi/ss/usermodel/Comment.java index 1959f9ad56..c85f79bfd3 100644 --- a/src/ooxml/interfaces-jdk14/org/apache/poi/ss/usermodel/Comment.java +++ b/src/ooxml/interfaces-jdk14/org/apache/poi/ss/usermodel/Comment.java @@ -18,6 +18,6 @@ package org.apache.poi.ss.usermodel; public interface Comment { - public void setRow(short row); + public void setRow(int row); public void setColumn(short row); } diff --git a/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Cell.java b/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Cell.java index 29e6bdd8f8..331dd1e78b 100644 --- a/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Cell.java +++ b/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Cell.java @@ -173,6 +173,8 @@ public interface Cell { void setCellValue(RichTextString value); + void setCellValue(String value); + void setCellFormula(String formula); String getCellFormula(); diff --git a/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Color.java b/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Color.java index ecd23691e6..2bbcaf16e3 100644 --- a/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Color.java +++ b/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Color.java @@ -19,22 +19,5 @@ package org.apache.poi.ss.usermodel; public interface Color { - /** - * @return index to the standard palette - */ - - short getIndex(); - - /** - * @return triplet representation like that in Excel - */ - - short[] getTriplet(); - - /** - * @return a hex string exactly like a gnumeric triplet - */ - - String getHexString(); } \ No newline at end of file diff --git a/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Font.java b/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Font.java index 5c070cdd1c..a737b26a41 100644 --- a/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Font.java +++ b/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Font.java @@ -129,14 +129,6 @@ public interface Font { String getFontName(); - /** - * get the index within the HSSFWorkbook (sequence within the collection of Font objects) - * @return unique index number of the underlying record this Font represents (probably you don't care - * unless you're comparing which one is which) - */ - - short getIndex(); - /** * set the font height in unit's of 1/20th of a point. Maybe you might want to * use the setFontHeightInPoints which matches to the familiar 10, 12, 14 etc.. @@ -217,24 +209,6 @@ public interface Font { */ short getColor(); - /** - * set the boldness to use - * @param boldweight - * @see #BOLDWEIGHT_NORMAL - * @see #BOLDWEIGHT_BOLD - */ - - void setBoldweight(short boldweight); - - /** - * get the boldness to use - * @return boldweight - * @see #BOLDWEIGHT_NORMAL - * @see #BOLDWEIGHT_BOLD - */ - - short getBoldweight(); - /** * set normal,super or subscript. * @param offset type to use (none,super,sub) diff --git a/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/RichTextString.java b/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/RichTextString.java index df0ba9ed0c..551fede973 100644 --- a/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/RichTextString.java +++ b/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/RichTextString.java @@ -25,8 +25,6 @@ package org.apache.poi.ss.usermodel; * @author Jason Height (jheight at apache.org) */ public interface RichTextString { - /** Place holder for indicating that NO_FONT has been applied here */ - public static final short NO_FONT = 0; /** * Applies a font to the specified characters of a string. @@ -68,20 +66,8 @@ public interface RichTextString { int length(); /** - * Returns the font in use at a particular index. + * @return The number of formatting runs used. * - * @param index The index. - * @return The font that's currently being applied at that - * index or null if no font is being applied or the - * index is out of range. - */ - short getFontAtIndex(int index); - - /** - * @return The number of formatting runs used. There will always be at - * least one of font NO_FONT. - * - * @see #NO_FONT */ int numFormattingRuns(); @@ -92,14 +78,6 @@ public interface RichTextString { */ int getIndexOfFormattingRun(int index); - /** - * Gets the font used in a particular formatting run. - * - * @param index the index of the formatting run - * @return the font number used. - */ - short getFontOfFormattingRun(int index); - /** * Applies the specified font to the entire string. * diff --git a/src/ooxml/java/org/apache/poi/POIXMLDocument.java b/src/ooxml/java/org/apache/poi/POIXMLDocument.java index 500d09a829..c268dd4c8c 100644 --- a/src/ooxml/java/org/apache/poi/POIXMLDocument.java +++ b/src/ooxml/java/org/apache/poi/POIXMLDocument.java @@ -100,7 +100,14 @@ public abstract class POIXMLDocument { throw new IOException(e.toString()); } } - + public static Package openPackage(InputStream is) throws IOException { + try { + return Package.open(is); + } catch (InvalidFormatException e) { + throw new IOException(e.toString()); + } + } + protected Package getPackage() { return this.pkg; } diff --git a/src/ooxml/java/org/apache/poi/xssf/dev/XSSFSave.java b/src/ooxml/java/org/apache/poi/xssf/dev/XSSFSave.java new file mode 100755 index 0000000000..148a02fcb2 --- /dev/null +++ b/src/ooxml/java/org/apache/poi/xssf/dev/XSSFSave.java @@ -0,0 +1,42 @@ +/* ==================================================================== + 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.dev; + +import org.apache.poi.xssf.usermodel.XSSFWorkbook; + +import java.io.FileOutputStream; + +/** + * Utility which loads a SpreadsheetML file and saves it back. + * This is a handy tool to investigate read-write round trip safety. + * + * @author Yegor Kozlov + */ +public class XSSFSave { + public static void main(String[] args) throws Exception { + for (int i = 0; i < args.length; i++) { + XSSFWorkbook wb = new XSSFWorkbook(args[i]); + + int sep = args[i].lastIndexOf('.'); + String outfile = args[i].substring(0, sep) + "-save.xlsx"; + FileOutputStream out = new FileOutputStream(outfile); + wb.write(out); + out.close(); + } + } + +} diff --git a/src/ooxml/java/org/apache/poi/xssf/model/SharedStringsTable.java b/src/ooxml/java/org/apache/poi/xssf/model/SharedStringsTable.java index 515706c772..b526d30828 100644 --- a/src/ooxml/java/org/apache/poi/xssf/model/SharedStringsTable.java +++ b/src/ooxml/java/org/apache/poi/xssf/model/SharedStringsTable.java @@ -195,7 +195,7 @@ public class SharedStringsTable implements SharedStringSource, XSSFModel { options.setUseDefaultNamespace(); //re-create the sst table every time saving a workbook - SstDocument doc = SstDocument.Factory.newInstance(options); + SstDocument doc = SstDocument.Factory.newInstance(); CTSst sst = doc.addNewSst(); sst.setCount(count); sst.setUniqueCount(uniqueCount); diff --git a/src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java b/src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java index b7a6560a67..7326943784 100644 --- a/src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java +++ b/src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java @@ -20,6 +20,7 @@ package org.apache.poi.xssf.model; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.util.ArrayList; import java.util.Enumeration; import java.util.Hashtable; import java.util.LinkedList; @@ -59,7 +60,7 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.StyleSheetDocument; */ public class StylesTable implements StylesSource, XSSFModel { private final Hashtable numberFormats = new Hashtable(); - private final LinkedList fonts = new LinkedList(); + private final ArrayList fonts = new ArrayList(); private final LinkedList fills = new LinkedList(); private final LinkedList borders = new LinkedList(); private final LinkedList styleXfs = new LinkedList(); @@ -230,7 +231,12 @@ public class StylesTable implements StylesSource, XSSFModel { public int getNumCellStyles(){ return styleXfs.size(); } - + /** + * get the size of fonts + */ + public int getNumberOfFonts(){ + return this.fonts.size(); + } /** * For unit testing only */ @@ -367,7 +373,7 @@ public class StylesTable implements StylesSource, XSSFModel { return fill.putFill(fills); } - private long putFont(XSSFFont font, LinkedList fonts) { + private long putFont(XSSFFont font, ArrayList fonts) { return font.putFont(fonts); } private void initialize() { @@ -423,8 +429,8 @@ public class StylesTable implements StylesSource, XSSFModel { XSSFFont font=new XSSFFont(ctFont); return font; */ - - XSSFFont xssfFont=new 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); diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/IndexedColors.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/IndexedColors.java new file mode 100755 index 0000000000..fbce6fa36c --- /dev/null +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/IndexedColors.java @@ -0,0 +1,79 @@ +package org.apache.poi.xssf.usermodel; + +/** + * A deprecated indexing scheme for colours that is still required for some records, and for backwards + * compatibility with OLE2 formats. + * + *

+ * Each element corresponds to a color index (zero-based). When using the default indexed color palette, + * the values are not written out, but instead are implied. When the color palette has been modified from default, + * then the entire color palette is used. + *

+ * + * @author Yegor Kozlov + */ +public enum IndexedColors { + + BLACK(8), + WHITE(9), + RED(10), + BRIGHT_GREEN(11), + BLUE(12), + YELLOW(13), + PINK(14), + TURQUOISE(15), + DARK_RED(16), + GREEN(17), + DARK_BLUE(18), + DARK_YELLOW(19), + VIOLET(20), + TEAL(21), + GREY_25_PERCENT(22), + GREY_50_PERCENT(23), + CORNFLOWER_BLUE(24), + MAROON(25), + LEMON_CHIFFON(26), + ORCHID(28), + CORAL(29), + ROYAL_BLUE(30), + LIGHT_CORNFLOWER_BLUE(31), + SKY_BLUE(40), + LIGHT_TURQUOISE(41), + LIGHT_GREEN(42), + LIGHT_YELLOW(43), + PALE_BLUE(44), + ROSE(45), + LAVENDER(46), + TAN(47), + LIGHT_BLUE(48), + AQUA(49), + LIME(50), + GOLD(51), + LIGHT_ORANGE(52), + ORANGE(53), + BLUE_GREY(54), + GREY_40_PERCENT(55), + DARK_TEAL(56), + SEA_GREEN(57), + DARK_GREEN(58), + OLIVE_GREEN(59), + BROWN(60), + PLUM(61), + INDIGO(62), + GREY_80_PERCENT(63); + + private short index; + + IndexedColors(int idx){ + index = (short)idx; + } + + /** + * Returns index of this color + * + * @return index of this color + */ + public short getIndex(){ + return index; + } +} diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java index 4b77bd10f0..29e2f0876a 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java @@ -392,6 +392,10 @@ public final class XSSFCell implements Cell { } + public void setCellValue(String str) { + this.setCellValue(new XSSFRichTextString(str)); + } + public void setCellValue(RichTextString value) { if(this.cell.getT() == STCellType.INLINE_STR) { this.cell.setV(value.getString()); @@ -401,6 +405,7 @@ public final class XSSFCell implements Cell { this.cell.setT(STCellType.S); } XSSFRichTextString rt = (XSSFRichTextString)value; + rt.setStylesTableReference(stylesSource); int sRef = sharedStringSource.addEntry(rt.getCTRst()); this.cell.setV(Integer.toString(sRef)); } @@ -437,13 +442,6 @@ public final class XSSFCell implements Cell { } } - /** - * Creates an XSSFRichTextString for you. - */ - public RichTextString createRichTextString(String text) { - return new XSSFRichTextString(text); - } - public Hyperlink getHyperlink() { return row.getSheet().getHyperlink(row.getRowNum(), cellNum); } diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java index cf1efaa990..544db15623 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java @@ -417,7 +417,7 @@ public class XSSFCellStyle implements CellStyle { return (short) getBorderColor(side).getIndexed(); } - private void setBorderColorIndexed(BorderSide side, long color) { + private void setBorderColorIndexed(BorderSide side, int color) { getBorderColor(side).setIndexed(color); } diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFont.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFont.java index fb0e0cbb68..e052519451 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFont.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFont.java @@ -1,491 +1,406 @@ -/* ==================================================================== - 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; - -import java.util.LinkedList; - -import org.apache.poi.ss.usermodel.Font; - -import org.apache.poi.xssf.util.CTFontWrapper; -import org.apache.poi.xssf.util.Charset; -import org.apache.poi.xssf.util.IndexedColors; -import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBooleanProperty; -import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor; - -import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFont; -import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFontName; -import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFontScheme; -import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFontSize; -import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTIntProperty; -import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTUnderlineProperty; -import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTVerticalAlignFontProperty; -import org.openxmlformats.schemas.spreadsheetml.x2006.main.STFontScheme; -import org.openxmlformats.schemas.spreadsheetml.x2006.main.STUnderlineValues; -import org.openxmlformats.schemas.spreadsheetml.x2006.main.STVerticalAlignRun; - -public class XSSFFont implements Font { - - - public static final int SCHEME_MAJOR=2; - public static final int SCHEME_MINOR=3; - public static final int SCHEME_NONE=0; - - public static final int FONT_FAMILY_NOT_APPLICABLE=0; - public static final int FONT_FAMILY_ROMAN=1; - public static final int FONT_FAMILY_SWISS=2; - public static final int FONT_FAMILY_MODERN=3; - public static final int FONT_FAMILY_SCRIPT=4; - public static final int FONT_FAMILY_DECORATIVE=5; - - - public static final String DEFAULT_FONT_NAME="Calibri"; - public static final short DEFAULT_FONT_SIZE=11; - public static final short DEFAULT_FONT_COLOR=(short)IndexedColors.BLACK; - - private int index=0; - - - private CTFontWrapper fontWrapper; - - - - public XSSFFont(CTFont font) { - this.fontWrapper=new CTFontWrapper(font); - } - - /* - public XSSFFont(int index) { - this.fontWrapper=new CTFontWrapper(font); - this.index=index; - } - */ - - public XSSFFont() { - this.fontWrapper = new CTFontWrapper(CTFont.Factory.newInstance()); - } - - - public CTFont getCTFont(){ - return fontWrapper.getCTFont(); - } - - - public short getBoldweight() { - CTBooleanProperty bold=fontWrapper.getB(); - if(bold!=null && bold.getVal()) - return Font.BOLDWEIGHT_BOLD; - else - return Font.BOLDWEIGHT_NORMAL; - } - - - - public byte getCharSet() { - CTIntProperty charset= fontWrapper.getCharset(); - if(charset!=null){ - //this value must be set -- can't be null - switch (charset.getVal()) { - case Charset.ANSI_CHARSET: - return Font.ANSI_CHARSET; - - case Charset.DEFAULT_CHARSET: - return Font.DEFAULT_CHARSET; - - case Charset.SYMBOL_CHARSET: - return Font.SYMBOL_CHARSET; - - default://maight be correct to return this byte value??? - return Byte.parseByte(Integer.toString(charset.getVal())); - } - } - else - return Font.ANSI_CHARSET; - } - - public short getColor() { - CTColor color=fontWrapper.getColor(); - long index=color.getIndexed(); - if (index==XSSFFont.DEFAULT_FONT_COLOR){ - return Font.COLOR_NORMAL; - } - else if(index==IndexedColors.RED){ - return Font.COLOR_RED; - } - else{ - return Short.parseShort(new Long(index).toString()); - } - } - - public short getFontHeight() { - if(fontWrapper.getSz()!=null){ - double fontHeight= fontWrapper.getSz().getVal()/20; - return (short)fontHeight; - } - else - return DEFAULT_FONT_SIZE/20; - } - - public short getFontHeightInPoints() { - if(fontWrapper.getSz()!=null){ - double fontHeight= fontWrapper.getSz().getVal();// /72; - return (short)fontHeight;//new Double(fontHeight).shortValue(); - } - else - return DEFAULT_FONT_SIZE; - } - - //AGGIUNGERE CONTROLLO NULL - public String getFontName() { - if(fontWrapper.getName()!=null) - return fontWrapper.getName().getVal(); - else - return DEFAULT_FONT_NAME; - } - - - public short getIndex() { - // TODO Auto-generated method stub - return 0; - } - - public boolean getItalic() { - if(fontWrapper.getI()!=null) - return fontWrapper.getI().getVal(); - else - return false; - } - - public boolean getStrikeout() { - if(fontWrapper.getStrike()!=null) - return fontWrapper.getStrike().getVal(); - else - return false; - } - - public short getTypeOffset() { - if(fontWrapper.getVertAlign()!=null){ - int val=fontWrapper.getVertAlign().getVal().intValue(); - switch (val) { - case STVerticalAlignRun.INT_BASELINE: - return Font.SS_NONE; - case STVerticalAlignRun.INT_SUBSCRIPT: - return Font.SS_SUB; - case STVerticalAlignRun.INT_SUPERSCRIPT: - return Font.SS_SUPER; - default: throw new RuntimeException("Wrong offset value "+val); - } - } - else - return Font.SS_NONE; - } - - public byte getUnderline() { - if(fontWrapper.getU()!=null){ - //attenzione : -- get val pu˜ tornare null---- - switch (fontWrapper.getU().getVal().intValue()) { - case STUnderlineValues.INT_DOUBLE: - return Font.U_DOUBLE; - case STUnderlineValues.INT_DOUBLE_ACCOUNTING: - return Font.U_DOUBLE_ACCOUNTING; - - case STUnderlineValues.INT_SINGLE_ACCOUNTING: - return Font.U_SINGLE_ACCOUNTING; - - case STUnderlineValues.INT_NONE: - return Font.U_NONE; - - case STUnderlineValues.INT_SINGLE: - default: - return Font.U_SINGLE; - } - } - return Font.U_NONE; - } - - public void setBoldweight(short boldweight) { - if(boldweight==Font.BOLDWEIGHT_BOLD){ - - CTBooleanProperty bold; - if(fontWrapper.getCTFont().getBArray().length==0){ - bold=fontWrapper.getCTFont().addNewB(); - } - else{ - bold=CTBooleanProperty.Factory.newInstance(); - } - bold.setVal(true); - fontWrapper.setB(bold); - } - } - - public void setCharSet(byte charset) { - CTIntProperty charsetProperty; - if(fontWrapper.getCTFont().getCharsetArray().length==0){ - charsetProperty=fontWrapper.getCTFont().addNewCharset(); - } - else{ - charsetProperty=CTIntProperty.Factory.newInstance(); - } - switch (charset) { - case Font.ANSI_CHARSET: - charsetProperty.setVal(Charset.ANSI_CHARSET); - break; - case Font.SYMBOL_CHARSET: - charsetProperty.setVal(Charset.SYMBOL_CHARSET); - break; - case Font.DEFAULT_CHARSET: - charsetProperty.setVal(Charset.DEFAULT_CHARSET); - break; - default: - throw new RuntimeException("Attention: an attempt to set a type of unknow charset and charset"); - } - - fontWrapper.setCharset(charsetProperty); - } - - - public void setColor(short color) { - CTColor ctColor; - if(fontWrapper.getCTFont().getColorArray().length==0){ - ctColor=fontWrapper.getCTFont().addNewColor(); - } - else{ - ctColor=CTColor.Factory.newInstance(); - } - - switch (color) { - case Font.COLOR_NORMAL:{ - ctColor.setIndexed(XSSFFont.DEFAULT_FONT_COLOR); - break; - } - case Font.COLOR_RED:{ - ctColor.setIndexed(IndexedColors.RED); - break; - } - default: - ctColor.setIndexed(color); - } - - fontWrapper.setColor(ctColor); - } - - - - public void setFontHeight(short height) { - CTFontSize fontSize; - if(fontWrapper.getCTFont().getSzArray().length==0){ - fontSize=fontWrapper.getCTFont().addNewSz(); - } - else{ - fontSize=CTFontSize.Factory.newInstance(); - } - fontSize.setVal(height*20); - fontWrapper.setSz(fontSize); - } - - - public void setFontHeightInPoints(short height) { - CTFontSize fontSize; - if(fontWrapper.getCTFont().getSzArray().length==0){ - fontSize=fontWrapper.getCTFont().addNewSz(); - } - else{ - fontSize=CTFontSize.Factory.newInstance(); - } - - fontSize.setVal(height); - fontWrapper.setSz(fontSize); - } - - - - public void setFontName(String name) { - CTFontName fontName; - if(fontWrapper.getCTFont().getNameArray().length==0){ - fontName=fontWrapper.getCTFont().addNewName(); - } - else{ - fontName=CTFontName.Factory.newInstance(); - } - - fontName.setVal(name); - fontWrapper.setName(fontName); - } - - - public void setItalic(boolean italic) { - CTBooleanProperty bool; - if(fontWrapper.getCTFont().getIArray().length==0){ - bool=fontWrapper.getCTFont().addNewI(); - } - else{ - bool=CTBooleanProperty.Factory.newInstance(); - } - - bool.setVal(italic); - fontWrapper.setI(bool); - } - - public void setStrikeout(boolean strikeout) { - CTBooleanProperty strike; - if(fontWrapper.getCTFont().getStrikeArray().length==0){ - strike=fontWrapper.getCTFont().addNewStrike(); - } - else{ - strike=CTBooleanProperty.Factory.newInstance(); - } - strike.setVal(strikeout); - fontWrapper.setStrike(strike); - } - - public void setTypeOffset(short offset) { - CTVerticalAlignFontProperty offsetProperty; - if(fontWrapper.getCTFont().getVertAlignArray().length==0){ - offsetProperty=fontWrapper.getCTFont().addNewVertAlign(); - } - else{ - offsetProperty=CTVerticalAlignFontProperty.Factory.newInstance(); - } - switch (offset) { - case Font.SS_NONE: - offsetProperty.setVal(STVerticalAlignRun.BASELINE); - break; - case Font.SS_SUB: - offsetProperty.setVal(STVerticalAlignRun.SUBSCRIPT); - break; - case Font.SS_SUPER: - offsetProperty.setVal(STVerticalAlignRun.SUPERSCRIPT); - break; - } - fontWrapper.setVertAlign(offsetProperty); - } - - public void setUnderline(byte underline) { - CTUnderlineProperty ctUnderline; - if(fontWrapper.getCTFont().getUArray().length==0){ - ctUnderline=fontWrapper.getCTFont().addNewU(); - } - else{ - ctUnderline=CTUnderlineProperty.Factory.newInstance(); - } - switch (underline) { - case Font.U_DOUBLE: - ctUnderline.setVal(STUnderlineValues.DOUBLE); - break; - case Font.U_DOUBLE_ACCOUNTING: - ctUnderline.setVal(STUnderlineValues.DOUBLE_ACCOUNTING); - break; - case Font.U_SINGLE_ACCOUNTING: - ctUnderline.setVal(STUnderlineValues.SINGLE_ACCOUNTING); - break; - case Font.U_NONE: - ctUnderline.setVal(STUnderlineValues.NONE); - break; - - case Font.U_SINGLE: - default: - ctUnderline.setVal(STUnderlineValues.SINGLE); - break; - } - - fontWrapper.setU(ctUnderline); - } - - - public long putFont(LinkedList fonts) { - //TODO - /* - * we need to implement a method equals to check that 2 instances of CTFont - * are different by comparison of all font attributes. - * NB: take a look to findFont method in XSSFWorkbook - */ - CTFont font=fontWrapper.getCTFont(); - if(fonts.contains(font)) { - return fonts.indexOf(font); - } - fonts.add(font); - return fonts.size() - 1; - } - - // solo di xssfFont - non di Font- - //sono utilizzati nel metodo createDefaultFont in StylesTable insta. - - public int getScheme(){ - int fontScheme = fontWrapper.getFontScheme().getVal().intValue(); - switch (fontScheme) { - case STFontScheme.INT_MAJOR: - return XSSFFont.SCHEME_MAJOR; - case STFontScheme.INT_MINOR: - return XSSFFont.SCHEME_MINOR; - case STFontScheme.INT_NONE: - return XSSFFont.SCHEME_NONE; - - default: - return fontScheme; - } - } - - - public void setScheme(int scheme){ - CTFontScheme ctFontScheme; - if(fontWrapper.getCTFont().getSchemeArray().length==0){ - ctFontScheme=fontWrapper.getCTFont().addNewScheme(); - } - else{ - ctFontScheme=CTFontScheme.Factory.newInstance(); - } - switch (scheme) { - case XSSFFont.SCHEME_MAJOR: - ctFontScheme.setVal(STFontScheme.MAJOR); - break; - case XSSFFont.SCHEME_MINOR: - ctFontScheme.setVal(STFontScheme.MINOR); - break; - case XSSFFont.SCHEME_NONE: - ctFontScheme.setVal(STFontScheme.NONE); - break; - default: - throw new RuntimeException("Schema value ["+ scheme +"] not supported in XSSFFont"); - } - - fontWrapper.setFontScheme(ctFontScheme); - } - - - - public int getFamily(){ - if(fontWrapper.getFamily()!=null) - return fontWrapper.getFamily().getVal(); - else - return XSSFFont.FONT_FAMILY_SWISS; - } - - public void setFamily(int value){ - //TODO - CTIntProperty family; - if(fontWrapper.getCTFont().getSchemeArray().length==0){ - family=fontWrapper.getCTFont().addNewFamily(); - } - else{ - family=CTIntProperty.Factory.newInstance(); - } - family.setVal(value); - //fontWrapper.setFamily - } - - - -} +/* ==================================================================== + 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; + +import org.apache.poi.ss.usermodel.Font; + +import org.apache.poi.xssf.util.Charset; +import org.apache.poi.xssf.usermodel.extensions.XSSFColor; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBooleanProperty; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor; + +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFont; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFontName; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFontScheme; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFontSize; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTIntProperty; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTUnderlineProperty; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTVerticalAlignFontProperty; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.STFontScheme; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.STUnderlineValues; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.STVerticalAlignRun; + +import java.util.ArrayList; + +public class XSSFFont implements Font { + + + public static final int SCHEME_MAJOR=2; + public static final int SCHEME_MINOR=3; + public static final int SCHEME_NONE=0; + + public static final int FONT_FAMILY_NOT_APPLICABLE=0; + public static final int FONT_FAMILY_ROMAN=1; + public static final int FONT_FAMILY_SWISS=2; + public static final int FONT_FAMILY_MODERN=3; + public static final int FONT_FAMILY_SCRIPT=4; + public static final int FONT_FAMILY_DECORATIVE=5; + + + public static final String DEFAULT_FONT_NAME="Calibri"; + public static final short DEFAULT_FONT_SIZE=11; + public static final short DEFAULT_FONT_COLOR = IndexedColors.BLACK.getIndex(); + + private CTFont ctFont; + + public XSSFFont(CTFont font) { + this.ctFont=font; + } + + protected XSSFFont() { + this.ctFont = CTFont.Factory.newInstance(); + } + + + public CTFont getCTFont(){ + return ctFont; + } + + /** + * + */ + public boolean getBold() { + CTBooleanProperty bold=ctFont.sizeOfBArray() == 0 ? null : ctFont.getBArray(0); + return (bold!=null && bold.getVal()); + } + + + + public byte getCharSet() { + CTIntProperty charset= ctFont.sizeOfCharsetArray() == 0?null:ctFont.getCharsetArray(0); + if(charset!=null){ + //this value must be set -- can't be null + switch (charset.getVal()) { + case Charset.ANSI_CHARSET: + return Font.ANSI_CHARSET; + + case Charset.DEFAULT_CHARSET: + return Font.DEFAULT_CHARSET; + + case Charset.SYMBOL_CHARSET: + return Font.SYMBOL_CHARSET; + + default://maight be correct to return this byte value??? + return Byte.parseByte(Integer.toString(charset.getVal())); + } + } + else + return Font.ANSI_CHARSET; + } + + public short getColor() { + CTColor color=ctFont.sizeOfColorArray()==0?null: ctFont.getColorArray(0); + if(color == null) return Font.COLOR_NORMAL; + + long index=color.getIndexed(); + if (index==XSSFFont.DEFAULT_FONT_COLOR){ + return Font.COLOR_NORMAL; + } + else if(index == IndexedColors.RED.getIndex()){ + return Font.COLOR_RED; + } + else{ + return Short.parseShort(new Long(index).toString()); + } + } + + + public byte[] getRgbColor() { + CTColor color=ctFont.sizeOfColorArray()==0?null: ctFont.getColorArray(0); + return color.getRgb(); + } + + public short getThemeColor() { + CTColor color=ctFont.sizeOfColorArray()==0?null: ctFont.getColorArray(0); + long index=color.getTheme(); + return (short)index; + } + + + public short getFontHeight() { + CTFontSize size=ctFont.sizeOfSzArray()==0?null: ctFont.getSzArray(0); + if(size!=null){ + double fontHeight= size.getVal()/20; + return (short)fontHeight; + } + else + return DEFAULT_FONT_SIZE/20; + } + + public short getFontHeightInPoints() { + CTFontSize size=ctFont.sizeOfSzArray()==0?null: ctFont.getSzArray(0); + if(size!=null){ + double fontHeight= size.getVal(); + return (short)fontHeight; + } + else + return DEFAULT_FONT_SIZE; + } + + + public String getFontName() { + CTFontName name=ctFont.sizeOfNameArray()==0?null:ctFont.getNameArray(0); + return name==null? null:name.getVal(); + } + + + public boolean getItalic() { + CTBooleanProperty italic=ctFont.sizeOfIArray()==0?null:ctFont.getIArray(0); + return italic!=null && italic.getVal(); + } + + public boolean getStrikeout() { + CTBooleanProperty strike=ctFont.sizeOfStrikeArray()==0?null:ctFont.getStrikeArray(0); + return strike!=null && strike.getVal(); + } + + public short getTypeOffset() { + CTVerticalAlignFontProperty vAlign=ctFont.sizeOfVertAlignArray()==0?null:ctFont.getVertAlignArray(0); + if(vAlign!=null){ + int val=vAlign.getVal().intValue(); + switch (val) { + case STVerticalAlignRun.INT_BASELINE: + return Font.SS_NONE; + case STVerticalAlignRun.INT_SUBSCRIPT: + return Font.SS_SUB; + case STVerticalAlignRun.INT_SUPERSCRIPT: + return Font.SS_SUPER; + default: throw new RuntimeException("Wrong offset value "+val); + } + } + else + return Font.SS_NONE; + } + + public byte getUnderline() { + CTUnderlineProperty underline=ctFont.sizeOfUArray()==0?null:ctFont.getUArray(0); + if(underline!=null){ + switch (underline.getVal().intValue()) { + case STUnderlineValues.INT_DOUBLE: + return Font.U_DOUBLE; + case STUnderlineValues.INT_DOUBLE_ACCOUNTING: + return Font.U_DOUBLE_ACCOUNTING; + + case STUnderlineValues.INT_SINGLE_ACCOUNTING: + return Font.U_SINGLE_ACCOUNTING; + + case STUnderlineValues.INT_NONE: + return Font.U_NONE; + + case STUnderlineValues.INT_SINGLE: + default: + return Font.U_SINGLE; + } + } + return Font.U_NONE; + } + + /** + * Set characters in bold face font style. + * If omitted, the default value is true. + */ + public void setBold(boolean bold) { + CTBooleanProperty ctBold=ctFont.sizeOfBArray()==0?ctFont.addNewB():ctFont.getBArray(0); + ctBold.setVal(true); + } + + /** + * + */ + public void setCharSet(byte charset) { + CTIntProperty charsetProperty=ctFont.sizeOfCharsetArray()==0?ctFont.addNewCharset():ctFont.getCharsetArray(0); + switch (charset) { + case Font.ANSI_CHARSET: + charsetProperty.setVal(Charset.ANSI_CHARSET); + break; + case Font.SYMBOL_CHARSET: + charsetProperty.setVal(Charset.SYMBOL_CHARSET); + break; + case Font.DEFAULT_CHARSET: + charsetProperty.setVal(Charset.DEFAULT_CHARSET); + break; + default: + throw new RuntimeException("Attention: an attempt to set a type of unknow charset and charset"); + } + } + + + public void setColor(short color) { + CTColor ctColor=ctFont.sizeOfColorArray()==0?ctFont.addNewColor():ctFont.getColorArray(0); + + switch (color) { + case Font.COLOR_NORMAL:{ + ctColor.setIndexed(XSSFFont.DEFAULT_FONT_COLOR); + break; + } + case Font.COLOR_RED:{ + ctColor.setIndexed(IndexedColors.RED.getIndex()); + break; + } + default: + ctColor.setIndexed(color); + } + } + + + + public void setFontHeight(short height) { + CTFontSize fontSize=ctFont.sizeOfSzArray()==0?ctFont.addNewSz():ctFont.getSzArray(0); + fontSize.setVal(height*20); + } + + + public void setFontHeightInPoints(short height) { + CTFontSize fontSize=ctFont.sizeOfSzArray()==0?ctFont.addNewSz():ctFont.getSzArray(0); + fontSize.setVal(height); + } + + + + public void setRgbColor(XSSFColor color) { + CTColor ctColor=ctFont.sizeOfColorArray()==0?ctFont.addNewColor():ctFont.getColorArray(0); + ctColor.setRgb(color.getRgb()); + } + + public void setThemeColor(short theme) { + CTColor ctColor=ctFont.sizeOfColorArray()==0?ctFont.addNewColor():ctFont.getColorArray(0); + ctColor.setTheme(theme); + } + + public void setFontName(String name) { + CTFontName fontName=ctFont.sizeOfNameArray()==0?ctFont.addNewName():ctFont.getNameArray(0); + fontName.setVal(name); + } + + public void setItalic(boolean italic) { + CTBooleanProperty bool=ctFont.sizeOfIArray()==0?ctFont.addNewI():ctFont.getIArray(0); + bool.setVal(italic); + } + + public void setStrikeout(boolean strikeout) { + CTBooleanProperty strike=ctFont.sizeOfStrikeArray()==0?ctFont.addNewStrike():ctFont.getStrikeArray(0); + strike.setVal(strikeout); + } + + public void setTypeOffset(short offset) { + CTVerticalAlignFontProperty offsetProperty=ctFont.sizeOfVertAlignArray()==0?ctFont.addNewVertAlign(): ctFont.getVertAlignArray(0); + switch (offset) { + case Font.SS_NONE: + offsetProperty.setVal(STVerticalAlignRun.BASELINE); + break; + case Font.SS_SUB: + offsetProperty.setVal(STVerticalAlignRun.SUBSCRIPT); + break; + case Font.SS_SUPER: + offsetProperty.setVal(STVerticalAlignRun.SUPERSCRIPT); + break; + } + } + + public void setUnderline(byte underline) { + CTUnderlineProperty ctUnderline=ctFont.sizeOfUArray()==0?ctFont.addNewU():ctFont.getUArray(0); + switch (underline) { + case Font.U_DOUBLE: + ctUnderline.setVal(STUnderlineValues.DOUBLE); + break; + case Font.U_DOUBLE_ACCOUNTING: + ctUnderline.setVal(STUnderlineValues.DOUBLE_ACCOUNTING); + break; + case Font.U_SINGLE_ACCOUNTING: + ctUnderline.setVal(STUnderlineValues.SINGLE_ACCOUNTING); + break; + case Font.U_NONE: + ctUnderline.setVal(STUnderlineValues.NONE); + break; + + case Font.U_SINGLE: + default: + ctUnderline.setVal(STUnderlineValues.SINGLE); + break; + } + } + + + public long putFont(ArrayList fonts) { + //TODO + /* + * we need to implement a method equals to check that 2 instances of CTFont + * are different by comparison of all font attributes. + * NB: take a look to findFont method in XSSFWorkbook + */ + if(fonts.contains(ctFont)) { + return fonts.indexOf(ctFont); + } + fonts.add(ctFont); + return fonts.size() - 1; + } + + // solo di xssfFont - non di Font- + //sono utilizzati nel metodo createDefaultFont in StylesTable insta. + + public int getScheme(){ + CTFontScheme scheme=ctFont.sizeOfSchemeArray()==0?null:ctFont.getSchemeArray(0); + if(scheme!=null){ + int fontScheme = scheme.getVal().intValue(); + switch (fontScheme) { + case STFontScheme.INT_MAJOR: + return XSSFFont.SCHEME_MAJOR; + case STFontScheme.INT_MINOR: + return XSSFFont.SCHEME_MINOR; + case STFontScheme.INT_NONE: + return XSSFFont.SCHEME_NONE; + + default: + return fontScheme; + } + } + return 0; + } + + + public void setScheme(int scheme){ + CTFontScheme ctFontScheme=ctFont.sizeOfSchemeArray()==0?ctFont.addNewScheme():ctFont.getSchemeArray(0); + switch (scheme) { + case XSSFFont.SCHEME_MAJOR: + ctFontScheme.setVal(STFontScheme.MAJOR); + break; + case XSSFFont.SCHEME_MINOR: + ctFontScheme.setVal(STFontScheme.MINOR); + break; + case XSSFFont.SCHEME_NONE: + ctFontScheme.setVal(STFontScheme.NONE); + break; + default: + throw new RuntimeException("Schema value ["+ scheme +"] not supported in XSSFFont"); + } + } + + + public int getFamily(){ + CTIntProperty family=ctFont.sizeOfFamilyArray()==0?ctFont.addNewFamily():ctFont.getFamilyArray(0); + if(family!=null) + return family.getVal(); + else + return XSSFFont.FONT_FAMILY_SWISS; + } + + public void setFamily(int value){ + CTIntProperty family=ctFont.sizeOfFamilyArray()==0?ctFont.addNewFamily():ctFont.getFamilyArray(0); + family.setVal(value); + } + +} diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRichTextString.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRichTextString.java index 83f44da842..799479dc98 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRichTextString.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRichTextString.java @@ -20,8 +20,9 @@ package org.apache.poi.xssf.usermodel; import org.apache.poi.ss.usermodel.Font; import org.apache.poi.ss.usermodel.RichTextString; import org.apache.poi.xssf.model.StylesTable; -import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst; -import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRElt; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.*; + +import java.util.ArrayList; /** @@ -30,23 +31,11 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRElt; *

* Most strings in a workbook have formatting applied at the cell level, that is, the entire string in the cell has the * same formatting applied. In these cases, the formatting for the cell is stored in the styles part, - * and the string for the cell can be shared across the workbook. The following xml and code snippet illustrate the example. + * and the string for the cell can be shared across the workbook. The following code illustrates the example. *

* *
*
- * <sst xmlns=http://schemas.openxmlformats.org/spreadsheetml/2006/5/main
- * count="1" uniqueCount="1">
- * <si>
- * <t>Apache POI</t>
- * </si>
- * </sst>
- * 
- *
- * - * The code to produce xml above: - *
- *
  *     cell1.setCellValue(new XSSFRichTextString("Apache POI"));
  *     cell2.setCellValue(new XSSFRichTextString("Apache POI"));
  *     cell3.setCellValue(new XSSFRichTextString("Apache POI"));
@@ -73,56 +62,32 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRElt;
  * 
*
* - * The code above will produce the following xml: - *
- *
- * <sst xmlns=http://schemas.openxmlformats.org/spreadsheetml/2006/5/main count="2" uniqueCount="2">
- *  <si>
- *    <r>
- *      <rPr>
- *        <b/>
- *        <sz val="11"/>
- *        <color theme="1"/>
- *        <rFont val="Arial"/>
- *        <family val="2"/>
- *        <scheme val="minor"/>
- *      </rPr>
- *      <t>Apache POI</t>
- *    </r>
- *  </si>
- *  <si>
- *    <r>
- *      <rPr>
- *       <i/>
- *       <sz val="11"/>
- *        <color theme="1"/>
- *        <rFont val="Courier"/>
- *        <family val="1"/>
- *        <scheme val="minor"/>
- *      </rPr>
- *      <t>Apache POI</t>
- *    </r>
- *  </si>
- *</sst>
- *
- * 
- *
* * @author Yegor Kozlov */ public class XSSFRichTextString implements RichTextString { private CTRst st; private StylesTable styles; + private ArrayList fontIdRuns; + /** + * Create a rich text string and initialize it with empty string + */ public XSSFRichTextString(String str) { st = CTRst.Factory.newInstance(); st.setT(str); } + /** + * Create empty rich text string + */ public XSSFRichTextString() { st = CTRst.Factory.newInstance(); } + /** + * Create a rich text string from the supplied XML bean + */ public XSSFRichTextString(CTRst st) { this.st = st; } @@ -135,8 +100,17 @@ public class XSSFRichTextString implements RichTextString { * @param fontIndex The font to use. */ public void applyFont(int startIndex, int endIndex, short fontIndex) { - // TODO Auto-generated method stub - + XSSFFont font; + if(styles == null) { + //style table is not set, remember fontIndex and set the run properties later, + //when setStylesTableReference is called + font = new XSSFFont(); + font.setFontName("#" + fontIndex); + fontIdRuns = new ArrayList(); + } else { + font = (XSSFFont)styles.getFontAt(fontIndex); + } + applyFont(startIndex, endIndex, font); } /** @@ -147,7 +121,69 @@ public class XSSFRichTextString implements RichTextString { * @param font The index of the font to use. */ public void applyFont(int startIndex, int endIndex, Font font) { - applyFont(0, length(), font.getIndex()); + if (startIndex > endIndex) + throw new IllegalArgumentException("Start index must be less than end index."); + if (startIndex < 0 || endIndex > length()) + throw new IllegalArgumentException("Start and end index not in range."); + if (startIndex == endIndex) + return; + + if(st.sizeOfRArray() == 0 && st.isSetT()) { + //convert string into a text run: string + st.addNewR().setT(st.getT()); + st.unsetT(); + } + + String text = getString(); + + XSSFFont xssfFont = (XSSFFont)font; + ArrayList runs = new ArrayList(); + + int pos = 0; + int i; + for (i = 0; i < st.sizeOfRArray(); i++) { + CTRElt r = st.getRArray(i); + + int len = r.getT().length(); + int p1 = pos; + int p2 = pos + len; + if(startIndex > p2) { + runs.add(r); + } else if (startIndex >= p1 && startIndex < p2){ + String t = r.getT(); + r.setT(t.substring(0, startIndex-p1)); + runs.add(r); + } else { + break; + } + pos = p2; + } + CTRElt r = CTRElt.Factory.newInstance(); + r.setT(text.substring(startIndex, endIndex)); + CTRPrElt pr = r.addNewRPr(); + setRunAttributes(xssfFont.getCTFont(), pr); + if(fontIdRuns != null) fontIdRuns.add(pr); + runs.add(r); + + for (; i < st.sizeOfRArray(); i++) { + r = st.getRArray(i); + + int len = r.getT().length(); + int p1 = pos; + int p2 = pos + len; + if(endIndex > p2) { + ; + } else if (endIndex >= p1 && endIndex < p2){ + String t = r.getT(); + r.setT(t.substring(endIndex-p1, len)); + runs.add(r); + } else { + runs.add(r); + } + pos = p2; + } + + st.setRArray(runs.toArray(new CTRElt[runs.size()])); } /** @@ -155,7 +191,20 @@ public class XSSFRichTextString implements RichTextString { * @param font The font to use. */ public void applyFont(Font font) { - applyFont(0, length(), font); + if(st.sizeOfRArray() == 0 && st.isSetT()) { + CTRElt r = st.addNewR(); + r.setT(st.getT()); + setRunAttributes(((XSSFFont)font).getCTFont(), r.addNewRPr()); + st.unsetT(); + } else { + CTRElt r = CTRElt.Factory.newInstance(); + r.setT(getString()); + setRunAttributes(((XSSFFont)font).getCTFont(), r.addNewRPr()); + st.setRArray(new CTRElt[]{r}); + } + + if(fontIdRuns != null) fontIdRuns.add(st.getRArray(0).getRPr()); + } /** @@ -164,50 +213,118 @@ public class XSSFRichTextString implements RichTextString { * @param fontIndex the font to apply. */ public void applyFont(short fontIndex) { - applyFont(0, length(), fontIndex); + XSSFFont font; + if(styles == null) { + font = new XSSFFont(); + font.setFontName("#" + fontIndex); + fontIdRuns = new ArrayList(); + } else { + font = (XSSFFont)styles.getFontAt(fontIndex); + } + applyFont(font); } /** - * Removes any formatting that may have been applied to the string. + * Append new text to this text run and apply the specify font to it + * + * @param text the text to append + * @param font the font to apply to the appended text or null if no formatting is required */ - public void clearFormatting() { - for (int i = 0; i < st.sizeOfRArray(); i++) { - st.removeR(i); + public void append(String text, XSSFFont font){ + if(st.sizeOfRArray() == 0 && st.isSetT()) { + //convert string into a text run: string + st.addNewR().setT(st.getT()); + st.unsetT(); } + CTRElt lt = st.addNewR(); + lt.setT(text); + CTRPrElt pr = lt.addNewRPr(); + if(font != null) setRunAttributes(font.getCTFont(), pr); + + if(fontIdRuns != null) fontIdRuns.add(pr); } /** - * Returns the font in use at a particular index. + * Append new text to this text run * - * @param index The index. - * @return The font that's currently being applied at that - * index or null if no font is being applied or the - * index is out of range. + * @param text the text to append */ - public short getFontAtIndex(int index) { - // TODO Auto-generated method stub - return 0; + public void append(String text){ + append(text, null); } /** - * Gets the font used in a particular formatting run. - * - * @param index the index of the formatting run - * @return the font number used. + * Copy font attributes from CTFont bean into CTRPrElt bean */ - public short getFontOfFormattingRun(int index) { - // TODO Auto-generated method stub - return 0; + private void setRunAttributes(CTFont ctFont, CTRPrElt pr){ + if(ctFont.sizeOfBArray() > 0) pr.addNewB().setVal(ctFont.getBArray(0).getVal()); + if(ctFont.sizeOfUArray() > 0) pr.addNewU().setVal(ctFont.getUArray(0).getVal()); + if(ctFont.sizeOfIArray() > 0) pr.addNewI().setVal(ctFont.getIArray(0).getVal()); + if(ctFont.sizeOfColorArray() > 0) { + CTColor c1 = ctFont.getColorArray(0); + CTColor c2 = pr.addNewColor(); + if(c1.isSetAuto()) c2.setAuto(c1.getAuto()); + if(c1.isSetIndexed()) c2.setIndexed(c1.getIndexed()); + if(c1.isSetRgb()) c2.setRgb(c1.getRgb()); + if(c1.isSetTheme()) c2.setTheme(c1.getTheme()); + if(c1.isSetTint()) c2.setTint(c1.getTint()); + } + if(ctFont.sizeOfNameArray() > 0) pr.addNewRFont().setVal(ctFont.getNameArray(0).getVal()); + if(ctFont.sizeOfFamilyArray() > 0) pr.addNewFamily().setVal(ctFont.getFamilyArray(0).getVal()); + if(ctFont.sizeOfSchemeArray() > 0) pr.addNewScheme().setVal(ctFont.getSchemeArray(0).getVal()); + if(ctFont.sizeOfCharsetArray() > 0) pr.addNewCharset().setVal(ctFont.getCharsetArray(0).getVal()); + if(ctFont.sizeOfCondenseArray() > 0) pr.addNewCondense().setVal(ctFont.getCondenseArray(0).getVal()); + if(ctFont.sizeOfExtendArray() > 0) pr.addNewExtend().setVal(ctFont.getExtendArray(0).getVal()); + if(ctFont.sizeOfVertAlignArray() > 0) pr.addNewVertAlign().setVal(ctFont.getVertAlignArray(0).getVal()); + if(ctFont.sizeOfOutlineArray() > 0) pr.addNewOutline().setVal(ctFont.getOutlineArray(0).getVal()); + if(ctFont.sizeOfShadowArray() > 0) pr.addNewShadow().setVal(ctFont.getShadowArray(0).getVal()); + if(ctFont.sizeOfStrikeArray() > 0) pr.addNewStrike().setVal(ctFont.getStrikeArray(0).getVal()); + } + + /** + * Removes any formatting that may have been applied to the string. + */ + public void clearFormatting() { + String text = getString(); + while (st.sizeOfRArray() > 0) { + st.removeR(st.sizeOfRArray()-1); + } + st.setT(text); } /** * The index within the string to which the specified formatting run applies. + * * @param index the index of the formatting run * @return the index within the string. */ public int getIndexOfFormattingRun(int index) { - // TODO Auto-generated method stub - return 0; + if(st.sizeOfRArray() == 0) return 0; + + int pos = 0; + for(int i = 0; i < st.sizeOfRArray(); i++){ + CTRElt r = st.getRArray(i); + if(i == index) return pos; + + pos += r.getT().length(); + } + return -1; + } + + /** + * Returns the number of characters this format run covers. + * + * @param index the index of the formatting run + * @return the number of characters this format run covers + */ + public int getLengthOfFormattingRun(int index) { + if(st.sizeOfRArray() == 0) return length(); + + for(int i = 0; i < st.sizeOfRArray(); i++){ + CTRElt r = st.getRArray(i); + if(i == index) return r.getT().length(); + } + return -1; } /** @@ -255,6 +372,44 @@ public class XSSFRichTextString implements RichTextString { return st.sizeOfRArray(); } + /** + * Gets a copy of the font used in a particular formatting run. + * + * @param index the index of the formatting run + * @return A copy of the font used or null if no formatting is applied to the specified text run. + */ + public XSSFFont getFontOfFormattingRun(int index) { + if(st.sizeOfRArray() == 0) return null; + + for(int i = 0; i < st.sizeOfRArray(); i++){ + CTRElt r = st.getRArray(i); + if(i == index) return new XSSFFont(toCTFont(r.getRPr())); + } + return null; + } + + /** + * Return a copy of the font in use at a particular index. + * + * @param index The index. + * @return A copy of the font that's currently being applied at that + * index or null if no font is being applied or the + * index is out of range. + */ + public XSSFFont getFontAtIndex( int index ) { + if(st.sizeOfRArray() == 0) return null; + + int pos = 0; + for(int i = 0; i < st.sizeOfRArray(); i++){ + CTRElt r = st.getRArray(i); + if(index >= pos && index < pos + r.getT().length()) return new XSSFFont(toCTFont(r.getRPr())); + + pos += r.getT().length(); + } + return null; + + } + /** * Return the underlying xml bean */ @@ -264,5 +419,51 @@ public class XSSFRichTextString implements RichTextString { protected void setStylesTableReference(StylesTable tbl){ styles = tbl; + if(fontIdRuns != null){ + for (CTRPrElt pr : fontIdRuns) { + if(pr.sizeOfRFontArray() > 0 ) { + String fontName = pr.getRFontArray(0).getVal(); + if(fontName.startsWith("#")){ + int idx = Integer.parseInt(fontName.substring(1)); + XSSFFont font = (XSSFFont)styles.getFontAt(idx); + pr.removeRFont(0); + setRunAttributes(font.getCTFont(), pr); + } + } + } + } + } + + /** + * + * CTRPrElt --> CTFont adapter + */ + protected static CTFont toCTFont(CTRPrElt pr){ + CTFont ctFont = CTFont.Factory.newInstance(); + + if(pr.sizeOfBArray() > 0) ctFont.addNewB().setVal(pr.getBArray(0).getVal()); + if(pr.sizeOfUArray() > 0) ctFont.addNewU().setVal(pr.getUArray(0).getVal()); + if(pr.sizeOfIArray() > 0) ctFont.addNewI().setVal(pr.getIArray(0).getVal()); + if(pr.sizeOfColorArray() > 0) { + CTColor c1 = pr.getColorArray(0); + CTColor c2 = ctFont.addNewColor(); + if(c1.isSetAuto()) c2.setAuto(c1.getAuto()); + if(c1.isSetIndexed()) c2.setIndexed(c1.getIndexed()); + if(c1.isSetRgb()) c2.setRgb(c1.getRgb()); + if(c1.isSetTheme()) c2.setTheme(c1.getTheme()); + if(c1.isSetTint()) c2.setTint(c1.getTint()); + } + if(pr.sizeOfRFontArray() > 0) ctFont.addNewName().setVal(pr.getRFontArray(0).getVal()); + if(pr.sizeOfFamilyArray() > 0) ctFont.addNewFamily().setVal(pr.getFamilyArray(0).getVal()); + if(pr.sizeOfSchemeArray() > 0) ctFont.addNewScheme().setVal(pr.getSchemeArray(0).getVal()); + if(pr.sizeOfCharsetArray() > 0) ctFont.addNewCharset().setVal(pr.getCharsetArray(0).getVal()); + if(pr.sizeOfCondenseArray() > 0) ctFont.addNewCondense().setVal(pr.getCondenseArray(0).getVal()); + if(pr.sizeOfExtendArray() > 0) ctFont.addNewExtend().setVal(pr.getExtendArray(0).getVal()); + if(pr.sizeOfVertAlignArray() > 0) ctFont.addNewVertAlign().setVal(pr.getVertAlignArray(0).getVal()); + if(pr.sizeOfOutlineArray() > 0) ctFont.addNewOutline().setVal(pr.getOutlineArray(0).getVal()); + if(pr.sizeOfShadowArray() > 0) ctFont.addNewShadow().setVal(pr.getShadowArray(0).getVal()); + if(pr.sizeOfStrikeArray() > 0) ctFont.addNewStrike().setVal(pr.getStrikeArray(0).getVal()); + + return ctFont; } } diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java index 0e89964b3b..5165a4d630 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java @@ -134,6 +134,8 @@ public class XSSFSheet implements Sheet { if (this.worksheet.getSheetData() == null) { this.worksheet.addNewSheetData(); } + //CTSheetView sheetView = getSheetTypeSheetView(); + //sheetView.setWorkbookViewId(0); initRows(this.worksheet); initColumns(this.worksheet); @@ -984,8 +986,17 @@ public class XSSFSheet implements Sheet { } // HSSFSheet compatibility methods. See also the following zoom related methods + /** + * Sets the zoom magnication for the sheet. The zoom is expressed as a + * fraction. For example to express a zoom of 75% use 3 for the numerator + * and 4 for the denominator. + * + * @param numerator The numerator for the zoom magnification. + * @param denominator The denominator for the zoom magnification. + */ public void setZoom(int numerator, int denominator) { - setZoom((numerator/denominator) * 100); + Float result = new Float(numerator)/new Float(denominator)*100; + setZoom(result.intValue()); } public void setZoom(long scale) { diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java index 53b13ce302..5bc8c14e5d 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java @@ -19,6 +19,7 @@ package org.apache.poi.xssf.usermodel; import java.io.IOException; import java.io.OutputStream; +import java.io.InputStream; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; @@ -102,6 +103,10 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook { public XSSFWorkbook(String path) throws IOException { this(openPackage(path)); } + public XSSFWorkbook(InputStream is) throws IOException { + this(openPackage(is)); + } + public XSSFWorkbook(Package pkg) throws IOException { super(pkg); try { @@ -299,11 +304,11 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook { } - public Font findFont(short boldWeight, short color, short fontHeight, String name, boolean italic, boolean strikeout, short typeOffset, byte underline) { + public XSSFFont findFont(short boldWeight, short color, short fontHeight, String name, boolean italic, boolean strikeout, short typeOffset, byte underline) { short fontNum=getNumberOfFonts(); - for (short i = 0; i <= fontNum; i++) { - XSSFFont xssfFont = (XSSFFont)getFontAt(i); - if (xssfFont.getBoldweight() == boldWeight + for (short i = 0; i < fontNum; i++) { + XSSFFont xssfFont = getFontAt(i); + if ( xssfFont.getBold() == (boldWeight == XSSFFont.BOLDWEIGHT_BOLD) && xssfFont.getColor() == color && xssfFont.getFontHeightInPoints() == fontHeight && xssfFont.getFontName().equals(name) @@ -384,8 +389,8 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook { return (short) getFirstVisibleTab(); } - public Font getFontAt(short idx) { - return stylesSource.getFontAt(idx); + public XSSFFont getFontAt(short idx) { + return (XSSFFont)stylesSource.getFontAt(idx); } public XSSFName getNameAt(int index) { @@ -418,7 +423,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook { public short getNumberOfFonts() { // TODO Auto-generated method stub - return 0; + return (short)((StylesTable)stylesSource).getNumberOfFonts(); } public int getNumberOfNames() { diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/extensions/XSSFCellBorder.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/extensions/XSSFCellBorder.java index 57d476b0d2..d57b824467 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/extensions/XSSFCellBorder.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/extensions/XSSFCellBorder.java @@ -70,7 +70,7 @@ public class XSSFCellBorder { } public void setBorderColor(BorderSide side, XSSFColor color) { - color.setToBorder(getBorder(side)); + getBorder(side).setColor(color.getCTColor()); } private CTBorderPr getBorder(BorderSide side) { diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/extensions/XSSFColor.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/extensions/XSSFColor.java index 1d7835a3c4..8b2d0e7062 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/extensions/XSSFColor.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/extensions/XSSFColor.java @@ -16,61 +16,183 @@ ==================================================================== */ package org.apache.poi.xssf.usermodel.extensions; -import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorderPr; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor; +/** + * Represents a color in SpreadsheetML + */ public class XSSFColor { - private CTColor color; - - public XSSFColor(CTColor color) { - this.color = color; - if (this.color == null) { - this.color = CTColor.Factory.newInstance(); - } + private CTColor ctColor; + + /** + * Create an instance of XSSFColor from the supplied XML bean + */ + public XSSFColor(CTColor color) { + this.ctColor = color; } - public boolean isAuto() { - return color.getAuto(); + /** + * Create an new instance of XSSFColor + */ + public XSSFColor() { + this.ctColor = CTColor.Factory.newInstance(); + } + + /** + * A boolean value indicating the ctColor is automatic and system ctColor dependent. + */ + public boolean isAuto() { + return ctColor.getAuto(); } + /** + * A boolean value indicating the ctColor is automatic and system ctColor dependent. + */ public void setAuto(boolean auto) { - color.setAuto(auto); + ctColor.setAuto(auto); } - - public long getIndexed() { - return color.getIndexed(); + + /** + * Indexed ctColor value. Only used for backwards compatibility. References a ctColor in indexedColors. + */ + public int getIndexed() { + return (int)ctColor.getIndexed(); } - public void setIndexed(long indexed) { - color.setIndexed(indexed); + /** + * Indexed ctColor value. Only used for backwards compatibility. References a ctColor in indexedColors. + */ + public void setIndexed(int indexed) { + ctColor.setIndexed(indexed); } - - public byte[] getRgb() { - return color.getRgb(); + + /** + * Standard Alpha Red Green Blue ctColor value (ARGB). + */ + public byte[] getRgb() { + return ctColor.getRgb(); } + /** + * Standard Alpha Red Green Blue ctColor value (ARGB). + */ public void setRgb(byte[] rgb) { - color.setRgb(rgb); + ctColor.setRgb(rgb); } - - public long getTheme() { - return color.getTheme(); + + /** + * Index into the collection, referencing a particular or + * value expressed in the Theme part. + */ + public int getTheme() { + return (int)ctColor.getTheme(); } - public void setTheme(long theme) { - color.setTheme(theme); + /** + * Index into the collection, referencing a particular or + * value expressed in the Theme part. + */ + public void setTheme(int theme) { + ctColor.setTheme(theme); } - - public double getTint() { - return color.getTint(); + + /** + * Specifies the tint value applied to the ctColor. + * + *

+ * If tint is supplied, then it is applied to the RGB value of the ctColor to determine the final + * ctColor applied. + *

+ *

+ * The tint value is stored as a double from -1.0 .. 1.0, where -1.0 means 100% darken and + * 1.0 means 100% lighten. Also, 0.0 means no change. + *

+ *

+ * In loading the RGB value, it is converted to HLS where HLS values are (0..HLSMAX), where + * HLSMAX is currently 255. + *

+ * Here are some examples of how to apply tint to ctColor: + *
+ *
+     * If (tint < 0)
+     * Lum’ = Lum * (1.0 + tint)
+     *
+     * For example: Lum = 200; tint = -0.5; Darken 50%
+     * Lum‘ = 200 * (0.5) => 100
+     * For example: Lum = 200; tint = -1.0; Darken 100% (make black)
+     * Lum‘ = 200 * (1.0-1.0) => 0
+     * If (tint > 0)
+     * Lum‘ = Lum * (1.0-tint) + (HLSMAX – HLSMAX * (1.0-tint))
+     * For example: Lum = 100; tint = 0.75; Lighten 75%
+     *
+     * Lum‘ = 100 * (1-.75) + (HLSMAX – HLSMAX*(1-.75))
+     * = 100 * .25 + (255 – 255 * .25)
+     * = 25 + (255 – 63) = 25 + 192 = 217
+     * For example: Lum = 100; tint = 1.0; Lighten 100% (make white)
+     * Lum‘ = 100 * (1-1) + (HLSMAX – HLSMAX*(1-1))
+     * = 100 * 0 + (255 – 255 * 0)
+     * = 0 + (255 – 0) = 255
+     * 
+ *
+ * + * @return the tint value + */ + public double getTint() { + return ctColor.getTint(); } + /** + * Specifies the tint value applied to the ctColor. + * + *

+ * If tint is supplied, then it is applied to the RGB value of the ctColor to determine the final + * ctColor applied. + *

+ *

+ * The tint value is stored as a double from -1.0 .. 1.0, where -1.0 means 100% darken and + * 1.0 means 100% lighten. Also, 0.0 means no change. + *

+ *

+ * In loading the RGB value, it is converted to HLS where HLS values are (0..HLSMAX), where + * HLSMAX is currently 255. + *

+ * Here are some examples of how to apply tint to ctColor: + *
+ *
+     * If (tint < 0)
+     * Lum’ = Lum * (1.0 + tint)
+     *
+     * For example: Lum = 200; tint = -0.5; Darken 50%
+     * Lum‘ = 200 * (0.5) => 100
+     * For example: Lum = 200; tint = -1.0; Darken 100% (make black)
+     * Lum‘ = 200 * (1.0-1.0) => 0
+     * If (tint > 0)
+     * Lum‘ = Lum * (1.0-tint) + (HLSMAX – HLSMAX * (1.0-tint))
+     * For example: Lum = 100; tint = 0.75; Lighten 75%
+     *
+     * Lum‘ = 100 * (1-.75) + (HLSMAX – HLSMAX*(1-.75))
+     * = 100 * .25 + (255 – 255 * .25)
+     * = 25 + (255 – 63) = 25 + 192 = 217
+     * For example: Lum = 100; tint = 1.0; Lighten 100% (make white)
+     * Lum‘ = 100 * (1-1) + (HLSMAX – HLSMAX*(1-1))
+     * = 100 * 0 + (255 – 255 * 0)
+     * = 0 + (255 – 0) = 255
+     * 
+ *
+ * + * @param tint the tint value + */ public void setTint(double tint) { - color.setTint(tint); + ctColor.setTint(tint); } - public void setToBorder(CTBorderPr border) { - border.setColor(this.color); - } + /** + * Returns the underlying XML bean + * + * @return the underlying XML bean + */ + public CTColor getCTColor(){ + return ctColor; + } } diff --git a/src/ooxml/java/org/apache/poi/xssf/util/CTFontWrapper.java b/src/ooxml/java/org/apache/poi/xssf/util/CTFontWrapper.java index c772a7566e..e69de29bb2 100755 --- a/src/ooxml/java/org/apache/poi/xssf/util/CTFontWrapper.java +++ b/src/ooxml/java/org/apache/poi/xssf/util/CTFontWrapper.java @@ -1,176 +0,0 @@ -package org.apache.poi.xssf.util; - -import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBooleanProperty; -import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor; -import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFont; -import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFontName; -import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFontScheme; -import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFontSize; -import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTIntProperty; -import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTUnderlineProperty; -import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTVerticalAlignFontProperty; - -/* - * The font element in xml is definited like . - * So in the java object CTFont all methods get and set returns an array of elements also if there is always defined - * only one type of attribute per type. - * This class is made to make simple using method get and set instead of getArray() or set(index,object). - * We consider always the index 0 like the only one index to refer of CT_Font attribute. - * - */ - - -public class CTFontWrapper{ - - private CTFont font; - - - public CTFontWrapper(CTFont font){ - this.font=font; - } - - public CTFont getCTFont(){ - return font; - } - - - public CTBooleanProperty getB(){ - if( font.getBArray().length>0) - return font.getBArray(0); - else - return null; - } - - - public CTIntProperty getCharset(){ - if(font.getCharsetArray().length>0) - return font.getCharsetArray(0); - else - return null; - } - - public CTColor getColor(){ - if(font.getColorArray().length>0) - return font.getColorArray(0); - else - return null; - } - - public CTBooleanProperty getStrike(){ - if(font.getStrikeArray().length>0) - return font.getStrikeArray(0); - else - return null; - } - - public CTVerticalAlignFontProperty getVertAlign() { - if(font.getVertAlignArray().length>0) - return font.getVertAlignArray(0); - else - return null; - } - - public CTFontName setName(){ - if(font.getNameArray().length>0) - return font.getNameArray(0); - else - return null; - } - - public CTFontSize getSz(){ - if(font.getSzArray().length>0) - return font.getSzArray(0); - else - return null; - } - - public CTBooleanProperty getI(){ - if(font.getIArray().length>0) - return font.getIArray(0); - else - return null; - } - - - - public CTUnderlineProperty getU(){ - if(font.getUArray().length>0) - return font.getUArray(0); - else - return null; - } - - public void setB(CTBooleanProperty value){ - font.setBArray(0,value); - } - - public void setCharset(CTIntProperty value){ - font.setCharsetArray(0, value); - } - - public void setColor(CTColor value){ - font.setColorArray(0,value); - } - - public void setFontName(CTFontName value){ - font.setNameArray(0,value); - } - - public void setSz(CTFontSize value){ - font.setSzArray(0,value); - } - public void setI(CTBooleanProperty value){ - font.setIArray(0,value); - } - - public void setU(CTUnderlineProperty value){ - font.setUArray(0,value); - } - - - public void setStrike(CTBooleanProperty value){ - font.setStrikeArray(0,value); - } - - - public void setVertAlign(CTVerticalAlignFontProperty value){ - font.setVertAlignArray(0,value); - } - - - public void setName(CTFontName fontName) { - font.setNameArray(0,fontName); - } - - public CTFontName getName() { - return font.getNameArray(0); - } - - public CTIntProperty getFamily() { - return font.getFamilyArray(0); - } - - public void setFamily(CTIntProperty family) { - font.setFamilyArray(0,family); - } - - - public void setFontScheme(CTFontScheme ctFontScheme) { - font.setSchemeArray(0,ctFontScheme); - } - - public CTFontScheme getFontScheme() { - return font.getSchemeArray(0); - } - - // methods used in FontFormatting - - public CTBooleanProperty getOutline(){ - return font.getOutlineArray(0); - } - - - -} - - \ No newline at end of file diff --git a/src/ooxml/java/org/apache/poi/xssf/util/IndexedColors.java b/src/ooxml/java/org/apache/poi/xssf/util/IndexedColors.java deleted file mode 100755 index b54ee9bae9..0000000000 --- a/src/ooxml/java/org/apache/poi/xssf/util/IndexedColors.java +++ /dev/null @@ -1,16 +0,0 @@ -package org.apache.poi.xssf.util; - -public class IndexedColors { - - public static int BLACK=0; - public static int WHITE=1; - public static int RED=2; - public static int GREEN=3; - public static int BLUE=4; - public static int YELLOW=5; - public static int PINK=6; - - public static int LIGHT_GREY=22; - public static int DARK_GREY=23; - -} diff --git a/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFExcelExtractor.java b/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFExcelExtractor.java index 1f170d2790..c389e63fa8 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFExcelExtractor.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFExcelExtractor.java @@ -132,8 +132,6 @@ public final class TestXSSFExcelExtractor extends TestCase { POITextExtractor extractor = extractors[i]; String text = extractor.getText().replaceAll("[\r\t]", ""); - //System.out.println(text.length()); - //System.out.println(text); assertTrue(text.startsWith("First Sheet\nTest spreadsheet\n2nd row2nd row 2nd column\n")); Pattern pattern = Pattern.compile(".*13(\\.0+)?\\s+Sheet3.*", Pattern.DOTALL); Matcher m = pattern.matcher(text); diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCell.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCell.java index 58b8ee730f..a83d4a0355 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCell.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCell.java @@ -32,6 +32,7 @@ import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.XSSFTestDataSamples; import org.apache.poi.xssf.model.CommentsTable; +import org.apache.poi.xssf.model.SharedStringsTable; import org.apache.poi.xssf.model.SharedStringSource; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment; @@ -336,7 +337,7 @@ public final class TestXSSFCell extends TestCase { private static XSSFRow createParentObjects() { XSSFWorkbook wb = new XSSFWorkbook(); - wb.setSharedStringSource(new DummySharedStringSource()); + wb.setSharedStringSource(new SharedStringsTable()); XSSFSheet sheet = new XSSFSheet(wb); XSSFRow row = new XSSFRow(sheet); return row; diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFFont.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFFont.java index 18557639e0..ced39c2b71 100755 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFFont.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFFont.java @@ -1,5 +1,7 @@ package org.apache.poi.xssf.usermodel; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; @@ -11,9 +13,8 @@ import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.Font; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; -import org.apache.poi.xssf.util.CTFontWrapper; import org.apache.poi.xssf.util.Charset; -import org.apache.poi.xssf.util.IndexedColors; +import org.openxml4j.opc.Package; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBooleanProperty; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFont; @@ -38,60 +39,51 @@ public class TestXSSFFont extends TestCase{ public void testBoldweight(){ CTFont ctFont=CTFont.Factory.newInstance(); - CTFontWrapper wrapper=new CTFontWrapper(ctFont); - - CTBooleanProperty bool=wrapper.getCTFont().addNewB(); + CTBooleanProperty bool=ctFont.addNewB(); bool.setVal(false); - wrapper.setB(bool); - + ctFont.setBArray(0,bool); XSSFFont xssfFont=new XSSFFont(ctFont); - assertEquals(Font.BOLDWEIGHT_NORMAL, xssfFont.getBoldweight()); + assertEquals(false, xssfFont.getBold()); - xssfFont.setBoldweight(Font.BOLDWEIGHT_BOLD); + xssfFont.setBold(true); assertEquals(ctFont.getBArray().length,1); assertEquals(true, ctFont.getBArray(0).getVal()); - assertEquals(true,wrapper.getB().getVal()); - } public void testCharSet(){ CTFont ctFont=CTFont.Factory.newInstance(); - CTFontWrapper wrapper=new CTFontWrapper(ctFont); CTIntProperty prop=ctFont.addNewCharset(); prop.setVal(Charset.ANSI_CHARSET); - - wrapper.setCharset(prop); + + ctFont.setCharsetArray(0,prop); XSSFFont xssfFont=new XSSFFont(ctFont); assertEquals(Font.ANSI_CHARSET,xssfFont.getCharSet()); - + xssfFont.setCharSet(Font.DEFAULT_CHARSET); - assertEquals(Charset.DEFAULT_CHARSET, wrapper.getCharset().getVal()); + assertEquals(Charset.DEFAULT_CHARSET,ctFont.getCharsetArray(0).getVal()); } public void testFontName(){ CTFont ctFont=CTFont.Factory.newInstance(); - CTFontWrapper wrapper=new CTFontWrapper(ctFont); CTFontName fname=ctFont.addNewName(); fname.setVal("Arial"); - wrapper.setFontName(fname); + ctFont.setNameArray(0,fname); XSSFFont xssfFont=new XSSFFont(ctFont); assertEquals("Arial", xssfFont.getFontName()); xssfFont.setFontName("Courier"); - assertEquals("Courier",wrapper.getName().getVal()); + assertEquals("Courier",ctFont.getNameArray(0).getVal()); } public void testItalic(){ CTFont ctFont=CTFont.Factory.newInstance(); - CTFontWrapper wrapper=new CTFontWrapper(ctFont); - - CTBooleanProperty bool=wrapper.getCTFont().addNewI(); + CTBooleanProperty bool=ctFont.addNewI(); bool.setVal(false); - wrapper.setI(bool); + ctFont.setIArray(0,bool); XSSFFont xssfFont=new XSSFFont(ctFont); assertEquals(false, xssfFont.getItalic()); @@ -99,17 +91,15 @@ public class TestXSSFFont extends TestCase{ xssfFont.setItalic(true); assertEquals(ctFont.getIArray().length,1); assertEquals(true, ctFont.getIArray(0).getVal()); - assertEquals(true,wrapper.getI().getVal()); + assertEquals(true,ctFont.getIArray(0).getVal()); } public void testStrikeout(){ CTFont ctFont=CTFont.Factory.newInstance(); - CTFontWrapper wrapper=new CTFontWrapper(ctFont); - - CTBooleanProperty bool=wrapper.getCTFont().addNewStrike(); + CTBooleanProperty bool=ctFont.addNewStrike(); bool.setVal(false); - wrapper.setStrike(bool); + ctFont.setStrikeArray(0,bool); XSSFFont xssfFont=new XSSFFont(ctFont); assertEquals(false, xssfFont.getStrikeout()); @@ -117,139 +107,218 @@ public class TestXSSFFont extends TestCase{ xssfFont.setStrikeout(true); assertEquals(ctFont.getStrikeArray().length,1); assertEquals(true, ctFont.getStrikeArray(0).getVal()); - assertEquals(true,wrapper.getStrike().getVal()); + assertEquals(true,ctFont.getStrikeArray(0).getVal()); } public void testFontHeight(){ CTFont ctFont=CTFont.Factory.newInstance(); - CTFontWrapper wrapper=new CTFontWrapper(ctFont); CTFontSize size=ctFont.addNewSz(); size.setVal(11); - wrapper.setSz(size); - + ctFont.setSzArray(0,size); + XSSFFont xssfFont=new XSSFFont(ctFont); assertEquals(11/20,xssfFont.getFontHeight()); - + xssfFont.setFontHeight((short)20); - assertEquals(new Double(20*20).doubleValue(),wrapper.getSz().getVal()); } + assertEquals(new Double(20*20).doubleValue(),ctFont.getSzArray(0).getVal()); + } public void testFontHeightInPoint(){ CTFont ctFont=CTFont.Factory.newInstance(); - CTFontWrapper wrapper=new CTFontWrapper(ctFont); CTFontSize size=ctFont.addNewSz(); size.setVal(14); - wrapper.setSz(size); - + ctFont.setSzArray(0,size); + XSSFFont xssfFont=new XSSFFont(ctFont); assertEquals(14,xssfFont.getFontHeightInPoints()); - + xssfFont.setFontHeightInPoints((short)20); - assertEquals(new Double(20).doubleValue(),wrapper.getSz().getVal()); + assertEquals(new Double(20).doubleValue(),ctFont.getSzArray(0).getVal()); } public void testUnderline(){ CTFont ctFont=CTFont.Factory.newInstance(); - CTFontWrapper wrapper=new CTFontWrapper(ctFont); - - CTUnderlineProperty underlinePropr=wrapper.getCTFont().addNewU(); + CTUnderlineProperty underlinePropr=ctFont.addNewU(); underlinePropr.setVal(STUnderlineValues.SINGLE); - wrapper.setU(underlinePropr); + ctFont.setUArray(0,underlinePropr); XSSFFont xssfFont=new XSSFFont(ctFont); assertEquals(Font.U_SINGLE, xssfFont.getUnderline()); xssfFont.setUnderline(Font.U_DOUBLE); assertEquals(ctFont.getUArray().length,1); - assertEquals(STUnderlineValues.DOUBLE,wrapper.getU().getVal()); - } + assertEquals(STUnderlineValues.DOUBLE,ctFont.getUArray(0).getVal()); + } public void testColor(){ CTFont ctFont=CTFont.Factory.newInstance(); - CTFontWrapper wrapper=new CTFontWrapper(ctFont); CTColor color=ctFont.addNewColor(); - //color.setIndexed(IndexedColors.DEFAULT_COLOR); color.setIndexed(XSSFFont.DEFAULT_FONT_COLOR); - wrapper.setColor(color); - + ctFont.setColorArray(0,color); + XSSFFont xssfFont=new XSSFFont(ctFont); assertEquals(Font.COLOR_NORMAL,xssfFont.getColor()); - - xssfFont.setColor(Font.COLOR_RED); - assertEquals(IndexedColors.RED,wrapper.getColor().getIndexed()); + + xssfFont.setColor(IndexedColors.RED.getIndex()); + assertEquals(IndexedColors.RED.getIndex(), ctFont.getColorArray(0).getIndexed()); + } +/* + public void testRgbColor(){ + CTFont ctFont=CTFont.Factory.newInstance(); + CTColor color=ctFont.addNewColor(); + color.setRgb(new byte[]{}); + ctFont.setColorArray(0,color); + + XSSFFont xssfFont=new XSSFFont(ctFont); + assertEquals(,xssfFont.getRgbColor()); + + xssfFont.setRgbColor(new XSSFColor(new java.awt.Color(10,19,10))); + //assertEquals(,ctFont.getColorArray(0).getRgb()); } + public void testThemeColor(){ + CTFont ctFont=CTFont.Factory.newInstance(); + CTColor color=ctFont.addNewColor(); + color.setTheme(); + ctFont.setColorArray(0,color); + + XSSFFont xssfFont=new XSSFFont(ctFont); + assertEquals(,xssfFont.getThemeColor()); + xssfFont.setThemeColor(Font.COLOR_RED); + assertEquals(,ctFont.getColorArray(0).getTheme()); + assertEquals(,ctFont.getColorArray(0).getTint()); + } +*/ public void testFamily(){ CTFont ctFont=CTFont.Factory.newInstance(); - CTFontWrapper wrapper=new CTFontWrapper(ctFont); CTIntProperty family=ctFont.addNewFamily(); family.setVal(XSSFFont.FONT_FAMILY_MODERN); - wrapper.setFamily(family); - + ctFont.setFamilyArray(0,family); + XSSFFont xssfFont=new XSSFFont(ctFont); assertEquals(XSSFFont.FONT_FAMILY_MODERN,xssfFont.getFamily()); - } - + public void testScheme(){ CTFont ctFont=CTFont.Factory.newInstance(); - CTFontWrapper wrapper=new CTFontWrapper(ctFont); CTFontScheme scheme=ctFont.addNewScheme(); scheme.setVal(STFontScheme.MAJOR); - wrapper.setFontScheme(scheme); - + ctFont.setSchemeArray(0,scheme); + XSSFFont font=new XSSFFont(ctFont); assertEquals(XSSFFont.SCHEME_MAJOR,font.getScheme()); - + font.setScheme(XSSFFont.SCHEME_NONE); - assertEquals(STFontScheme.NONE,wrapper.getFontScheme().getVal()); + assertEquals(STFontScheme.NONE,ctFont.getSchemeArray(0).getVal()); } public void testTypeOffset(){ CTFont ctFont=CTFont.Factory.newInstance(); - CTFontWrapper wrapper=new CTFontWrapper(ctFont); CTVerticalAlignFontProperty valign=ctFont.addNewVertAlign(); valign.setVal(STVerticalAlignRun.BASELINE); - wrapper.setVertAlign(valign); - + ctFont.setVertAlignArray(0,valign); + XSSFFont font=new XSSFFont(ctFont); assertEquals(Font.SS_NONE,font.getTypeOffset()); - + font.setTypeOffset(XSSFFont.SS_SUPER); - assertEquals(STVerticalAlignRun.SUPERSCRIPT,wrapper.getVertAlign().getVal()); + assertEquals(STVerticalAlignRun.SUPERSCRIPT,ctFont.getVertAlignArray(0).getVal()); } - + + /** + * Tests that we can define fonts to a new + * file, save, load, and still see them + * @throws Exception + */ + public void testCreateSave() throws Exception { + XSSFWorkbook wb = new XSSFWorkbook(); + XSSFSheet s1 = (XSSFSheet)wb.createSheet(); + Row r1 = s1.createRow(0); + Cell r1c1 = r1.createCell(0); + r1c1.setCellValue(2.2); + + assertEquals(1, wb.getNumberOfFonts()); + + XSSFFont font=wb.createFont(); + font.setBold(true); + font.setStrikeout(true); + font.setColor(IndexedColors.YELLOW.getIndex()); + font.setFontName("Courier"); + wb.createCellStyle().setFont(font); + assertEquals(2, wb.getNumberOfFonts()); + + CellStyle cellStyleTitle=wb.createCellStyle(); + cellStyleTitle.setFont(font); + r1c1.setCellStyle(cellStyleTitle); + + // Save and re-load + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + wb.write(baos); + ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); + + wb = new XSSFWorkbook(Package.open(bais)); + s1 = (XSSFSheet)wb.getSheetAt(0); + + assertEquals(2, wb.getNumberOfFonts()); + assertNotNull(s1.getRow(0).getCell(0).getCellStyle().getFont(wb)); + assertEquals(IndexedColors.YELLOW.getIndex(), s1.getRow(0).getCell(0).getCellStyle().getFont(wb).getColor()); + assertEquals("Courier", s1.getRow(0).getCell(0).getCellStyle().getFont(wb).getFontName()); + + // Now add an orphaned one + XSSFFont font2 = wb.createFont(); + font2.setItalic(true); + font2.setFontHeightInPoints((short)15); + wb.createCellStyle().setFont(font2); + assertEquals(3, wb.getNumberOfFonts()); + + // Save and re-load + baos = new ByteArrayOutputStream(); + wb.write(baos); + bais = new ByteArrayInputStream(baos.toByteArray()); + + wb = new XSSFWorkbook(Package.open(bais)); + s1 = (XSSFSheet)wb.getSheetAt(0); + + assertEquals(3, wb.getNumberOfFonts()); + assertNotNull(wb.getFontAt((short)1)); + assertNotNull(wb.getFontAt((short)2)); + + assertEquals(15, wb.getFontAt((short)2).getFontHeightInPoints()); + assertEquals(true, wb.getFontAt((short)2).getItalic()); + } + public void testXSSFFont() throws IOException{ XSSFWorkbook workbook=new XSSFWorkbook(); //Font font1=workbook.createFont(); - + Sheet sheet=workbook.createSheet("sheet 1 - test font"); - - + + Row row=sheet.createRow(0); Cell cell=row.createCell(0); cell.setCellValue(new XSSFRichTextString("XSSFFont test example file")); - Font font=new XSSFFont(); - font.setBoldweight(Font.BOLDWEIGHT_BOLD); + XSSFFont font=new XSSFFont(); + font.setBold(true); font.setFontHeightInPoints((short)22); - font.setColor((short)IndexedColors.BLUE); + font.setColor(IndexedColors.BLUE.getIndex()); font.setFontName("Verdana"); CellStyle cellStyleTitle=workbook.createCellStyle(); cellStyleTitle.setFont(font); cell.setCellStyle(cellStyleTitle); - + row=sheet.createRow(3); - Font font1=new XSSFFont(); - font1.setBoldweight(Font.BOLDWEIGHT_BOLD); + XSSFFont font1=new XSSFFont(); + font1.setBold(true); font1.setItalic(true); font1.setFontHeightInPoints((short)18); - font1.setColor(Font.COLOR_RED); + font1.setColor(IndexedColors.RED.getIndex()); font1.setFontName("Arial"); CellStyle cellStyle1=workbook.createCellStyle(); cellStyle1.setFont(font1); @@ -279,7 +348,7 @@ public class TestXSSFFont extends TestCase{ font3.setFontHeightInPoints((short)9); font3.setFontName("Times"); font3.setStrikeout(true); - font3.setColor((short)IndexedColors.PINK); + font3.setColor(IndexedColors.PINK.getIndex()); CellStyle cellStyle3=workbook.createCellStyle(); cellStyle3.setFont(font3); diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFRichTextString.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFRichTextString.java new file mode 100755 index 0000000000..a716d812d0 --- /dev/null +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFRichTextString.java @@ -0,0 +1,135 @@ +/* ==================================================================== + 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; + +import java.io.*; +import java.util.ArrayList; +import java.util.Calendar; +import java.util.Date; + +import junit.framework.TestCase; + +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.CellStyle; +import org.apache.poi.ss.usermodel.Comment; +import org.apache.poi.ss.usermodel.CreationHelper; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.SharedStringSource; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.xssf.model.CommentsTable; +import org.apache.xmlbeans.XmlOptions; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.*; + +/** + * Tests functionality of the XSSFRichTextRun object + * + * @author Yegor Kozlov + */ +public class TestXSSFRichTextString extends TestCase { + + public void testCreate() throws Exception { + + XSSFRichTextString rt = new XSSFRichTextString("Apache POI"); + assertEquals("Apache POI", rt.getString()); + + CTRst st = rt.getCTRst(); + assertTrue(st.isSetT()); + assertEquals("Apache POI", st.getT()); + + rt.append(" is cool stuff"); + assertEquals(2, st.sizeOfRArray()); + assertFalse(st.isSetT()); + + assertEquals("Apache POI is cool stuff", rt.getString()); + } + + + public void testApplyFont() throws Exception { + + XSSFRichTextString rt = new XSSFRichTextString(); + rt.append("123"); + rt.append("4567"); + rt.append("89"); + + XSSFFont font1 = new XSSFFont(); + font1.setBold(true); + + rt.applyFont(2, 5, font1); + + assertEquals(4, rt.numFormattingRuns()); + assertEquals(0, rt.getIndexOfFormattingRun(0)); + assertEquals(2, rt.getLengthOfFormattingRun(0)); + + assertEquals(2, rt.getIndexOfFormattingRun(1)); + assertEquals(3, rt.getLengthOfFormattingRun(1)); + + assertEquals(5, rt.getIndexOfFormattingRun(2)); + assertEquals(2, rt.getLengthOfFormattingRun(2)); + + assertEquals(7, rt.getIndexOfFormattingRun(3)); + assertEquals(2, rt.getLengthOfFormattingRun(3)); + } + + public void testClearFormatting() throws Exception { + + XSSFRichTextString rt = new XSSFRichTextString("Apache POI"); + assertEquals("Apache POI", rt.getString()); + + rt.clearFormatting(); + + CTRst st = rt.getCTRst(); + assertTrue(st.isSetT()); + assertEquals("Apache POI", rt.getString()); + assertEquals(0, rt.numFormattingRuns()); + + XSSFFont font = new XSSFFont(); + font.setBold(true); + + rt.applyFont(7, 10, font); + assertEquals(2, rt.numFormattingRuns()); + rt.clearFormatting(); + assertEquals("Apache POI", rt.getString()); + assertEquals(0, rt.numFormattingRuns()); + + } + + public void testGetFonts() throws Exception { + + XSSFRichTextString rt = new XSSFRichTextString(); + + XSSFFont font1 = new XSSFFont(); + font1.setFontName("Arial"); + font1.setItalic(true); + rt.append("The quick", font1); + + XSSFFont font1$ = rt.getFontOfFormattingRun(0); + assertEquals(font1.getItalic(), font1$.getItalic()); + assertEquals(font1.getFontName(), font1$.getFontName()); + + XSSFFont font2 = new XSSFFont(); + font2.setFontName("Courier"); + font2.setBold(true); + rt.append(" brown fox", font2); + + XSSFFont font2$ = rt.getFontOfFormattingRun(1); + assertEquals(font2.getBold(), font2$.getBold()); + assertEquals(font2.getFontName(), font2$.getFontName()); + + } +} diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFRow.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFRow.java index a682bab1c6..2fbec02863 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFRow.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFRow.java @@ -24,6 +24,7 @@ import junit.framework.TestCase; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.xssf.usermodel.TestXSSFCell.DummySharedStringSource; +import org.apache.poi.xssf.model.SharedStringsTable; /** * Tests for XSSFRow @@ -167,7 +168,7 @@ public final class TestXSSFRow extends TestCase { row.setZeroHeight(true); assertTrue(row.getZeroHeight()); } - + /** * Tests for the missing/blank cell policy stuff */ @@ -184,7 +185,7 @@ public final class TestXSSFRow extends TestCase { row.createCell((short)1).setCellValue(3.2); row.createCell((short)4, Cell.CELL_TYPE_BLANK); row.createCell((short)5).setCellValue(4); - + // First up, no policy assertEquals(Cell.CELL_TYPE_STRING, row.getCell(0).getCellType()); assertEquals(Cell.CELL_TYPE_NUMERIC, row.getCell(1).getCellType()); @@ -192,7 +193,7 @@ public final class TestXSSFRow extends TestCase { assertEquals(null, row.getCell(3)); assertEquals(Cell.CELL_TYPE_BLANK, row.getCell(4).getCellType()); assertEquals(Cell.CELL_TYPE_NUMERIC, row.getCell(5).getCellType()); - + // RETURN_NULL_AND_BLANK - same as default assertEquals(Cell.CELL_TYPE_STRING, row.getCell(0, Row.RETURN_NULL_AND_BLANK).getCellType()); assertEquals(Cell.CELL_TYPE_NUMERIC, row.getCell(1, Row.RETURN_NULL_AND_BLANK).getCellType()); @@ -200,7 +201,7 @@ public final class TestXSSFRow extends TestCase { assertEquals(null, row.getCell(3, Row.RETURN_NULL_AND_BLANK)); assertEquals(Cell.CELL_TYPE_BLANK, row.getCell(4, Row.RETURN_NULL_AND_BLANK).getCellType()); assertEquals(Cell.CELL_TYPE_NUMERIC, row.getCell(5, Row.RETURN_NULL_AND_BLANK).getCellType()); - + // RETURN_BLANK_AS_NULL - nearly the same assertEquals(Cell.CELL_TYPE_STRING, row.getCell(0, XSSFRow.RETURN_BLANK_AS_NULL).getCellType()); assertEquals(Cell.CELL_TYPE_NUMERIC, row.getCell(1, XSSFRow.RETURN_BLANK_AS_NULL).getCellType()); @@ -208,7 +209,7 @@ public final class TestXSSFRow extends TestCase { assertEquals(null, row.getCell(3, XSSFRow.RETURN_BLANK_AS_NULL)); assertEquals(null, row.getCell(4, XSSFRow.RETURN_BLANK_AS_NULL)); assertEquals(Cell.CELL_TYPE_NUMERIC, row.getCell(5, XSSFRow.RETURN_BLANK_AS_NULL).getCellType()); - + // CREATE_NULL_AS_BLANK - creates as needed assertEquals(Cell.CELL_TYPE_STRING, row.getCell(0, XSSFRow.CREATE_NULL_AS_BLANK).getCellType()); assertEquals(Cell.CELL_TYPE_NUMERIC, row.getCell(1, XSSFRow.CREATE_NULL_AS_BLANK).getCellType()); @@ -216,7 +217,7 @@ public final class TestXSSFRow extends TestCase { assertEquals(Cell.CELL_TYPE_BLANK, row.getCell(3, XSSFRow.CREATE_NULL_AS_BLANK).getCellType()); assertEquals(Cell.CELL_TYPE_BLANK, row.getCell(4, XSSFRow.CREATE_NULL_AS_BLANK).getCellType()); assertEquals(Cell.CELL_TYPE_NUMERIC, row.getCell(5, XSSFRow.CREATE_NULL_AS_BLANK).getCellType()); - + // Check created ones get the right column assertEquals((short)0, row.getCell(0, XSSFRow.CREATE_NULL_AS_BLANK).getCellNum()); assertEquals((short)1, row.getCell(1, XSSFRow.CREATE_NULL_AS_BLANK).getCellNum()); @@ -244,7 +245,7 @@ public final class TestXSSFRow extends TestCase { private static XSSFSheet createParentObjects() { XSSFWorkbook wb = new XSSFWorkbook(); - wb.setSharedStringSource(new DummySharedStringSource()); + wb.setSharedStringSource(new SharedStringsTable()); return new XSSFSheet(wb); } } diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java index d5c892c584..e06340db05 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java @@ -639,12 +639,10 @@ public class TestXSSFSheet extends TestCase { } public void testGetActiveCell() { - Workbook workbook = new XSSFWorkbook(); - CTSheet ctSheet = CTSheet.Factory.newInstance(); - CTWorksheet ctWorksheet = CTWorksheet.Factory.newInstance(); - XSSFSheet sheet = new XSSFSheet(ctSheet, ctWorksheet, (XSSFWorkbook) workbook); - ctWorksheet.addNewSheetViews().addNewSheetView().addNewSelection().setActiveCell("R5"); - + XSSFWorkbook workbook = new XSSFWorkbook(); + XSSFSheet sheet = workbook.createSheet(); + sheet.setActiveCell("R5"); + assertEquals("R5", sheet.getActiveCell()); } @@ -733,7 +731,6 @@ public class TestXSSFSheet extends TestCase { assertEquals(1, ctWorksheet.getColsArray(0).getColArray(0).getStyle()); XSSFRow row = (XSSFRow) sheet.createRow(0); XSSFCell cell = (XSSFCell) sheet.getRow(0).createCell(3); - //System.out.println(cell.getCellStyle()); } @@ -772,7 +769,6 @@ public class TestXSSFSheet extends TestCase { XSSFSheet sheet = new XSSFSheet(ctSheet, ctWorksheet, (XSSFWorkbook) workbook); //one level - System.out.println("livello 1"); sheet.groupColumn((short)2,(short)7); sheet.groupColumn((short)10,(short)11); CTCols cols=sheet.getWorksheet().getColsArray(0); @@ -784,7 +780,6 @@ public class TestXSSFSheet extends TestCase { assertEquals(1, colArray[0].getOutlineLevel()); //two level - System.out.println("\n livello 2"); sheet.groupColumn((short)1,(short)2); cols=sheet.getWorksheet().getColsArray(0); assertEquals(4,cols.sizeOfColArray()); @@ -792,7 +787,6 @@ public class TestXSSFSheet extends TestCase { assertEquals(2, colArray[1].getOutlineLevel()); //three level - System.out.println("\n livello 3"); sheet.groupColumn((short)6,(short)8); sheet.groupColumn((short)2,(short)3); cols=sheet.getWorksheet().getColsArray(0); @@ -855,5 +849,13 @@ public class TestXSSFSheet extends TestCase { assertEquals(1,sheet.getSheetTypeSheetFormatPr().getOutlineLevelRow()); } + + public void testSetZoom() { + Workbook workBook = new XSSFWorkbook(); + XSSFSheet sheet1 = (XSSFSheet) workBook.createSheet("new sheet"); + sheet1.setZoom(3,4); // 75 percent magnification + long zoom = sheet1.getSheetTypeSheetView().getZoomScale(); + assertEquals(zoom, 75); + } } diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java index 87cd37035e..65a934304b 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java @@ -247,7 +247,7 @@ public class TestXSSFWorkbook extends TestCase { //get default font, then change 2 values and check against different values (height changes) Font font=workbook.createFont(); - font.setBoldweight(Font.BOLDWEIGHT_BOLD); + ((XSSFFont)font).setBold(true); font.setUnderline(Font.U_DOUBLE); StylesSource styleSource=new StylesTable(); long index=styleSource.putFont(font); @@ -291,6 +291,25 @@ public class TestXSSFWorkbook extends TestCase { assertNotNull(fontAt); } + public void testGetNumberOfFonts(){ + XSSFWorkbook wb = new XSSFWorkbook(); + + XSSFFont f1=wb.createFont(); + f1.setBold(true); + wb.createCellStyle().setFont(f1); + + XSSFFont f2=wb.createFont(); + f2.setUnderline(Font.U_DOUBLE); + wb.createCellStyle().setFont(f2); + + XSSFFont f3=wb.createFont(); + f3.setFontHeightInPoints((short)23); + wb.createCellStyle().setFont(f3); + + assertEquals(4,wb.getNumberOfFonts()); + assertEquals(Font.U_DOUBLE,wb.getFontAt((short)2).getUnderline()); + } + public void testGetNumCellStyles(){ XSSFWorkbook workbook = new XSSFWorkbook(); short i = workbook.getNumCellStyles();