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;
// 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
*/
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.
*
*/
public interface SharedStringSource {
- /**
- * Return the string at position <code>idx</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);
}
*/
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
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
* 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.
*/
int getNumberOfSheets();
-
+
/**
* Finds the sheet index for a particular external sheet number.
* @param externSheetNumber The external sheet number to convert
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
*/
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$
+ * <p>
+ * 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.
+ * </p>
+ * <p>
+ * 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.
+ * </p>
+ * <p>
+ * The shared string table contains all the necessary information for displaying the string: the text, formatting
+ * properties, and phonetic properties (for East Asian languages).
+ * </p>
+ *
+ * @author Nick Birch
+ * @author Yegor Kozlov
*/
public class SharedStringsTable implements SharedStringSource, XSSFModel {
- private final LinkedList<String> strings = new LinkedList<String>();
- private SstDocument doc;
-
+ /**
+ * Array of individual string items in the Shared String table.
+ */
+ private final List<CTRst> strings = new ArrayList<CTRst>();
+
+ /**
+ * Maps strings and their indexes in the <code>strings</code> arrays
+ */
+ private final Map<String, Integer> stmap = new HashMap<String, Integer>();
+
+ /**
+ * 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.
* Create a new, empty SharedStringsTable
*/
public SharedStringsTable() {
- doc = SstDocument.Factory.newInstance();
+ count = uniqueCount = 0;
}
/**
*/
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);
+ /**
+ * 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 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).
+ *
+ * <p>
+ * If the Shared String table already contains this <code>CTRst</code> bean, its index is returned.
+ * Otherwise a new entry is aded.
+ * </p>
+ *
+ * @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);
}
- strings.add(s);
- return strings.size() - 1;
+ uniqueCount++;
+ int idx = strings.size();
+ stmap.put(s, idx);
+ strings.add(st);
+ return idx;
}
-
/**
- * For unit testing only!
+ * Provide low-level access to the underlying array of CTRst beans
+ *
+ * @return array of CTRst beans
*/
- public int _getNumberOfStrings() {
- return strings.size();
+ public List<CTRst> getItems() {
+ return strings;
}
/**
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);
}
+
}
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;
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);
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() {
// (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());
}
}
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");
}
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));
}
this.cell.setV(value ? TRUE_AS_STRING : FALSE_AS_STRING);
}
- @Override
public String toString() {
return "[" + this.row.getRowNum() + "," + this.getCellNum() + "] " + this.cell.getV();
}
}
}
- public Row createRow(int rowNum) {
+ public XSSFRow createRow(int rowNum) {
return null;
}
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.
+ *
+ * <p>
+ * 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.
+ * </p>
+ *
+ * <blockquote>
+ * <pre>
+ * <sst xmlns=http://schemas.openxmlformats.org/spreadsheetml/2006/5/main
+ * count="1" uniqueCount="1">
+ * <si>
+ * <t>Apache POI</t>
+ * </si>
+ * </sst>
+ * </pre>
+ * </blockquote>
+ *
+ * The code to produce xml above:
+ * <blockquote>
+ * <pre>
+ * cell1.setCellValue(new XSSFRichTextString("Apache POI"));
+ * cell2.setCellValue(new XSSFRichTextString("Apache POI"));
+ * cell3.setCellValue(new XSSFRichTextString("Apache POI"));
+ * </pre>
+ * </blockquote>
+ * In the above example all three cells will use the same string cached on workbook level.
+ *
+ * <p>
+ * 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.
+ * </p>
+ *
+ * <blockquote>
+ * <pre>
+ * XSSFRichTextString s1 = new XSSFRichTextString("Apache POI");
+ * s1.applyFont(boldArial);
+ * cell1.setCellValue(s1);
+ *
+ * XSSFRichTextString s2 = new XSSFRichTextString("Apache POI");
+ * s2.applyFont(italicCourier);
+ * cell2.setCellValue(s2);
+ * </pre>
+ * </blockquote>
+ *
+ * The code above will produce the following xml:
+ * <blockquote>
+ * <pre>
+ * <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>
+ *
+ * </pre>
+ * </blockquote>
+ *
+ * @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;
}
}
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);
}
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) {
return xrow;
}
- public Row createRow(int rownum) {
+ public XSSFRow createRow(int rownum) {
int index = 0;
for (Row r : this.rows) {
if (r.getRowNum() == rownum) {
getSheetTypePrintOptions().setGridLines(newPrintGridlines);
}
- public void setProtect(boolean protect) {
- // TODO Auto-generated method stub
-
- }
-
public void setRowBreak(int row) {
CTPageBreak pageBreak = getSheetTypeRowBreaks();
if (! isRowBroken(row)) {
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<XSSFSheet> sheets = new LinkedList<XSSFSheet>();
private List<XSSFName> namedRanges = new LinkedList<XSSFName>();
-
+
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));
}
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)
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);
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<? extends XSSFModel> childModels;
CommentsSource comments = null;
if(childModels.size() > 0) {
comments = (CommentsSource)childModels.get(0);
}
-
+
// Get the drawings for the sheet, if there are any
drawings = (ArrayList<Drawing>)XSSFRelation.VML_DRAWINGS.loadAll(part);
// Get the activeX controls for the sheet, if there are any
} 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));
}
} catch (InvalidFormatException e) {
throw new IOException(e.toString());
}
-
+
// Process the named ranges
if(workbook.getDefinedNames() != null) {
for(CTDefinedName ctName : workbook.getDefinedNames().getDefinedNameArray()) {
/**
* Get the PackagePart corresponding to a given sheet.
- *
+ *
* @param ctSheet The sheet
* @return A PackagePart, or null if no matching part found.
* @throws InvalidFormatException
}
return getTargetPart(rel);
}
-
- public int addPicture(byte[] pictureData, int format) {
- // TODO Auto-generated method stub
- return 0;
- }
- public int addSSTString(String string) {
+ public int addPicture(byte[] pictureData, int format) {
// 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) {
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
return null;
}
- public CellStyle createCellStyle() {
+ public XSSFCellStyle createCellStyle() {
return new XSSFCellStyle(stylesSource);
}
return getCreationHelper().createDataFormat();
}
- public Font createFont() {
+ public XSSFFont createFont() {
return new XSSFFont();
}
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);
/**
* 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);
}
/**
* deprecated Aug 2008
- * @deprecated - Misleading name - use getFirstVisibleTab()
+ * @deprecated - Misleading name - use getFirstVisibleTab()
*/
public short getDisplayedTab() {
return (short) getFirstVisibleTab();
}
return -1;
}
-
+
/**
* TODO - figure out what the hell this methods does in
* HSSF...
return null;
}
- public String getSSTString(int index) {
- return getSharedStringSource().getSharedStringAt(index);
- }
-
public short getSelectedTab() {
short i = 0;
for (XSSFSheet sheet : this.sheets) {
}
return -1;
}
-
+
/**
* Doesn't do anything - returns the same index
* TODO - figure out if this is a ole2 specific thing, or
}
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);
}
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;
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
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));
import junit.framework.TestCase;
import org.apache.poi.util.IOUtils;
+import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.openxml4j.opc.Package;
/**
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 {