mirror of https://github.com/apache/poi.git
Preserve rich text across read-write of SharedStringsTable, also improved performance and removed obsolete ole2-specific stuff
git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@695832 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
99008de47e
commit
9ef957f768
|
@ -21,6 +21,7 @@ import java.util.Iterator;
|
||||||
|
|
||||||
import org.apache.poi.xssf.eventusermodel.XSSFReader;
|
import org.apache.poi.xssf.eventusermodel.XSSFReader;
|
||||||
import org.apache.poi.xssf.model.SharedStringsTable;
|
import org.apache.poi.xssf.model.SharedStringsTable;
|
||||||
|
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
|
||||||
import org.openxml4j.opc.Package;
|
import org.openxml4j.opc.Package;
|
||||||
import org.xml.sax.Attributes;
|
import org.xml.sax.Attributes;
|
||||||
import org.xml.sax.ContentHandler;
|
import org.xml.sax.ContentHandler;
|
||||||
|
@ -113,7 +114,7 @@ public class FromHowTo {
|
||||||
// Do now, as characters() may be called more than once
|
// Do now, as characters() may be called more than once
|
||||||
if(nextIsString) {
|
if(nextIsString) {
|
||||||
int idx = Integer.parseInt(lastContents);
|
int idx = Integer.parseInt(lastContents);
|
||||||
lastContents = sst.getSharedStringAt(idx);
|
lastContents = new XSSFRichTextString(sst.getEntryAt(idx)).toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
// v => contents of a cell
|
// v => contents of a cell
|
||||||
|
|
|
@ -100,18 +100,6 @@ public interface RichTextString {
|
||||||
*/
|
*/
|
||||||
short getFontOfFormattingRun(int index);
|
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.
|
* Applies the specified font to the entire string.
|
||||||
*
|
*
|
||||||
|
|
|
@ -22,19 +22,4 @@ package org.apache.poi.ss.usermodel;
|
||||||
*/
|
*/
|
||||||
public interface SharedStringSource {
|
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);
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -450,13 +450,6 @@ public interface Sheet extends Iterable<Row> {
|
||||||
*/
|
*/
|
||||||
boolean getScenarioProtect();
|
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
|
* Sets the protection enabled as well as the password
|
||||||
* @param password to set for protection
|
* @param password to set for protection
|
||||||
|
|
|
@ -94,35 +94,13 @@ public interface Workbook {
|
||||||
short getDisplayedTab();
|
short getDisplayedTab();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @deprecated POI will now properly handle unicode strings without
|
* set the sheet name.
|
||||||
* 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.
|
|
||||||
* Will throw IllegalArgumentException if the name is greater than 31 chars
|
* Will throw IllegalArgumentException if the name is greater than 31 chars
|
||||||
* or contains /\?*[]
|
* or contains /\?*[]
|
||||||
* @param sheet number (0 based)
|
* @param sheet number (0 based)
|
||||||
*/
|
*/
|
||||||
void setSheetName(int sheet, String name);
|
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
|
* get the sheet name
|
||||||
* @param sheet Number
|
* @param sheet Number
|
||||||
|
@ -147,11 +125,11 @@ public interface Workbook {
|
||||||
* Returns the external sheet index of the sheet
|
* Returns the external sheet index of the sheet
|
||||||
* with the given internal index, creating one
|
* with the given internal index, creating one
|
||||||
* if needed.
|
* if needed.
|
||||||
* Used by some of the more obscure formula and
|
* Used by some of the more obscure formula and
|
||||||
* named range things.
|
* named range things.
|
||||||
*/
|
*/
|
||||||
int getExternalSheetIndex(int internalSheetIndex);
|
int getExternalSheetIndex(int internalSheetIndex);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* create an HSSFSheet for this HSSFWorkbook, adds it to the sheets and returns
|
* create an HSSFSheet for this HSSFWorkbook, adds it to the sheets and returns
|
||||||
* the high level representation. Use this to create new sheets.
|
* the high level representation. Use this to create new sheets.
|
||||||
|
@ -185,7 +163,7 @@ public interface Workbook {
|
||||||
*/
|
*/
|
||||||
|
|
||||||
int getNumberOfSheets();
|
int getNumberOfSheets();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Finds the sheet index for a particular external sheet number.
|
* Finds the sheet index for a particular external sheet number.
|
||||||
* @param externSheetNumber The external sheet number to convert
|
* @param externSheetNumber The external sheet number to convert
|
||||||
|
@ -338,16 +316,6 @@ public interface Workbook {
|
||||||
|
|
||||||
byte[] getBytes();
|
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
|
/** gets the total number of named ranges in the workboko
|
||||||
* @return number of named ranges
|
* @return number of named ranges
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -20,26 +20,67 @@ package org.apache.poi.xssf.model;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.util.LinkedList;
|
import java.util.*;
|
||||||
|
|
||||||
import org.apache.poi.ss.usermodel.SharedStringSource;
|
import org.apache.poi.ss.usermodel.SharedStringSource;
|
||||||
|
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
|
||||||
import org.apache.xmlbeans.XmlException;
|
import org.apache.xmlbeans.XmlException;
|
||||||
import org.apache.xmlbeans.XmlOptions;
|
import org.apache.xmlbeans.XmlOptions;
|
||||||
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst;
|
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst;
|
||||||
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSst;
|
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSst;
|
||||||
import org.openxmlformats.schemas.spreadsheetml.x2006.main.SstDocument;
|
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.
|
* Table of strings shared across all sheets in a workbook.
|
||||||
*
|
* <p>
|
||||||
* @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.
|
||||||
|
* </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 {
|
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
|
* Create a new SharedStringsTable, by reading it
|
||||||
* from the InputStream of a PackagePart.
|
* from the InputStream of a PackagePart.
|
||||||
|
@ -54,7 +95,7 @@ public class SharedStringsTable implements SharedStringSource, XSSFModel {
|
||||||
* Create a new, empty SharedStringsTable
|
* Create a new, empty SharedStringsTable
|
||||||
*/
|
*/
|
||||||
public 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 {
|
public void readFrom(InputStream is) throws IOException {
|
||||||
try {
|
try {
|
||||||
doc = SstDocument.Factory.parse(is);
|
int cnt = 0;
|
||||||
for (CTRst rst : doc.getSst().getSiArray()) {
|
CTSst sst = SstDocument.Factory.parse(is).getSst();
|
||||||
strings.add(rst.getT());
|
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) {
|
} catch (XmlException e) {
|
||||||
throw new IOException(e.getLocalizedMessage());
|
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);
|
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() {
|
public int getCount(){
|
||||||
return strings.size();
|
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);
|
||||||
|
}
|
||||||
|
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<CTRst> getItems() {
|
||||||
|
return strings;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -103,17 +193,16 @@ public class SharedStringsTable implements SharedStringSource, XSSFModel {
|
||||||
XmlOptions options = new XmlOptions();
|
XmlOptions options = new XmlOptions();
|
||||||
options.setSaveOuter();
|
options.setSaveOuter();
|
||||||
options.setUseDefaultNamespace();
|
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);
|
SstDocument doc = SstDocument.Factory.newInstance(options);
|
||||||
CTSst sst = doc.addNewSst();
|
CTSst sst = doc.addNewSst();
|
||||||
sst.setCount(strings.size());
|
sst.setCount(count);
|
||||||
sst.setUniqueCount(strings.size());
|
sst.setUniqueCount(uniqueCount);
|
||||||
for (String s : strings) {
|
|
||||||
sst.addNewSi().setT(s);
|
CTRst[] ctr = strings.toArray(new CTRst[strings.size()]);
|
||||||
}
|
sst.setSiArray(ctr);
|
||||||
doc.save(out, options);
|
doc.save(out, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,6 +32,8 @@ import org.apache.poi.ss.usermodel.StylesSource;
|
||||||
import org.apache.poi.util.POILogFactory;
|
import org.apache.poi.util.POILogFactory;
|
||||||
import org.apache.poi.util.POILogger;
|
import org.apache.poi.util.POILogger;
|
||||||
import org.apache.poi.ss.util.CellReference;
|
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.CTCell;
|
||||||
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellFormula;
|
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellFormula;
|
||||||
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STCellType;
|
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STCellType;
|
||||||
|
@ -46,8 +48,8 @@ public final class XSSFCell implements Cell {
|
||||||
private final CTCell cell;
|
private final CTCell cell;
|
||||||
private final XSSFRow row;
|
private final XSSFRow row;
|
||||||
private int cellNum;
|
private int cellNum;
|
||||||
private SharedStringSource sharedStringSource;
|
private SharedStringsTable sharedStringSource;
|
||||||
private StylesSource stylesSource;
|
private StylesTable stylesSource;
|
||||||
|
|
||||||
private POILogger logger = POILogFactory.getLogger(XSSFCell.class);
|
private POILogger logger = POILogFactory.getLogger(XSSFCell.class);
|
||||||
|
|
||||||
|
@ -65,8 +67,8 @@ public final class XSSFCell implements Cell {
|
||||||
if (cell.getR() != null) {
|
if (cell.getR() != null) {
|
||||||
this.cellNum = parseCellNum(cell.getR());
|
this.cellNum = parseCellNum(cell.getR());
|
||||||
}
|
}
|
||||||
this.sharedStringSource = row.getSheet().getWorkbook().getSharedStringSource();
|
this.sharedStringSource = (SharedStringsTable) row.getSheet().getWorkbook().getSharedStringSource();
|
||||||
this.stylesSource = row.getSheet().getWorkbook().getStylesSource();
|
this.stylesSource = (StylesTable)row.getSheet().getWorkbook().getStylesSource();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected SharedStringSource getSharedStringSource() {
|
protected SharedStringSource getSharedStringSource() {
|
||||||
|
@ -234,7 +236,7 @@ public final class XSSFCell implements Cell {
|
||||||
// (i.e. whether to return empty string or throw exception).
|
// (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.getT() == STCellType.INLINE_STR) {
|
||||||
if(this.cell.isSetV()) {
|
if(this.cell.isSetV()) {
|
||||||
return new XSSFRichTextString(this.cell.getV());
|
return new XSSFRichTextString(this.cell.getV());
|
||||||
|
@ -243,12 +245,15 @@ public final class XSSFCell implements Cell {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(this.cell.getT() == STCellType.S) {
|
if(this.cell.getT() == STCellType.S) {
|
||||||
|
XSSFRichTextString rt;
|
||||||
if(this.cell.isSetV()) {
|
if(this.cell.isSetV()) {
|
||||||
int sRef = Integer.parseInt(this.cell.getV());
|
int sRef = Integer.parseInt(this.cell.getV());
|
||||||
return new XSSFRichTextString(getSharedStringSource().getSharedStringAt(sRef));
|
rt = new XSSFRichTextString(sharedStringSource.getEntryAt(sRef));
|
||||||
} else {
|
} 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");
|
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) {
|
if(this.cell.getT() != STCellType.S) {
|
||||||
this.cell.setT(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(Integer.toString(sRef));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -408,7 +414,6 @@ public final class XSSFCell implements Cell {
|
||||||
this.cell.setV(value ? TRUE_AS_STRING : FALSE_AS_STRING);
|
this.cell.setV(value ? TRUE_AS_STRING : FALSE_AS_STRING);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "[" + this.row.getRowNum() + "," + this.getCellNum() + "] " + this.cell.getV();
|
return "[" + this.row.getRowNum() + "," + this.getCellNum() + "] " + this.cell.getV();
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,7 +42,7 @@ public class XSSFDialogsheet extends XSSFSheet implements Sheet{
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Row createRow(int rowNum) {
|
public XSSFRow createRow(int rowNum) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -19,76 +19,250 @@ package org.apache.poi.xssf.usermodel;
|
||||||
|
|
||||||
import org.apache.poi.ss.usermodel.Font;
|
import org.apache.poi.ss.usermodel.Font;
|
||||||
import org.apache.poi.ss.usermodel.RichTextString;
|
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 {
|
public class XSSFRichTextString implements RichTextString {
|
||||||
private String string;
|
private CTRst st;
|
||||||
|
private StylesTable styles;
|
||||||
|
|
||||||
public XSSFRichTextString(String str) {
|
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) {
|
public void applyFont(int startIndex, int endIndex, short fontIndex) {
|
||||||
// TODO Auto-generated method stub
|
// 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) {
|
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) {
|
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) {
|
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() {
|
public void clearFormatting() {
|
||||||
// TODO Auto-generated method stub
|
for (int i = 0; i < st.sizeOfRArray(); i++) {
|
||||||
|
st.removeR(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int compareTo(Object o) {
|
|
||||||
// TODO Auto-generated method stub
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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) {
|
public short getFontAtIndex(int index) {
|
||||||
// TODO Auto-generated method stub
|
// TODO Auto-generated method stub
|
||||||
return 0;
|
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) {
|
public short getFontOfFormattingRun(int index) {
|
||||||
// TODO Auto-generated method stub
|
// TODO Auto-generated method stub
|
||||||
return 0;
|
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) {
|
public int getIndexOfFormattingRun(int index) {
|
||||||
// TODO Auto-generated method stub
|
// TODO Auto-generated method stub
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the plain string representation.
|
||||||
|
*/
|
||||||
public String getString() {
|
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() {
|
public String toString() {
|
||||||
return string;
|
return getString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the number of characters in this string.
|
||||||
|
*/
|
||||||
public int length() {
|
public int length() {
|
||||||
return string.length();
|
return getString().length();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return The number of formatting runs used.
|
||||||
|
*/
|
||||||
public int numFormattingRuns() {
|
public int numFormattingRuns() {
|
||||||
// TODO Auto-generated method stub
|
return st.sizeOfRArray();
|
||||||
return 0;
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the underlying xml bean
|
||||||
|
*/
|
||||||
|
public CTRst getCTRst() {
|
||||||
|
return st;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void setStylesTableReference(StylesTable tbl){
|
||||||
|
styles = tbl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -80,10 +80,10 @@ public class XSSFRow implements Row {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Cell createCell(int column) {
|
public XSSFCell createCell(int column) {
|
||||||
return createCell(column, Cell.CELL_TYPE_BLANK);
|
return createCell(column, Cell.CELL_TYPE_BLANK);
|
||||||
}
|
}
|
||||||
public Cell createCell(short column) {
|
public XSSFCell createCell(short column) {
|
||||||
return createCell((int)column);
|
return createCell((int)column);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -105,10 +105,10 @@ public class XSSFRow implements Row {
|
||||||
return xcell;
|
return xcell;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Cell createCell(short column, int type) {
|
public XSSFCell createCell(short column, int type) {
|
||||||
return createCell((int)column, type);
|
return createCell((int)column, type);
|
||||||
}
|
}
|
||||||
public Cell createCell(int column, int type) {
|
public XSSFCell createCell(int column, int type) {
|
||||||
int index = 0;
|
int index = 0;
|
||||||
for (Cell c : this.cells) {
|
for (Cell c : this.cells) {
|
||||||
if (c.getCellNum() == column) {
|
if (c.getCellNum() == column) {
|
||||||
|
|
|
@ -266,7 +266,7 @@ public class XSSFSheet implements Sheet {
|
||||||
return xrow;
|
return xrow;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Row createRow(int rownum) {
|
public XSSFRow createRow(int rownum) {
|
||||||
int index = 0;
|
int index = 0;
|
||||||
for (Row r : this.rows) {
|
for (Row r : this.rows) {
|
||||||
if (r.getRowNum() == rownum) {
|
if (r.getRowNum() == rownum) {
|
||||||
|
@ -932,11 +932,6 @@ public class XSSFSheet implements Sheet {
|
||||||
getSheetTypePrintOptions().setGridLines(newPrintGridlines);
|
getSheetTypePrintOptions().setGridLines(newPrintGridlines);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setProtect(boolean protect) {
|
|
||||||
// TODO Auto-generated method stub
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setRowBreak(int row) {
|
public void setRowBreak(int row) {
|
||||||
CTPageBreak pageBreak = getSheetTypeRowBreaks();
|
CTPageBreak pageBreak = getSheetTypeRowBreaks();
|
||||||
if (! isRowBroken(row)) {
|
if (! isRowBroken(row)) {
|
||||||
|
|
|
@ -75,31 +75,31 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.WorksheetDocument;
|
||||||
public class XSSFWorkbook extends POIXMLDocument implements Workbook {
|
public class XSSFWorkbook extends POIXMLDocument implements Workbook {
|
||||||
/** Are we a normal workbook, or a macro enabled one? */
|
/** Are we a normal workbook, or a macro enabled one? */
|
||||||
private boolean isMacroEnabled = false;
|
private boolean isMacroEnabled = false;
|
||||||
|
|
||||||
private CTWorkbook workbook;
|
private CTWorkbook workbook;
|
||||||
|
|
||||||
private List<XSSFSheet> sheets = new LinkedList<XSSFSheet>();
|
private List<XSSFSheet> sheets = new LinkedList<XSSFSheet>();
|
||||||
private List<XSSFName> namedRanges = new LinkedList<XSSFName>();
|
private List<XSSFName> namedRanges = new LinkedList<XSSFName>();
|
||||||
|
|
||||||
private SharedStringSource sharedStringSource;
|
private SharedStringSource sharedStringSource;
|
||||||
private StylesSource stylesSource;
|
private StylesSource stylesSource;
|
||||||
|
|
||||||
private MissingCellPolicy missingCellPolicy = Row.RETURN_NULL_AND_BLANK;
|
private MissingCellPolicy missingCellPolicy = Row.RETURN_NULL_AND_BLANK;
|
||||||
|
|
||||||
private static POILogger log = POILogFactory.getLogger(XSSFWorkbook.class);
|
private static POILogger log = POILogFactory.getLogger(XSSFWorkbook.class);
|
||||||
|
|
||||||
public XSSFWorkbook() {
|
public XSSFWorkbook() {
|
||||||
this.workbook = CTWorkbook.Factory.newInstance();
|
this.workbook = CTWorkbook.Factory.newInstance();
|
||||||
CTBookViews bvs = this.workbook.addNewBookViews();
|
CTBookViews bvs = this.workbook.addNewBookViews();
|
||||||
CTBookView bv = bvs.addNewWorkbookView();
|
CTBookView bv = bvs.addNewWorkbookView();
|
||||||
bv.setActiveTab(0);
|
bv.setActiveTab(0);
|
||||||
this.workbook.addNewSheets();
|
this.workbook.addNewSheets();
|
||||||
|
|
||||||
// We always require styles and shared strings
|
// We always require styles and shared strings
|
||||||
sharedStringSource = new SharedStringsTable();
|
sharedStringSource = new SharedStringsTable();
|
||||||
stylesSource = new StylesTable();
|
stylesSource = new StylesTable();
|
||||||
}
|
}
|
||||||
|
|
||||||
public XSSFWorkbook(String path) throws IOException {
|
public XSSFWorkbook(String path) throws IOException {
|
||||||
this(openPackage(path));
|
this(openPackage(path));
|
||||||
}
|
}
|
||||||
|
@ -108,11 +108,11 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
|
||||||
try {
|
try {
|
||||||
WorkbookDocument doc = WorkbookDocument.Factory.parse(getCorePart().getInputStream());
|
WorkbookDocument doc = WorkbookDocument.Factory.parse(getCorePart().getInputStream());
|
||||||
this.workbook = doc.getWorkbook();
|
this.workbook = doc.getWorkbook();
|
||||||
|
|
||||||
// Are we macro enabled, or just normal?
|
// Are we macro enabled, or just normal?
|
||||||
isMacroEnabled =
|
isMacroEnabled =
|
||||||
getCorePart().getContentType().equals(XSSFRelation.MACROS_WORKBOOK.getContentType());
|
getCorePart().getContentType().equals(XSSFRelation.MACROS_WORKBOOK.getContentType());
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Load shared strings
|
// Load shared strings
|
||||||
this.sharedStringSource = (SharedStringSource)
|
this.sharedStringSource = (SharedStringSource)
|
||||||
|
@ -128,7 +128,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
throw new IOException("Unable to load styles - " + e.toString());
|
throw new IOException("Unable to load styles - " + e.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load individual sheets
|
// Load individual sheets
|
||||||
for (CTSheet ctSheet : this.workbook.getSheets().getSheetArray()) {
|
for (CTSheet ctSheet : this.workbook.getSheets().getSheetArray()) {
|
||||||
PackagePart part = getPackagePart(ctSheet);
|
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");
|
log.log(POILogger.WARN, "Sheet with name " + ctSheet.getName() + " and r:id " + ctSheet.getId()+ " was defined, but didn't exist in package, skipping");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load child streams of the sheet
|
// Load child streams of the sheet
|
||||||
ArrayList<? extends XSSFModel> childModels;
|
ArrayList<? extends XSSFModel> childModels;
|
||||||
CommentsSource comments = null;
|
CommentsSource comments = null;
|
||||||
|
@ -148,7 +148,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
|
||||||
if(childModels.size() > 0) {
|
if(childModels.size() > 0) {
|
||||||
comments = (CommentsSource)childModels.get(0);
|
comments = (CommentsSource)childModels.get(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the drawings for the sheet, if there are any
|
// Get the drawings for the sheet, if there are any
|
||||||
drawings = (ArrayList<Drawing>)XSSFRelation.VML_DRAWINGS.loadAll(part);
|
drawings = (ArrayList<Drawing>)XSSFRelation.VML_DRAWINGS.loadAll(part);
|
||||||
// Get the activeX controls for the sheet, if there are any
|
// 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) {
|
} catch(Exception e) {
|
||||||
throw new RuntimeException("Unable to construct child part",e);
|
throw new RuntimeException("Unable to construct child part",e);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Now create the sheet
|
// Now create the sheet
|
||||||
WorksheetDocument worksheetDoc = WorksheetDocument.Factory.parse(part.getInputStream());
|
WorksheetDocument worksheetDoc = WorksheetDocument.Factory.parse(part.getInputStream());
|
||||||
XSSFSheet sheet = new XSSFSheet(ctSheet, worksheetDoc.getWorksheet(), this, comments, drawings, controls);
|
XSSFSheet sheet = new XSSFSheet(ctSheet, worksheetDoc.getWorksheet(), this, comments, drawings, controls);
|
||||||
this.sheets.add(sheet);
|
this.sheets.add(sheet);
|
||||||
|
|
||||||
// Process external hyperlinks for the sheet,
|
// Process external hyperlinks for the sheet,
|
||||||
// if there are any
|
// if there are any
|
||||||
PackageRelationshipCollection hyperlinkRels =
|
PackageRelationshipCollection hyperlinkRels =
|
||||||
part.getRelationshipsByType(XSSFRelation.SHEET_HYPERLINKS.getRelation());
|
part.getRelationshipsByType(XSSFRelation.SHEET_HYPERLINKS.getRelation());
|
||||||
sheet.initHyperlinks(hyperlinkRels);
|
sheet.initHyperlinks(hyperlinkRels);
|
||||||
|
|
||||||
// Get the embeddings for the workbook
|
// Get the embeddings for the workbook
|
||||||
for(PackageRelationship rel : part.getRelationshipsByType(XSSFRelation.OLEEMBEDDINGS.getRelation()))
|
for(PackageRelationship rel : part.getRelationshipsByType(XSSFRelation.OLEEMBEDDINGS.getRelation()))
|
||||||
embedds.add(getTargetPart(rel)); // TODO: Add this reference to each sheet as well
|
embedds.add(getTargetPart(rel)); // TODO: Add this reference to each sheet as well
|
||||||
|
|
||||||
for(PackageRelationship rel : part.getRelationshipsByType(XSSFRelation.PACKEMBEDDINGS.getRelation()))
|
for(PackageRelationship rel : part.getRelationshipsByType(XSSFRelation.PACKEMBEDDINGS.getRelation()))
|
||||||
embedds.add(getTargetPart(rel));
|
embedds.add(getTargetPart(rel));
|
||||||
}
|
}
|
||||||
|
@ -180,7 +180,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
|
||||||
} catch (InvalidFormatException e) {
|
} catch (InvalidFormatException e) {
|
||||||
throw new IOException(e.toString());
|
throw new IOException(e.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Process the named ranges
|
// Process the named ranges
|
||||||
if(workbook.getDefinedNames() != null) {
|
if(workbook.getDefinedNames() != null) {
|
||||||
for(CTDefinedName ctName : workbook.getDefinedNames().getDefinedNameArray()) {
|
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.
|
* Get the PackagePart corresponding to a given sheet.
|
||||||
*
|
*
|
||||||
* @param ctSheet The sheet
|
* @param ctSheet The sheet
|
||||||
* @return A PackagePart, or null if no matching part found.
|
* @return A PackagePart, or null if no matching part found.
|
||||||
* @throws InvalidFormatException
|
* @throws InvalidFormatException
|
||||||
|
@ -208,18 +208,13 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
|
||||||
}
|
}
|
||||||
return getTargetPart(rel);
|
return getTargetPart(rel);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int addPicture(byte[] pictureData, int format) {
|
public int addPicture(byte[] pictureData, int format) {
|
||||||
// TODO Auto-generated method stub
|
// TODO Auto-generated method stub
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int addSSTString(String string) {
|
public XSSFSheet cloneSheet(int sheetNum) {
|
||||||
// TODO Auto-generated method stub
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Sheet cloneSheet(int sheetNum) {
|
|
||||||
XSSFSheet srcSheet = sheets.get(sheetNum);
|
XSSFSheet srcSheet = sheets.get(sheetNum);
|
||||||
String srcName = getSheetName(sheetNum);
|
String srcName = getSheetName(sheetNum);
|
||||||
if (srcSheet != null) {
|
if (srcSheet != null) {
|
||||||
|
@ -228,7 +223,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
|
||||||
sheets.add(clonedSheet);
|
sheets.add(clonedSheet);
|
||||||
CTSheet newcts = this.workbook.getSheets().addNewSheet();
|
CTSheet newcts = this.workbook.getSheets().addNewSheet();
|
||||||
newcts.set(clonedSheet.getSheet());
|
newcts.set(clonedSheet.getSheet());
|
||||||
|
|
||||||
int i = 1;
|
int i = 1;
|
||||||
while (true) {
|
while (true) {
|
||||||
//Try and find the next sheet name that is unique
|
//Try and find the next sheet name that is unique
|
||||||
|
@ -251,7 +246,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public CellStyle createCellStyle() {
|
public XSSFCellStyle createCellStyle() {
|
||||||
return new XSSFCellStyle(stylesSource);
|
return new XSSFCellStyle(stylesSource);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -259,7 +254,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
|
||||||
return getCreationHelper().createDataFormat();
|
return getCreationHelper().createDataFormat();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Font createFont() {
|
public XSSFFont createFont() {
|
||||||
return new XSSFFont();
|
return new XSSFFont();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -269,22 +264,23 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Sheet createSheet() {
|
public XSSFSheet createSheet() {
|
||||||
return createSheet(null);
|
String sheetname = "Sheet" + (sheets.size() + 1);
|
||||||
|
return createSheet(sheetname);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Sheet createSheet(String sheetname) {
|
public XSSFSheet createSheet(String sheetname) {
|
||||||
return createSheet(sheetname, null);
|
return createSheet(sheetname, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Sheet createSheet(String sheetname, CTWorksheet worksheet) {
|
public XSSFSheet createSheet(String sheetname, CTWorksheet worksheet) {
|
||||||
CTSheet sheet = addSheet(sheetname);
|
CTSheet sheet = addSheet(sheetname);
|
||||||
XSSFWorksheet wrapper = new XSSFWorksheet(sheet, worksheet, this);
|
XSSFWorksheet wrapper = new XSSFWorksheet(sheet, worksheet, this);
|
||||||
this.sheets.add(wrapper);
|
this.sheets.add(wrapper);
|
||||||
return wrapper;
|
return wrapper;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Sheet createDialogsheet(String sheetname, CTDialogsheet dialogsheet) {
|
public XSSFSheet createDialogsheet(String sheetname, CTDialogsheet dialogsheet) {
|
||||||
CTSheet sheet = addSheet(sheetname);
|
CTSheet sheet = addSheet(sheetname);
|
||||||
XSSFDialogsheet wrapper = new XSSFDialogsheet(sheet, dialogsheet, this);
|
XSSFDialogsheet wrapper = new XSSFDialogsheet(sheet, dialogsheet, this);
|
||||||
this.sheets.add(wrapper);
|
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.
|
* get the first tab that is displayed in the list of tabs in excel.
|
||||||
*/
|
*/
|
||||||
public int getFirstVisibleTab() {
|
public int getFirstVisibleTab() {
|
||||||
CTBookViews bookViews = workbook.getBookViews();
|
CTBookViews bookViews = workbook.getBookViews();
|
||||||
CTBookView bookView = bookViews.getWorkbookViewArray(0);
|
CTBookView bookView = bookViews.getWorkbookViewArray(0);
|
||||||
|
@ -383,7 +379,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* deprecated Aug 2008
|
* deprecated Aug 2008
|
||||||
* @deprecated - Misleading name - use getFirstVisibleTab()
|
* @deprecated - Misleading name - use getFirstVisibleTab()
|
||||||
*/
|
*/
|
||||||
public short getDisplayedTab() {
|
public short getDisplayedTab() {
|
||||||
return (short) getFirstVisibleTab();
|
return (short) getFirstVisibleTab();
|
||||||
|
@ -407,7 +403,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
|
||||||
}
|
}
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TODO - figure out what the hell this methods does in
|
* TODO - figure out what the hell this methods does in
|
||||||
* HSSF...
|
* HSSF...
|
||||||
|
@ -439,10 +435,6 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getSSTString(int index) {
|
|
||||||
return getSharedStringSource().getSharedStringAt(index);
|
|
||||||
}
|
|
||||||
|
|
||||||
public short getSelectedTab() {
|
public short getSelectedTab() {
|
||||||
short i = 0;
|
short i = 0;
|
||||||
for (XSSFSheet sheet : this.sheets) {
|
for (XSSFSheet sheet : this.sheets) {
|
||||||
|
@ -453,7 +445,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
|
||||||
}
|
}
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Doesn't do anything - returns the same index
|
* Doesn't do anything - returns the same index
|
||||||
* TODO - figure out if this is a ole2 specific thing, or
|
* 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) {
|
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) {
|
for (int i = 0 ; i < sheets.length ; ++i) {
|
||||||
if (name.equals(sheets[i].getName())) {
|
if (name.equals(sheets[i].getName())) {
|
||||||
return this.sheets.get(i);
|
return this.sheets.get(i);
|
||||||
|
@ -486,7 +478,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getSheetIndex(String name) {
|
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) {
|
for (int i = 0 ; i < sheets.length ; ++i) {
|
||||||
if (name.equals(sheets[i].getName())) {
|
if (name.equals(sheets[i].getName())) {
|
||||||
return i;
|
return i;
|
||||||
|
@ -498,12 +490,12 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
|
||||||
public int getSheetIndex(Sheet sheet) {
|
public int getSheetIndex(Sheet sheet) {
|
||||||
return this.sheets.indexOf(sheet);
|
return this.sheets.indexOf(sheet);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the external sheet index of the sheet
|
* Returns the external sheet index of the sheet
|
||||||
* with the given internal index, creating one
|
* with the given internal index, creating one
|
||||||
* if needed.
|
* if needed.
|
||||||
* Used by some of the more obscure formula and
|
* Used by some of the more obscure formula and
|
||||||
* named range things.
|
* named range things.
|
||||||
* Fairly easy on XSSF (we think...) since the
|
* Fairly easy on XSSF (we think...) since the
|
||||||
* internal and external indicies are the same
|
* 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);
|
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) {
|
public void setSheetOrder(String sheetname, int pos) {
|
||||||
int idx = getSheetIndex(sheetname);
|
int idx = getSheetIndex(sheetname);
|
||||||
sheets.add(pos, sheets.remove(idx));
|
sheets.add(pos, sheets.remove(idx));
|
||||||
|
|
|
@ -24,6 +24,7 @@ import java.util.Iterator;
|
||||||
import junit.framework.TestCase;
|
import junit.framework.TestCase;
|
||||||
|
|
||||||
import org.apache.poi.util.IOUtils;
|
import org.apache.poi.util.IOUtils;
|
||||||
|
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
|
||||||
import org.openxml4j.opc.Package;
|
import org.openxml4j.opc.Package;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -76,8 +77,8 @@ public class TestXSSFReader extends TestCase {
|
||||||
|
|
||||||
XSSFReader r = new XSSFReader(pkg);
|
XSSFReader r = new XSSFReader(pkg);
|
||||||
|
|
||||||
assertEquals(11, r.getSharedStringsTable()._getNumberOfStrings());
|
assertEquals(11, r.getSharedStringsTable().getItems().size());
|
||||||
assertEquals("Test spreadsheet", r.getSharedStringsTable().getSharedStringAt(0));
|
assertEquals("Test spreadsheet", new XSSFRichTextString(r.getSharedStringsTable().getEntryAt(0)).toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testSheets() throws Exception {
|
public void testSheets() throws Exception {
|
||||||
|
|
Binary file not shown.
Loading…
Reference in New Issue