diff --git a/src/examples/src/org/apache/poi/xssf/eventusermodel/examples/FromHowTo.java b/src/examples/src/org/apache/poi/xssf/eventusermodel/examples/FromHowTo.java index f298ee10f..2c145bfe1 100644 --- a/src/examples/src/org/apache/poi/xssf/eventusermodel/examples/FromHowTo.java +++ b/src/examples/src/org/apache/poi/xssf/eventusermodel/examples/FromHowTo.java @@ -21,6 +21,7 @@ import java.util.Iterator; import org.apache.poi.xssf.eventusermodel.XSSFReader; import org.apache.poi.xssf.model.SharedStringsTable; +import org.apache.poi.xssf.usermodel.XSSFRichTextString; import org.openxml4j.opc.Package; import org.xml.sax.Attributes; import org.xml.sax.ContentHandler; @@ -113,7 +114,7 @@ public class FromHowTo { // Do now, as characters() may be called more than once if(nextIsString) { int idx = Integer.parseInt(lastContents); - lastContents = sst.getSharedStringAt(idx); + lastContents = new XSSFRichTextString(sst.getEntryAt(idx)).toString(); } // v => contents of a cell 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 7430cfad2..df0ba9ed0 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 @@ -100,18 +100,6 @@ public interface RichTextString { */ short getFontOfFormattingRun(int index); - /** - * Compares one rich text string to another. - */ - int compareTo(Object o); - - boolean equals(Object o); - - /** - * @return the plain text representation of this string. - */ - String toString(); - /** * Applies the specified font to the entire string. * diff --git a/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/SharedStringSource.java b/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/SharedStringSource.java index 2f01b227b..6d56abc2c 100644 --- a/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/SharedStringSource.java +++ b/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/SharedStringSource.java @@ -22,19 +22,4 @@ package org.apache.poi.ss.usermodel; */ public interface SharedStringSource { - /** - * Return the string at position idx (0-based) in this source. - * - * @param idx String position. - * @return The string, or null if not found. - */ - public String getSharedStringAt(int idx); - - /** - * Store a string in this source. - * - * @param s The string to store. - * @return The 0-based position of the newly added string. - */ - public int putSharedString(String s); } diff --git a/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Sheet.java b/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Sheet.java index b04b5d453..18ec51b8a 100644 --- a/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Sheet.java +++ b/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Sheet.java @@ -450,13 +450,6 @@ public interface Sheet extends Iterable { */ boolean getScenarioProtect(); - /** - * Sets the protection on enabled or disabled - * @param protect true => protection enabled; false => protection disabled - * @deprecated use protectSheet(String, boolean, boolean) - */ - void setProtect(boolean protect); - /** * Sets the protection enabled as well as the password * @param password to set for protection diff --git a/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Workbook.java b/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Workbook.java index caafc634d..bcf1b4ee9 100644 --- a/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Workbook.java +++ b/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Workbook.java @@ -94,35 +94,13 @@ public interface Workbook { short getDisplayedTab(); /** - * @deprecated POI will now properly handle unicode strings without - * forceing an encoding - */ - public final static byte ENCODING_COMPRESSED_UNICODE = 0; - - /** - * @deprecated POI will now properly handle unicode strings without - * forceing an encoding - */ - public final static byte ENCODING_UTF_16 = 1; - - /** - * set the sheet name. + * set the sheet name. * Will throw IllegalArgumentException if the name is greater than 31 chars * or contains /\?*[] * @param sheet number (0 based) */ void setSheetName(int sheet, String name); - /** - * set the sheet name forcing the encoding. Forcing the encoding IS A BAD IDEA!!! - * @deprecated 3-Jan-2006 POI now automatically detects unicode and sets the encoding - * appropriately. Simply use setSheetName(int sheet, String encoding) - * @throws IllegalArgumentException if the name is greater than 31 chars - * or contains /\?*[] - * @param sheet number (0 based) - */ - void setSheetName(int sheet, String name, short encoding); - /** * get the sheet name * @param sheet Number @@ -147,11 +125,11 @@ public interface Workbook { * Returns the external sheet index of the sheet * with the given internal index, creating one * if needed. - * Used by some of the more obscure formula and + * Used by some of the more obscure formula and * named range things. */ int getExternalSheetIndex(int internalSheetIndex); - + /** * create an HSSFSheet for this HSSFWorkbook, adds it to the sheets and returns * the high level representation. Use this to create new sheets. @@ -185,7 +163,7 @@ public interface Workbook { */ int getNumberOfSheets(); - + /** * Finds the sheet index for a particular external sheet number. * @param externSheetNumber The external sheet number to convert @@ -338,16 +316,6 @@ public interface Workbook { byte[] getBytes(); - /** @deprecated Do not call this method from your applications. Use the methods - * available in the HSSFRow to add string HSSFCells - */ - int addSSTString(String string); - - /** @deprecated Do not call this method from your applications. Use the methods - * available in the HSSFRow to get string HSSFCells - */ - String getSSTString(int index); - /** gets the total number of named ranges in the workboko * @return number of named ranges */ 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 70d201b38..1712e3c94 100644 --- a/src/ooxml/java/org/apache/poi/xssf/model/SharedStringsTable.java +++ b/src/ooxml/java/org/apache/poi/xssf/model/SharedStringsTable.java @@ -20,26 +20,67 @@ package org.apache.poi.xssf.model; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; -import java.util.LinkedList; +import java.util.*; import org.apache.poi.ss.usermodel.SharedStringSource; +import org.apache.poi.xssf.usermodel.XSSFRichTextString; import org.apache.xmlbeans.XmlException; import org.apache.xmlbeans.XmlOptions; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSst; import org.openxmlformats.schemas.spreadsheetml.x2006.main.SstDocument; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRElt; /** * Table of strings shared across all sheets in a workbook. - * - * @version $Id$ + *

+ * A workbook may contain thousands of cells containing string (non-numeric) data. Furthermore this data is very + * likely to be repeated across many rows or columns. The goal of implementing a single string table that is shared + * across the workbook is to improve performance in opening and saving the file by only reading and writing the + * repetitive information once. + *

+ *

+ * Consider for example a workbook summarizing information for cities within various countries. There may be a + * column for the name of the country, a column for the name of each city in that country, and a column + * containing the data for each city. In this case the country name is repetitive, being duplicated in many cells. + * In many cases the repetition is extensive, and a tremendous savings is realized by making use of a shared string + * table when saving the workbook. When displaying text in the spreadsheet, the cell table will just contain an + * index into the string table as the value of a cell, instead of the full string. + *

+ *

+ * The shared string table contains all the necessary information for displaying the string: the text, formatting + * properties, and phonetic properties (for East Asian languages). + *

+ * + * @author Nick Birch + * @author Yegor Kozlov */ public class SharedStringsTable implements SharedStringSource, XSSFModel { - private final LinkedList strings = new LinkedList(); - private SstDocument doc; - + /** + * Array of individual string items in the Shared String table. + */ + private final List strings = new ArrayList(); + + /** + * Maps strings and their indexes in the strings arrays + */ + private final Map stmap = new HashMap(); + + /** + * An integer representing the total count of strings in the workbook. This count does not + * include any numbers, it counts only the total of text strings in the workbook. + */ + private int count; + + /** + * An integer representing the total count of unique strings in the Shared String Table. + * A string is unique even if it is a copy of another string, but has different formatting applied + * at the character level. + */ + private int uniqueCount; + /** * Create a new SharedStringsTable, by reading it * from the InputStream of a PackagePart. @@ -54,7 +95,7 @@ public class SharedStringsTable implements SharedStringSource, XSSFModel { * Create a new, empty SharedStringsTable */ public SharedStringsTable() { - doc = SstDocument.Factory.newInstance(); + count = uniqueCount = 0; } /** @@ -65,32 +106,81 @@ public class SharedStringsTable implements SharedStringSource, XSSFModel { */ public void readFrom(InputStream is) throws IOException { try { - doc = SstDocument.Factory.parse(is); - for (CTRst rst : doc.getSst().getSiArray()) { - strings.add(rst.getT()); + int cnt = 0; + CTSst sst = SstDocument.Factory.parse(is).getSst(); + count = (int)sst.getCount(); + uniqueCount = (int)sst.getUniqueCount(); + for (CTRst st : sst.getSiArray()) { + stmap.put(st.toString(), cnt); + strings.add(st); + cnt++; } } catch (XmlException e) { throw new IOException(e.getLocalizedMessage()); } } - public String getSharedStringAt(int idx) { + /** + * Return a string item by index + * + * @param idx index of item to return. + * @return the item at the specified position in this Shared String table. + */ + public CTRst getEntryAt(int idx) { return strings.get(idx); } - public synchronized int putSharedString(String s) { - if (strings.contains(s)) { - return strings.indexOf(s); - } - strings.add(s); - return strings.size() - 1; - } - /** - * For unit testing only! + * Return an integer representing the total count of strings in the workbook. This count does not + * include any numbers, it counts only the total of text strings in the workbook. + * + * @return the total count of strings in the workbook */ - public int _getNumberOfStrings() { - return strings.size(); + public int getCount(){ + return count; + } + + /** + * Returns an integer representing the total count of unique strings in the Shared String Table. + * A string is unique even if it is a copy of another string, but has different formatting applied + * at the character level. + * + * @return the total count of unique strings in the workbook + */ + public int getUniqueCount(){ + return uniqueCount; + } + + /** + * Add an entry to this Shared String table (a new value is appened to the end). + * + *

+ * If the Shared String table already contains this CTRst bean, its index is returned. + * Otherwise a new entry is aded. + *

+ * + * @param st the entry to add + * @return index the index of added entry + */ + public int addEntry(CTRst st) { + String s = st.toString(); + count++; + if (stmap.containsKey(s)) { + return stmap.get(s); + } + uniqueCount++; + int idx = strings.size(); + stmap.put(s, idx); + strings.add(st); + return idx; + } + /** + * Provide low-level access to the underlying array of CTRst beans + * + * @return array of CTRst beans + */ + public List getItems() { + return strings; } /** @@ -103,17 +193,16 @@ public class SharedStringsTable implements SharedStringSource, XSSFModel { XmlOptions options = new XmlOptions(); options.setSaveOuter(); options.setUseDefaultNamespace(); - - // Requests use of whitespace for easier reading - options.setSavePrettyPrint(); + //re-create the sst table every time saving a workbook SstDocument doc = SstDocument.Factory.newInstance(options); CTSst sst = doc.addNewSst(); - sst.setCount(strings.size()); - sst.setUniqueCount(strings.size()); - for (String s : strings) { - sst.addNewSi().setT(s); - } + sst.setCount(count); + sst.setUniqueCount(uniqueCount); + + CTRst[] ctr = strings.toArray(new CTRst[strings.size()]); + sst.setSiArray(ctr); doc.save(out, options); } + } 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 8da984c93..658d4ebbd 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java @@ -32,6 +32,8 @@ import org.apache.poi.ss.usermodel.StylesSource; import org.apache.poi.util.POILogFactory; import org.apache.poi.util.POILogger; import org.apache.poi.ss.util.CellReference; +import org.apache.poi.xssf.model.SharedStringsTable; +import org.apache.poi.xssf.model.StylesTable; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellFormula; import org.openxmlformats.schemas.spreadsheetml.x2006.main.STCellType; @@ -46,8 +48,8 @@ public final class XSSFCell implements Cell { private final CTCell cell; private final XSSFRow row; private int cellNum; - private SharedStringSource sharedStringSource; - private StylesSource stylesSource; + private SharedStringsTable sharedStringSource; + private StylesTable stylesSource; private POILogger logger = POILogFactory.getLogger(XSSFCell.class); @@ -65,8 +67,8 @@ public final class XSSFCell implements Cell { if (cell.getR() != null) { this.cellNum = parseCellNum(cell.getR()); } - this.sharedStringSource = row.getSheet().getWorkbook().getSharedStringSource(); - this.stylesSource = row.getSheet().getWorkbook().getStylesSource(); + this.sharedStringSource = (SharedStringsTable) row.getSheet().getWorkbook().getSharedStringSource(); + this.stylesSource = (StylesTable)row.getSheet().getWorkbook().getStylesSource(); } protected SharedStringSource getSharedStringSource() { @@ -234,7 +236,7 @@ public final class XSSFCell implements Cell { // (i.e. whether to return empty string or throw exception). } - public RichTextString getRichStringCellValue() { + public XSSFRichTextString getRichStringCellValue() { if(this.cell.getT() == STCellType.INLINE_STR) { if(this.cell.isSetV()) { return new XSSFRichTextString(this.cell.getV()); @@ -243,12 +245,15 @@ public final class XSSFCell implements Cell { } } if(this.cell.getT() == STCellType.S) { + XSSFRichTextString rt; if(this.cell.isSetV()) { int sRef = Integer.parseInt(this.cell.getV()); - return new XSSFRichTextString(getSharedStringSource().getSharedStringAt(sRef)); + rt = new XSSFRichTextString(sharedStringSource.getEntryAt(sRef)); } else { - return new XSSFRichTextString(""); + rt = new XSSFRichTextString(""); } + rt.setStylesTableReference(stylesSource); + return rt; } throw new NumberFormatException("You cannot get a string value from a non-string cell"); } @@ -396,7 +401,8 @@ public final class XSSFCell implements Cell { if(this.cell.getT() != STCellType.S) { this.cell.setT(STCellType.S); } - int sRef = getSharedStringSource().putSharedString(value.getString()); + XSSFRichTextString rt = (XSSFRichTextString)value; + int sRef = sharedStringSource.addEntry(rt.getCTRst()); this.cell.setV(Integer.toString(sRef)); } @@ -408,7 +414,6 @@ public final class XSSFCell implements Cell { this.cell.setV(value ? TRUE_AS_STRING : FALSE_AS_STRING); } - @Override public String toString() { return "[" + this.row.getRowNum() + "," + this.getCellNum() + "] " + this.cell.getV(); } diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFDialogsheet.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFDialogsheet.java index 78634d496..a211d93b5 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFDialogsheet.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFDialogsheet.java @@ -42,7 +42,7 @@ public class XSSFDialogsheet extends XSSFSheet implements Sheet{ } } - public Row createRow(int rowNum) { + public XSSFRow createRow(int rowNum) { return null; } 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 8396004d5..83f44da84 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRichTextString.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRichTextString.java @@ -19,76 +19,250 @@ 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; /** - * TODO - the rich part + * Rich text unicode string. These strings can have fonts applied to arbitary parts of the string. + * + *

+ * 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. + *

+ * + *
+ *
+ * <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"));
+ * 
+ *
+ * In the above example all three cells will use the same string cached on workbook level. + * + *

+ * Some strings in the workbook may have formatting applied at a level that is more granular than the cell level. + * For instance, specific characters within the string may be bolded, have coloring, italicizing, etc. + * In these cases, the formatting is stored along with the text in the string table, and is treated as + * a unique entry in the workbook. The following xml and code snippet illustrate this. + *

+ * + *
+ *
+ *     XSSFRichTextString s1 = new XSSFRichTextString("Apache POI");
+ *     s1.applyFont(boldArial);
+ *     cell1.setCellValue(s1);
+ *
+ *     XSSFRichTextString s2 = new XSSFRichTextString("Apache POI");
+ *     s2.applyFont(italicCourier);
+ *     cell2.setCellValue(s2);
+ * 
+ *
+ * + * 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 String string; - + private CTRst st; + private StylesTable styles; + public XSSFRichTextString(String str) { - this.string = str; + st = CTRst.Factory.newInstance(); + st.setT(str); } - + + public XSSFRichTextString() { + st = CTRst.Factory.newInstance(); + } + + public XSSFRichTextString(CTRst st) { + this.st = st; + } + + /** + * Applies a font to the specified characters of a string. + * + * @param startIndex The start index to apply the font to (inclusive) + * @param endIndex The end index to apply the font to (exclusive) + * @param fontIndex The font to use. + */ public void applyFont(int startIndex, int endIndex, short fontIndex) { // TODO Auto-generated method stub } + /** + * Applies a font to the specified characters of a string. + * + * @param startIndex The start index to apply the font to (inclusive) + * @param endIndex The end index to apply to font to (exclusive) + * @param font The index of the font to use. + */ public void applyFont(int startIndex, int endIndex, Font font) { - // TODO Auto-generated method stub - + applyFont(0, length(), font.getIndex()); } + /** + * Sets the font of the entire string. + * @param font The font to use. + */ public void applyFont(Font font) { - // TODO Auto-generated method stub - + applyFont(0, length(), font); } + /** + * Applies the specified font to the entire string. + * + * @param fontIndex the font to apply. + */ public void applyFont(short fontIndex) { - // TODO Auto-generated method stub - + applyFont(0, length(), fontIndex); } + /** + * Removes any formatting that may have been applied to the string. + */ public void clearFormatting() { - // TODO Auto-generated method stub - - } - - public int compareTo(Object o) { - // TODO Auto-generated method stub - return 0; + for (int i = 0; i < st.sizeOfRArray(); i++) { + st.removeR(i); + } } + /** + * Returns the font in use at a particular index. + * + * @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. + */ public short getFontAtIndex(int index) { // TODO Auto-generated method stub return 0; } + /** + * Gets the font used in a particular formatting run. + * + * @param index the index of the formatting run + * @return the font number used. + */ public short getFontOfFormattingRun(int index) { // TODO Auto-generated method stub return 0; } + /** + * 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; } + /** + * Returns the plain string representation. + */ public String getString() { - return string; + if(st.sizeOfRArray() == 0) return st.getT(); + else { + StringBuffer buf = new StringBuffer(); + for(CTRElt r : st.getRArray()){ + buf.append(r.getT()); + } + return buf.toString(); + } } + + /** + * Removes any formatting and sets new string value + * + * @param s new string value + */ + public void setString(String s){ + clearFormatting(); + st.setT(s); + } + + /** + * Returns the plain string representation. + */ public String toString() { - return string; + return getString(); } + /** + * Returns the number of characters in this string. + */ public int length() { - return string.length(); + return getString().length(); } + /** + * @return The number of formatting runs used. + */ public int numFormattingRuns() { - // TODO Auto-generated method stub - return 0; + return st.sizeOfRArray(); + } + + /** + * Return the underlying xml bean + */ + public CTRst getCTRst() { + return st; + } + + protected void setStylesTableReference(StylesTable tbl){ + styles = tbl; } } diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRow.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRow.java index 624cb093d..ba97b8e08 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRow.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRow.java @@ -80,10 +80,10 @@ public class XSSFRow implements Row { return 0; } - public Cell createCell(int column) { + public XSSFCell createCell(int column) { return createCell(column, Cell.CELL_TYPE_BLANK); } - public Cell createCell(short column) { + public XSSFCell createCell(short column) { return createCell((int)column); } @@ -105,10 +105,10 @@ public class XSSFRow implements Row { return xcell; } - public Cell createCell(short column, int type) { + public XSSFCell createCell(short column, int type) { return createCell((int)column, type); } - public Cell createCell(int column, int type) { + public XSSFCell createCell(int column, int type) { int index = 0; for (Cell c : this.cells) { if (c.getCellNum() == column) { 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 94da7ac01..c2b2dccdb 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java @@ -266,7 +266,7 @@ public class XSSFSheet implements Sheet { return xrow; } - public Row createRow(int rownum) { + public XSSFRow createRow(int rownum) { int index = 0; for (Row r : this.rows) { if (r.getRowNum() == rownum) { @@ -932,11 +932,6 @@ public class XSSFSheet implements Sheet { getSheetTypePrintOptions().setGridLines(newPrintGridlines); } - public void setProtect(boolean protect) { - // TODO Auto-generated method stub - - } - public void setRowBreak(int row) { CTPageBreak pageBreak = getSheetTypeRowBreaks(); if (! isRowBroken(row)) { 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 28cee4227..e3d770fe5 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java @@ -75,31 +75,31 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.WorksheetDocument; public class XSSFWorkbook extends POIXMLDocument implements Workbook { /** Are we a normal workbook, or a macro enabled one? */ private boolean isMacroEnabled = false; - + private CTWorkbook workbook; - + private List sheets = new LinkedList(); private List namedRanges = new LinkedList(); - + private SharedStringSource sharedStringSource; private StylesSource stylesSource; - + private MissingCellPolicy missingCellPolicy = Row.RETURN_NULL_AND_BLANK; private static POILogger log = POILogFactory.getLogger(XSSFWorkbook.class); - + public XSSFWorkbook() { this.workbook = CTWorkbook.Factory.newInstance(); CTBookViews bvs = this.workbook.addNewBookViews(); CTBookView bv = bvs.addNewWorkbookView(); bv.setActiveTab(0); this.workbook.addNewSheets(); - + // We always require styles and shared strings sharedStringSource = new SharedStringsTable(); stylesSource = new StylesTable(); } - + public XSSFWorkbook(String path) throws IOException { this(openPackage(path)); } @@ -108,11 +108,11 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook { try { WorkbookDocument doc = WorkbookDocument.Factory.parse(getCorePart().getInputStream()); this.workbook = doc.getWorkbook(); - + // Are we macro enabled, or just normal? - isMacroEnabled = + isMacroEnabled = getCorePart().getContentType().equals(XSSFRelation.MACROS_WORKBOOK.getContentType()); - + try { // Load shared strings this.sharedStringSource = (SharedStringSource) @@ -128,7 +128,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook { e.printStackTrace(); throw new IOException("Unable to load styles - " + e.toString()); } - + // Load individual sheets for (CTSheet ctSheet : this.workbook.getSheets().getSheetArray()) { PackagePart part = getPackagePart(ctSheet); @@ -136,7 +136,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook { log.log(POILogger.WARN, "Sheet with name " + ctSheet.getName() + " and r:id " + ctSheet.getId()+ " was defined, but didn't exist in package, skipping"); continue; } - + // Load child streams of the sheet ArrayList childModels; CommentsSource comments = null; @@ -148,7 +148,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook { if(childModels.size() > 0) { comments = (CommentsSource)childModels.get(0); } - + // Get the drawings for the sheet, if there are any drawings = (ArrayList)XSSFRelation.VML_DRAWINGS.loadAll(part); // Get the activeX controls for the sheet, if there are any @@ -156,22 +156,22 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook { } catch(Exception e) { throw new RuntimeException("Unable to construct child part",e); } - + // Now create the sheet WorksheetDocument worksheetDoc = WorksheetDocument.Factory.parse(part.getInputStream()); XSSFSheet sheet = new XSSFSheet(ctSheet, worksheetDoc.getWorksheet(), this, comments, drawings, controls); this.sheets.add(sheet); - + // Process external hyperlinks for the sheet, // if there are any PackageRelationshipCollection hyperlinkRels = part.getRelationshipsByType(XSSFRelation.SHEET_HYPERLINKS.getRelation()); sheet.initHyperlinks(hyperlinkRels); - + // Get the embeddings for the workbook for(PackageRelationship rel : part.getRelationshipsByType(XSSFRelation.OLEEMBEDDINGS.getRelation())) embedds.add(getTargetPart(rel)); // TODO: Add this reference to each sheet as well - + for(PackageRelationship rel : part.getRelationshipsByType(XSSFRelation.PACKEMBEDDINGS.getRelation())) embedds.add(getTargetPart(rel)); } @@ -180,7 +180,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook { } catch (InvalidFormatException e) { throw new IOException(e.toString()); } - + // Process the named ranges if(workbook.getDefinedNames() != null) { for(CTDefinedName ctName : workbook.getDefinedNames().getDefinedNameArray()) { @@ -195,7 +195,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook { /** * Get the PackagePart corresponding to a given sheet. - * + * * @param ctSheet The sheet * @return A PackagePart, or null if no matching part found. * @throws InvalidFormatException @@ -208,18 +208,13 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook { } return getTargetPart(rel); } - + public int addPicture(byte[] pictureData, int format) { // TODO Auto-generated method stub return 0; } - public int addSSTString(String string) { - // TODO Auto-generated method stub - return 0; - } - - public Sheet cloneSheet(int sheetNum) { + public XSSFSheet cloneSheet(int sheetNum) { XSSFSheet srcSheet = sheets.get(sheetNum); String srcName = getSheetName(sheetNum); if (srcSheet != null) { @@ -228,7 +223,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook { sheets.add(clonedSheet); CTSheet newcts = this.workbook.getSheets().addNewSheet(); newcts.set(clonedSheet.getSheet()); - + int i = 1; while (true) { //Try and find the next sheet name that is unique @@ -251,7 +246,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook { return null; } - public CellStyle createCellStyle() { + public XSSFCellStyle createCellStyle() { return new XSSFCellStyle(stylesSource); } @@ -259,7 +254,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook { return getCreationHelper().createDataFormat(); } - public Font createFont() { + public XSSFFont createFont() { return new XSSFFont(); } @@ -269,22 +264,23 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook { return name; } - public Sheet createSheet() { - return createSheet(null); + public XSSFSheet createSheet() { + String sheetname = "Sheet" + (sheets.size() + 1); + return createSheet(sheetname); } - public Sheet createSheet(String sheetname) { + public XSSFSheet createSheet(String sheetname) { return createSheet(sheetname, null); } - - public Sheet createSheet(String sheetname, CTWorksheet worksheet) { + + public XSSFSheet createSheet(String sheetname, CTWorksheet worksheet) { CTSheet sheet = addSheet(sheetname); XSSFWorksheet wrapper = new XSSFWorksheet(sheet, worksheet, this); this.sheets.add(wrapper); return wrapper; } - - public Sheet createDialogsheet(String sheetname, CTDialogsheet dialogsheet) { + + public XSSFSheet createDialogsheet(String sheetname, CTDialogsheet dialogsheet) { CTSheet sheet = addSheet(sheetname); XSSFDialogsheet wrapper = new XSSFDialogsheet(sheet, dialogsheet, this); this.sheets.add(wrapper); @@ -375,7 +371,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook { /** * get the first tab that is displayed in the list of tabs in excel. - */ + */ public int getFirstVisibleTab() { CTBookViews bookViews = workbook.getBookViews(); CTBookView bookView = bookViews.getWorkbookViewArray(0); @@ -383,7 +379,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook { } /** * deprecated Aug 2008 - * @deprecated - Misleading name - use getFirstVisibleTab() + * @deprecated - Misleading name - use getFirstVisibleTab() */ public short getDisplayedTab() { return (short) getFirstVisibleTab(); @@ -407,7 +403,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook { } return -1; } - + /** * TODO - figure out what the hell this methods does in * HSSF... @@ -439,10 +435,6 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook { return null; } - public String getSSTString(int index) { - return getSharedStringSource().getSharedStringAt(index); - } - public short getSelectedTab() { short i = 0; for (XSSFSheet sheet : this.sheets) { @@ -453,7 +445,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook { } return -1; } - + /** * Doesn't do anything - returns the same index * TODO - figure out if this is a ole2 specific thing, or @@ -472,7 +464,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook { } public Sheet getSheet(String name) { - CTSheet[] sheets = this.workbook.getSheets().getSheetArray(); + CTSheet[] sheets = this.workbook.getSheets().getSheetArray(); for (int i = 0 ; i < sheets.length ; ++i) { if (name.equals(sheets[i].getName())) { return this.sheets.get(i); @@ -486,7 +478,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook { } public int getSheetIndex(String name) { - CTSheet[] sheets = this.workbook.getSheets().getSheetArray(); + CTSheet[] sheets = this.workbook.getSheets().getSheetArray(); for (int i = 0 ; i < sheets.length ; ++i) { if (name.equals(sheets[i].getName())) { return i; @@ -498,12 +490,12 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook { public int getSheetIndex(Sheet sheet) { return this.sheets.indexOf(sheet); } - + /** * Returns the external sheet index of the sheet * with the given internal index, creating one * if needed. - * Used by some of the more obscure formula and + * Used by some of the more obscure formula and * named range things. * Fairly easy on XSSF (we think...) since the * internal and external indicies are the same @@ -621,10 +613,6 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook { this.workbook.getSheets().getSheetArray(sheet).setName(name); } - public void setSheetName(int sheet, String name, short encoding) { - this.workbook.getSheets().getSheetArray(sheet).setName(name); - } - public void setSheetOrder(String sheetname, int pos) { int idx = getSheetIndex(sheetname); sheets.add(pos, sheets.remove(idx)); diff --git a/src/ooxml/testcases/org/apache/poi/xssf/eventusermodel/TestXSSFReader.java b/src/ooxml/testcases/org/apache/poi/xssf/eventusermodel/TestXSSFReader.java index bdb9fedaa..8ba59de43 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/eventusermodel/TestXSSFReader.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/eventusermodel/TestXSSFReader.java @@ -24,6 +24,7 @@ import java.util.Iterator; import junit.framework.TestCase; import org.apache.poi.util.IOUtils; +import org.apache.poi.xssf.usermodel.XSSFRichTextString; import org.openxml4j.opc.Package; /** @@ -76,8 +77,8 @@ public class TestXSSFReader extends TestCase { XSSFReader r = new XSSFReader(pkg); - assertEquals(11, r.getSharedStringsTable()._getNumberOfStrings()); - assertEquals("Test spreadsheet", r.getSharedStringsTable().getSharedStringAt(0)); + assertEquals(11, r.getSharedStringsTable().getItems().size()); + assertEquals("Test spreadsheet", new XSSFRichTextString(r.getSharedStringsTable().getEntryAt(0)).toString()); } public void testSheets() throws Exception { diff --git a/src/testcases/org/apache/poi/hssf/data/sample.xlsx b/src/testcases/org/apache/poi/hssf/data/sample.xlsx index a275cf417..6a53fd992 100644 Binary files a/src/testcases/org/apache/poi/hssf/data/sample.xlsx and b/src/testcases/org/apache/poi/hssf/data/sample.xlsx differ