org.apache.poi.xssf.model
Class StylesTable

java.lang.Object
  extended by org.apache.poi.POIXMLDocumentPart
      extended by org.apache.poi.xssf.model.StylesTable

public class StylesTable
extends POIXMLDocumentPart

Table of styles shared across all sheets in a workbook.


Nested Class Summary
 
Nested classes/interfaces inherited from class org.apache.poi.POIXMLDocumentPart
POIXMLDocumentPart.RelationPart
 
Field Summary
static int FIRST_CUSTOM_STYLE_ID
          The first style id available for use as a custom style
 
Constructor Summary
StylesTable()
          Create a new, empty StylesTable
StylesTable(PackagePart part)
           
 
Method Summary
 int _getDXfsSize()
           
 int _getStyleXfsSize()
          For unit testing only
protected  void commit()
          Save the content in the underlying package part.
 XSSFCellStyle createCellStyle()
          Create a cell style in this style table.
 void ensureThemesTable()
          If there isn't currently a ThemesTable for the current Workbook, then creates one and sets it up.
 XSSFFont findFont(boolean bold, short color, short fontHeight, java.lang.String name, boolean italic, boolean strikeout, short typeOffset, byte underline)
          Finds a font that matches the one with the supplied attributes
 XSSFCellBorder getBorderAt(int idx)
           
 java.util.List<XSSFCellBorder> getBorders()
           
 org.openxmlformats.schemas.spreadsheetml.x2006.main.CTXf getCellStyleXfAt(int idx)
           
 org.openxmlformats.schemas.spreadsheetml.x2006.main.CTXf getCellXfAt(int idx)
           
 org.openxmlformats.schemas.spreadsheetml.x2006.main.CTStylesheet getCTStylesheet()
          For unit testing only!
 org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDxf getDxfAt(int idx)
           
 TableStyle getExplicitTableStyle(java.lang.String name)
          NOTE: this only returns explicitly defined styles
 java.util.Set<java.lang.String> getExplicitTableStyleNames()
           
 XSSFCellFill getFillAt(int idx)
           
 java.util.List<XSSFCellFill> getFills()
           
 XSSFFont getFontAt(int idx)
           
 java.util.List<XSSFFont> getFonts()
           
 IndexedColorMap getIndexedColors()
           
 int getMaxNumberOfDataFormats()
          Get the upper limit on the number of data formats that has been set for the style table.
 java.lang.String getNumberFormatAt(short fmtId)
          Get number format string given its id
 java.util.Map<java.lang.Short,java.lang.String> getNumberFormats()
           
 int getNumCellStyles()
          get the size of cell styles
 int getNumDataFormats()
           
 XSSFCellStyle getStyleAt(int idx)
           
 TableStyle getTableStyle(java.lang.String name)
           
 ThemesTable getTheme()
          Get the current Workbook's theme table, or null if the Workbook lacks any themes.
 int putBorder(XSSFCellBorder border)
          Adds a border to the border style table if it isn't already in the style table Does nothing if border is already in borders style table
 int putCellStyleXf(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTXf cellStyleXf)
          Adds a cell style to the styles table.
 int putCellXf(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTXf cellXf)
          Adds a cell to the styles table.
 int putDxf(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDxf dxf)
          Adds a Dxf to the style table Does not check for duplicates.
 int putFill(XSSFCellFill fill)
          Adds a fill to the fill style table if it isn't already in the style table Does nothing if fill is already in fill style table
 int putFont(XSSFFont font)
           
 int putFont(XSSFFont font, boolean forceRegistration)
          Records the given font in the font table.
 void putNumberFormat(short index, java.lang.String fmt)
          Add a number format with a specific ID into the numberFormats map.
 int putNumberFormat(java.lang.String fmt)
          Puts fmt in the numberFormats map if the format is not already in the the number format style table.
 int putStyle(XSSFCellStyle style)
           
 void readFrom(java.io.InputStream is)
          Read this shared styles table from an XML file.
 boolean removeNumberFormat(short index)
          Remove a number format from the style table if it exists.
 boolean removeNumberFormat(java.lang.String fmt)
          Remove a number format from the style table if it exists All cell styles with this number format will be modified to use the default number format
protected  void replaceCellStyleXfAt(int idx, org.openxmlformats.schemas.spreadsheetml.x2006.main.CTXf cellStyleXf)
           
 void replaceCellXfAt(int idx, org.openxmlformats.schemas.spreadsheetml.x2006.main.CTXf cellXf)
           
 void setMaxNumberOfDataFormats(int num)
          Changes the maximum number of data formats that may be in a style table
 void setTheme(ThemesTable theme)
           
 void setWorkbook(XSSFWorkbook wb)
           
 void writeTo(java.io.OutputStream out)
          Write this table out as XML.
 
Methods inherited from class org.apache.poi.POIXMLDocumentPart
_invokeOnDocumentRead, addRelation, createRelationship, createRelationship, createRelationship, getNextPartNumber, getPackagePart, getParent, getRelationById, getRelationId, getRelationParts, getRelations, getTargetPart, onDocumentCreate, onDocumentRead, onDocumentRemove, onSave, prepareForCommit, read, rebase, removeRelation, removeRelation, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

FIRST_CUSTOM_STYLE_ID

public static final int FIRST_CUSTOM_STYLE_ID
The first style id available for use as a custom style

See Also:
Constant Field Values
Constructor Detail

StylesTable

public StylesTable()
Create a new, empty StylesTable


StylesTable

public StylesTable(PackagePart part)
            throws java.io.IOException
Throws:
java.io.IOException
Since:
POI 3.14-Beta1
Method Detail

setMaxNumberOfDataFormats

public void setMaxNumberOfDataFormats(int num)
Changes the maximum number of data formats that may be in a style table

Parameters:
num - the upper limit on number of data formats in the styles table when adding new data formats
Throws:
java.lang.IllegalArgumentException - if num < 0
java.lang.IllegalStateException - if num < current number of data formats in the style table. Data formats must be explicitly removed before the limit can be decreased.

getMaxNumberOfDataFormats

public int getMaxNumberOfDataFormats()
Get the upper limit on the number of data formats that has been set for the style table. To get the current number of data formats in use, use getNumDataFormats().

Returns:
the maximum number of data formats allowed in the workbook

setWorkbook

public void setWorkbook(XSSFWorkbook wb)

getTheme

public ThemesTable getTheme()
Get the current Workbook's theme table, or null if the Workbook lacks any themes.

Use ensureThemesTable() to have a themes table created if needed


setTheme

public void setTheme(ThemesTable theme)

ensureThemesTable

public void ensureThemesTable()
If there isn't currently a ThemesTable for the current Workbook, then creates one and sets it up. After this, calls to getTheme() won't give null


readFrom

public void readFrom(java.io.InputStream is)
              throws java.io.IOException
Read this shared styles table from an XML file.

Parameters:
is - The input stream containing the XML document.
Throws:
java.io.IOException - if an error occurs while reading.

getNumberFormatAt

public java.lang.String getNumberFormatAt(short fmtId)
Get number format string given its id

Parameters:
fmtId - number format id
Returns:
number format code

putNumberFormat

public int putNumberFormat(java.lang.String fmt)
Puts fmt in the numberFormats map if the format is not already in the the number format style table. Does nothing if fmt is already in number format style table.

Parameters:
fmt - the number format to add to number format style table
Returns:
the index of fmt in the number format style table
Throws:
java.lang.IllegalStateException - if adding the number format to the styles table would exceed the MAXIMUM_NUMBER_OF_DATA_FORMATS allowed.

putNumberFormat

public void putNumberFormat(short index,
                            java.lang.String fmt)
Add a number format with a specific ID into the numberFormats map. If a format with the same ID already exists, overwrite the format code with fmt This may be used to override built-in number formats.

Parameters:
index - the number format ID
fmt - the number format code

removeNumberFormat

public boolean removeNumberFormat(short index)
Remove a number format from the style table if it exists. All cell styles with this number format will be modified to use the default number format.

Parameters:
index - the number format id to remove
Returns:
true if the number format was removed

removeNumberFormat

public boolean removeNumberFormat(java.lang.String fmt)
Remove a number format from the style table if it exists All cell styles with this number format will be modified to use the default number format

Parameters:
fmt - the number format to remove
Returns:
true if the number format was removed

getFontAt

public XSSFFont getFontAt(int idx)

putFont

public int putFont(XSSFFont font,
                   boolean forceRegistration)
Records the given font in the font table. Will re-use an existing font index if this font matches another, EXCEPT if forced registration is requested. This allows people to create several fonts then customise them later. Note - End Users probably want to call XSSFFont.registerTo(StylesTable)


putFont

public int putFont(XSSFFont font)

getStyleAt

public XSSFCellStyle getStyleAt(int idx)
Parameters:
idx - style index
Returns:
XSSFCellStyle or null if idx is out of bounds for xfs array

putStyle

public int putStyle(XSSFCellStyle style)

getBorderAt

public XSSFCellBorder getBorderAt(int idx)

putBorder

public int putBorder(XSSFCellBorder border)
Adds a border to the border style table if it isn't already in the style table Does nothing if border is already in borders style table

Parameters:
border - border to add
Returns:
the index of the added border

getFillAt

public XSSFCellFill getFillAt(int idx)

getBorders

public java.util.List<XSSFCellBorder> getBorders()

getFills

public java.util.List<XSSFCellFill> getFills()

getFonts

public java.util.List<XSSFFont> getFonts()

getNumberFormats

public java.util.Map<java.lang.Short,java.lang.String> getNumberFormats()

putFill

public int putFill(XSSFCellFill fill)
Adds a fill to the fill style table if it isn't already in the style table Does nothing if fill is already in fill style table

Parameters:
fill - fill to add
Returns:
the index of the added fill

getCellXfAt

@Internal
public org.openxmlformats.schemas.spreadsheetml.x2006.main.CTXf getCellXfAt(int idx)

putCellXf

@Internal
public int putCellXf(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTXf cellXf)
Adds a cell to the styles table. Does not check for duplicates.

Parameters:
cellXf - the cell to add to the styles table
Returns:
the added cell ID in the style table

replaceCellXfAt

@Internal
public void replaceCellXfAt(int idx,
                                     org.openxmlformats.schemas.spreadsheetml.x2006.main.CTXf cellXf)

getCellStyleXfAt

@Internal
public org.openxmlformats.schemas.spreadsheetml.x2006.main.CTXf getCellStyleXfAt(int idx)

putCellStyleXf

@Internal
public int putCellStyleXf(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTXf cellStyleXf)
Adds a cell style to the styles table. Does not check for duplicates.

Parameters:
cellStyleXf - the cell style to add to the styles table
Returns:
the cell style ID in the style table

replaceCellStyleXfAt

@Internal
protected void replaceCellStyleXfAt(int idx,
                                             org.openxmlformats.schemas.spreadsheetml.x2006.main.CTXf cellStyleXf)

getNumCellStyles

public int getNumCellStyles()
get the size of cell styles


getNumDataFormats

public int getNumDataFormats()
Returns:
number of data formats in the styles table

_getStyleXfsSize

@Internal
public int _getStyleXfsSize()
For unit testing only


getCTStylesheet

@Internal
public org.openxmlformats.schemas.spreadsheetml.x2006.main.CTStylesheet getCTStylesheet()
For unit testing only!


_getDXfsSize

@Internal
public int _getDXfsSize()

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

getDxfAt

@Internal
public org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDxf getDxfAt(int idx)

putDxf

@Internal
public int putDxf(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDxf dxf)
Adds a Dxf to the style table Does not check for duplicates.

Parameters:
dxf - the Dxf to add
Returns:
added dxf ID in the style table

getExplicitTableStyle

public TableStyle getExplicitTableStyle(java.lang.String name)
NOTE: this only returns explicitly defined styles

Parameters:
name - of the table style
Returns:
defined style, or null if not explicitly defined
Since:
3.17 beta 1

getExplicitTableStyleNames

public java.util.Set<java.lang.String> getExplicitTableStyleNames()
Returns:
names of all explicitly defined table styles in the workbook
Since:
3.17 beta 1

getTableStyle

public TableStyle getTableStyle(java.lang.String name)
Parameters:
name - of the table style
Returns:
defined style, either explicit or built-in, or null if not found
Since:
3.17 beta 1

createCellStyle

public XSSFCellStyle createCellStyle()
Create a cell style in this style table. Note - End users probably want to call XSSFWorkbook.createCellStyle() rather than working with the styles table directly.

Throws:
java.lang.IllegalStateException - if the maximum number of cell styles has been reached.

findFont

public XSSFFont findFont(boolean bold,
                         short color,
                         short fontHeight,
                         java.lang.String name,
                         boolean italic,
                         boolean strikeout,
                         short typeOffset,
                         byte underline)
Finds a font that matches the one with the supplied attributes


getIndexedColors

public IndexedColorMap getIndexedColors()
Returns:
default or custom indexed color to RGB mapping