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
|
||||||
|
|
|
@ -93,18 +93,6 @@ public interface Workbook {
|
||||||
*/
|
*/
|
||||||
short getDisplayedTab();
|
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
|
* Will throw IllegalArgumentException if the name is greater than 31 chars
|
||||||
|
@ -113,16 +101,6 @@ public interface Workbook {
|
||||||
*/
|
*/
|
||||||
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
|
||||||
|
@ -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,25 +20,66 @@ 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>
|
||||||
|
* 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>
|
||||||
*
|
*
|
||||||
* @version $Id$
|
* @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
|
||||||
|
@ -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 an integer representing the total count of strings in the workbook. This count does not
|
||||||
return strings.indexOf(s);
|
* include any numbers, it counts only the total of text strings in the workbook.
|
||||||
}
|
*
|
||||||
strings.add(s);
|
* @return the total count of strings in the workbook
|
||||||
return strings.size() - 1;
|
*/
|
||||||
|
public int getCount(){
|
||||||
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* For unit testing only!
|
* 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 _getNumberOfStrings() {
|
public int getUniqueCount(){
|
||||||
return strings.size();
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -104,16 +194,15 @@ public class SharedStringsTable implements SharedStringSource, XSSFModel {
|
||||||
options.setSaveOuter();
|
options.setSaveOuter();
|
||||||
options.setUseDefaultNamespace();
|
options.setUseDefaultNamespace();
|
||||||
|
|
||||||
// Requests use of whitespace for easier reading
|
//re-create the sst table every time saving a workbook
|
||||||
options.setSavePrettyPrint();
|
|
||||||
|
|
||||||
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)) {
|
||||||
|
|
|
@ -214,12 +214,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
|
||||||
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) {
|
||||||
|
@ -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);
|
||||||
|
@ -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) {
|
||||||
|
@ -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