public class SharedStringsTable extends POIXMLDocumentPart implements SharedStrings, java.io.Closeable
A workbook may contain thousands of cells containing string (non-numeric) data. Furthermore this data is very likely to be repeated across many rows or columns. The goal of implementing a single string table that is shared across the workbook is to improve performance in opening and saving the file by only reading and writing the repetitive information once.
Consider for example a workbook summarizing information for cities within various countries. There may be a column for the name of the country, a column for the name of each city in that country, and a column containing the data for each city. In this case the country name is repetitive, being duplicated in many cells. In many cases the repetition is extensive, and a tremendous savings is realized by making use of a shared string table when saving the workbook. When displaying text in the spreadsheet, the cell table will just contain an index into the string table as the value of a cell, instead of the full string.
The shared string table contains all the necessary information for displaying the string: the text, formatting properties, and phonetic properties (for East Asian languages).
(工作簿中所有工作表共享的字符串表。工作簿可能包含数千个包含字符串(非数字)数据的单元格。此外,这些数据很可能在许多行或列中重复。实现跨工作簿共享的单个字符串表的目标是通过仅读取和写入重复信息一次来提高打开和保存文件的性能。例如,考虑一个总结各国城市信息的工作簿。可能有一列是国家名称,一列是该国家每个城市的名称,还有一列包含每个城市的数据。在这种情况下,国家名称是重复的,在许多单元格中重复。在许多情况下,重复是广泛的,并且通过在保存工作簿时使用共享字符串表实现了巨大的节省。在电子表格中显示文本时,单元格表将仅包含字符串表的索引作为单元格的值,而不是完整的字符串。共享字符串表包含显示字符串所需的所有信息:文本、格式属性和语音属性(针对东亚语言)。)POIXMLDocumentPart.RelationPart
Modifier and Type | Field and Description |
---|---|
protected int |
count
An integer representing the total count of strings in the workbook.
(一个整数,表示工作簿中的字符串总数。)
|
protected int |
uniqueCount
An integer representing the total count of unique strings in the Shared String Table.
(一个整数,表示共享字符串表中唯一字符串的总数。)
|
Constructor and Description |
---|
SharedStringsTable() |
SharedStringsTable(PackagePart part) |
Modifier and Type | Method and Description |
---|---|
int |
addSharedStringItem(RichTextString string)
Add an entry to this Shared String table (a new value is appended to the end).
(向此共享字符串表添加一个条目(一个新值附加到末尾)。)
|
void |
close()
Close any open resources, like temp files.
(关闭所有打开的资源,例如临时文件。)
|
protected void |
commit()
Save the content in the underlying package part.
(将内容保存在底层包部分中。)
|
int |
getCount()
Return an integer representing the total count of strings in the workbook.
(返回一个整数,表示工作簿中的字符串总数。)
|
RichTextString |
getItemAt(int idx)
Return a string item by index
(按索引返回字符串项)
|
java.util.List<RichTextString> |
getSharedStringItems()
Provide access to the strings in the SharedStringsTable
(提供对 SharedStringsTable 中字符串的访问)
|
int |
getUniqueCount()
Returns an integer representing the total count of unique strings in the Shared String Table.
(返回一个整数,表示共享字符串表中唯一字符串的总数。)
|
void |
readFrom(java.io.InputStream is)
Read this shared strings table from an XML file.
(从 XML 文件中读取此共享字符串表。)
|
void |
writeTo(java.io.OutputStream out)
Write this table out as XML.
(将此表写为 XML。)
|
protected java.lang.String |
xmlText(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst st) |
_invokeOnDocumentRead, addRelation, createRelationship, createRelationship, createRelationship, getNextPartNumber, getPackagePart, getParent, getRelationById, getRelationId, getRelationPartById, getRelationParts, getRelations, getTargetPart, isCommitted, onDocumentCreate, onDocumentRead, onDocumentRemove, onSave, prepareForCommit, read, rebase, removeRelation, removeRelation, removeRelation, setCommitted, toString
protected int count
protected int uniqueCount
public SharedStringsTable()
public SharedStringsTable(PackagePart part) throws java.io.IOException
java.io.IOException
(java.io.IOException)
public void readFrom(java.io.InputStream is) throws java.io.IOException
is
- The input stream containing the XML document.
(is - 包含 XML 文档的输入流。)
java.io.IOException
- if an error occurs while reading.
(java.io.IOException - 如果读取时发生错误。)
protected java.lang.String xmlText(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst st)
public RichTextString getItemAt(int idx)
getItemAt
in interface
SharedStrings
(接口 SharedStrings 中的 getItemAt)
idx
- index of item to return.
(idx - 要返回的项目的索引。)
public int getCount()
getCount
in interface
SharedStrings
(接口 SharedStrings 中的 getCount)
public int getUniqueCount()
getUniqueCount
in interface
SharedStrings
(接口 SharedStrings 中的 getUniqueCount)
public int addSharedStringItem(RichTextString string)
If the Shared String table already contains this string entry, its index is returned. Otherwise a new entry is added.
(向此共享字符串表添加一个条目(一个新值附加到末尾)。如果共享字符串表已经包含此字符串条目,则返回其索引。否则会添加一个新条目。)string
- the entry to add
(string - 要添加的条目)
public java.util.List<RichTextString> getSharedStringItems()
public void writeTo(java.io.OutputStream out) throws java.io.IOException
out
- The stream to write to.
(out - 要写入的流。)
java.io.IOException
- if an error occurs while writing.
(java.io.IOException - 如果写入时发生错误。)
protected void commit() throws java.io.IOException
POIXMLDocumentPart
Sub-classes should override and add logic to marshal the "model" into Ooxml4J.
For example, the code saving a generic XML entry may look as follows:
protected void commit() throws IOException { PackagePart part = getPackagePart(); OutputStream out = part.getOutputStream(); XmlObject bean = getXmlBean(); //the "model" which holds changes in memory bean.save(out, DEFAULT_XML_OPTIONS); out.close(); }(将内容保存在底层包部分中。默认实现是空的,这意味着包部分保持不变。子类应覆盖并添加逻辑以将“模型”编组到 Ooxml4J。例如,保存通用 XML 条目的代码可能如下所示: protected void commit() throws IOException { PackagePart part = getPackagePart();输出流输出 = part.getOutputStream(); XmlObject bean = getXmlBean(); //保存内存变化的“模型” bean.save(out, DEFAULT_XML_OPTIONS); out.close(); })
commit
in class
POIXMLDocumentPart
java.io.IOException
- a subclass may throw an IOException if the changes can't be committed
(java.io.IOException - 如果无法提交更改,子类可能会抛出 IOException)
public void close() throws java.io.IOException
XSSFWorkbook#close()
.
This implementation is empty but subclasses may need to implement some logic.
(关闭所有打开的资源,例如临时文件。此方法由 XSSFWorkbook#close() 调用。这个实现是空的,但是子类可能需要实现一些逻辑。)close
in interface
java.io.Closeable
(在接口 java.io.Closeable 中关闭)
close
in interface
java.lang.AutoCloseable
(在接口 java.lang.AutoCloseable 中关闭)
java.io.IOException
- if an error occurs while closing.
(java.io.IOException - 如果关闭时发生错误。)
Copyright 2021 The Apache Software Foundation or its licensors, as applicable.