org.apache.poi.xssf.model

## Class SharedStringsTable

- java.lang.Object
- - org.apache.poi.ooxml.POIXMLDocumentPart
  - - org.apache.poi.xssf.model.SharedStringsTable

- All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable, SharedStrings

      public class SharedStringsTable
      extends POIXMLDocumentPart
      implements SharedStrings, java.io.Closeable

  Table of strings shared across all sheets in a workbook.

  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).

- - ### Nested Class Summary

    - ### Nested classes/interfaces inherited from class org.apache.poi.ooxml.POIXMLDocumentPart

      `POIXMLDocumentPart.RelationPart`

  - ### Field Summary

    Fields
    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 Summary

    Constructors
    Constructor and Description
    `SharedStringsTable()`
    `SharedStringsTable(PackagePart part)`

  - ### Method Summary

    All Methods Instance Methods Concrete Methods Deprecated Methods
    Modifier and Type | Method and Description
    --- | ---
    `int` | `addEntry(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst st)` Deprecated. use `addSharedStringItem(RichTextString string)` instead
    `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.
    `org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst` | `getEntryAt(int idx)` Deprecated. use `getItemAt(int idx)` instead
    `RichTextString` | `getItemAt(int idx)` Return a string item by index
    `java.util.List<org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst>` | `getItems()` Deprecated. use `getSharedStringItems` instead
    `java.util.List<RichTextString>` | `getSharedStringItems()` Provide access to the strings in the 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.
    `void` | `writeTo(java.io.OutputStream out)` Write this table out as XML.
    `protected java.lang.String` | `xmlText(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst st)`

    - ### Methods inherited from class org.apache.poi.ooxml.POIXMLDocumentPart

      `_invokeOnDocumentRead, addRelation, createRelationship, createRelationship, createRelationship, getNextPartNumber, getPackagePart, getParent, getRelationById, getRelationId, getRelationPartById, getRelationParts, getRelations, getTargetPart, isCommited, isCommitted, onDocumentCreate, onDocumentRead, onDocumentRemove, onSave, prepareForCommit, read, rebase, removeRelation, removeRelation, removeRelation, setCommited, setCommitted, toString`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

- - ### Field Detail

    - #### count

          protected int count

      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.

    - #### uniqueCount

          protected int uniqueCount

      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.

  - ### Constructor Detail

    - #### SharedStringsTable

          public SharedStringsTable()

    - #### SharedStringsTable

          public SharedStringsTable(PackagePart part)
                             throws java.io.IOException

      Throws:
        `java.io.IOException`
      Since:
        POI 3.14-Beta1

  - ### Method Detail

    - #### readFrom

          public void readFrom(java.io.InputStream is)
                        throws java.io.IOException

      Read this shared strings table from an XML file.
      Parameters:
        `is` - The input stream containing the XML document.
      Throws:
        `java.io.IOException` - if an error occurs while reading.

    - #### xmlText

          protected java.lang.String xmlText(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst st)

    - #### getEntryAt

          @Removal(version="4.2")
          public org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst getEntryAt(int idx)

      Deprecated. use `getItemAt(int idx)` instead
      Return a string item by index
      Parameters:
        `idx` - index of item to return.
      Returns:
        the item at the specified position in this Shared String table.

    - #### getItemAt

          public RichTextString getItemAt(int idx)

      Return a string item by index
      Specified by:
        `getItemAt` in interface `SharedStrings`
      Parameters:
        `idx` - index of item to return.
      Returns:
        the item at the specified position in this Shared String table.

    - #### getCount

          public int getCount()

      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.
      Specified by:
        `getCount` in interface `SharedStrings`
      Returns:
        the total count of strings in the workbook

    - #### getUniqueCount

          public int getUniqueCount()

      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.
      Specified by:
        `getUniqueCount` in interface `SharedStrings`
      Returns:
        the total count of unique strings in the workbook

    - #### addEntry

          @Removal(version="4.2")
          public int addEntry(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst st)

      Deprecated. use `addSharedStringItem(RichTextString string)` instead
      Add an entry to this Shared String table (a new value is appended to the end).

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

      Parameters:
        `st` - the entry to add
      Returns:
        index the index of added entry

    - #### addSharedStringItem

          public int addSharedStringItem(RichTextString string)

      Add an entry to this Shared String table (a new value is appended to the end).

      If the Shared String table already contains this string entry, its index is returned. Otherwise a new entry is added.

      Parameters:
        `string` - the entry to add
      Returns:
        index the index of added entry
      Since:
        POI 4.0.0

    - #### getItems

          @Removal(version="4.2")
          public java.util.List<org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst> getItems()

      Deprecated. use `getSharedStringItems` instead
      Provide low-level access to the underlying array of CTRst beans
      Returns:
        array of CTRst beans

    - #### getSharedStringItems

          public java.util.List<RichTextString> getSharedStringItems()

      Provide access to the strings in the SharedStringsTable
      Returns:
        list of shared string instances

    - #### writeTo

          public void writeTo(java.io.OutputStream out)
                       throws java.io.IOException

      Write this table out as XML.
      Parameters:
        `out` - The stream to write to.
      Throws:
        `java.io.IOException` - if an error occurs while writing.

    - #### commit

          protected void commit()
                         throws java.io.IOException

      Description copied from class: `POIXMLDocumentPart`
      Save the content in the underlying package part. Default implementation is empty meaning that the package part is left unmodified.

      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();
           }

      Overrides:
        `commit` in class `POIXMLDocumentPart`
      Throws:
        `java.io.IOException` - a subclass may throw an IOException if the changes can't be committed

    - #### close

          public void close()
                     throws java.io.IOException

      Close any open resources, like temp files. This method is called by `XSSFWorkbook#close()`.

      This implementation is empty but subclasses may need to implement some logic.

      Specified by:
        `close` in interface `java.io.Closeable`
      Specified by:
        `close` in interface `java.lang.AutoCloseable`
      Throws:
        `java.io.IOException` - if an error occurs while closing.
      Since:
        4.0.0

Copyright 2020 The Apache Software Foundation or its licensors, as applicable.