Top Description Inners Fields Constructors Methods
org.apache.poi.hssf.usermodel

public final Class HSSFWorkbook

extends POIDocument
implements Workbook
Class Inheritance
All Implemented Interfaces
org.apache.poi.ss.usermodel.Workbook, java.lang.Iterable, java.io.Closeable, java.lang.AutoCloseable
Annotations
@SuppressWarnings:WeakerAccess
Static Imports
org.apache.logging.log4j.util.Unbox.box, org.apache.poi.hssf.model.InternalWorkbook.BOOK, .InternalWorkbook.OLD_WORKBOOK_DIR_ENTRY_NAME, .InternalWorkbook.WORKBOOK, .InternalWorkbook.WORKBOOK_DIR_ENTRY_NAMES

High level representation of a workbook. This is the first object most users will construct whether they are reading or writing a workbook. It is also the top level object for creating new sheets/etc.
See Also
InternalWorkbook, HSSFSheet

Nested and Inner Type Summary

Modifier and TypeClass and Description
private class
private static class
HSSFWorkbook.SheetRecordCollector

Totals the sizes of all sheet records and eventually serializes them

Field Summary

Modifier and TypeField and Description
protected List<HSSFSheet>
_sheets

this holds the HSSFSheet objects attached to this workbook

private final UDFFinder
_udfFinder

The locator of user-defined functions.

private static final Pattern
private static final int
private static final int
private Map<Integer, HSSFFont>
fonts

this holds the HSSFFont objects attached to this workbook.

private HSSFDataFormat
formatter

Used to keep track of the data formatter so that all createDataFormatter calls return the same one for a given book.

public static final int
INITIAL_CAPACITY

used for compile-time performance/memory optimization.

private static final Logger
private static int
private static int
private static final int
MAX_STYLES

The maximum number of cell styles in a .xls workbook.

private Row.MissingCellPolicy
missingCellPolicy

The policy to apply in the event of missing or blank cells when fetching from a row.

private final ArrayList<HSSFName>
names

this holds the HSSFName objects attached to this workbook

private boolean
preserveNodes

holds whether or not to preserve other nodes in the POIFS.

private InternalWorkbook
workbook

this is the reference to the low level Workbook object

Constructor Summary

AccessConstructor and Description
public
HSSFWorkbook()

Creates new HSSFWorkbook from scratch (start here!)

private
public
HSSFWorkbook(POIFSFileSystem fs)

Given a POI POIFSFileSystem object, read in its Workbook along with all related nodes, and populate the high and low level models.

public
HSSFWorkbook(POIFSFileSystem
the POI filesystem that contains the Workbook stream.
fs
,
boolean
whether to preserve other nodes, such as macros. This takes more memory, so only say yes if you need to. If set, will store all of the POIFSFileSystem in memory
preserveNodes
)

Given a POI POIFSFileSystem object, read in its Workbook and populate the high and low level models.

public
HSSFWorkbook(DirectoryNode
the POI filesystem directory to process from
directory
,
POIFSFileSystem
the POI filesystem that contains the Workbook stream.
fs
,
boolean
whether to preserve other nodes, such as macros. This takes more memory, so only say yes if you need to. If set, will store all of the POIFSFileSystem in memory
preserveNodes
)

given a POI POIFSFileSystem object, and a specific directory within it, read in its Workbook and populate the high and low level models.

public
HSSFWorkbook(DirectoryNode
the POI filesystem directory to process from
directory
,
boolean
whether to preserve other nodes, such as macros. This takes more memory, so only say yes if you need to. If set, will store all of the POIFSFileSystem in memory
preserveNodes
)

given a POI POIFSFileSystem object, and a specific directory within it, read in its Workbook and populate the high and low level models.

public
HSSFWorkbook(InputStream s)

Companion to HSSFWorkbook(POIFSFileSystem), this constructs the POI filesystem around your InputStream, including all nodes.

public
HSSFWorkbook(InputStream
the POI filesystem that contains the Workbook stream.
s
,
boolean
whether to preserve other nodes, such as macros. This takes more memory, so only say yes if you need to.
preserveNodes
)

Companion to HSSFWorkbook(POIFSFileSystem), this constructs the POI filesystem around your InputStream.

Method Summary

Modifier and TypeMethod and Description
public int

Returns:

the index of the added ole object
addOlePackage
(POIFSFileSystem
an POIFS containing the embedded document, to be added
poiData
,
String
the label of the payload
label
,
String
the original filename
fileName
,
String
the command to open the payload
command
)

Adds an OLE package manager object with the given POIFS to the sheet

public int
addOlePackage(byte[]
the payload
oleData
,
String
the label of the payload
label
,
String
the original filename
fileName
,
String
the command to open the payload
command
)

Implements org.apache.poi.ss.usermodel.Workbook.addOlePackage.

Adds an OLE package manager object with the given content to the sheet

public int

Returns:

the index to this picture (1 based).
addPicture
(byte[]
The bytes of the picture
pictureData
,
int
The format of the picture. One of PICTURE_TYPE_*
format
)

Implements org.apache.poi.ss.usermodel.Workbook.addPicture.

Adds a picture to the workbook.

public void
addToolPack(UDFFinder
the toolpack to register
toolpack
)

Implements org.apache.poi.ss.usermodel.Workbook.addToolPack.

Register a new toolpack in this workbook.

public boolean

Returns:

true if the oldUrl was found and replaced with newUrl. Otherwise false
changeExternalReference
(String
The old URL to search for and which is to be replaced
oldUrl
,
String
The URL replacement
newUrl
)

Changes an external referenced file to another file.

public HSSFSheet

Returns:

HSSFSheet representing the cloned sheet.
cloneSheet
(int sheetIndex)

Implements org.apache.poi.ss.usermodel.Workbook.cloneSheet.

create an HSSFSheet from an existing sheet in the HSSFWorkbook.

public void
close()

Overrides org.apache.poi.POIDocument.close.

Implements org.apache.poi.ss.usermodel.Workbook.close, java.io.Closeable.close.

Closes the underlying POIFSFileSystem from which the Workbook was read, if any.

private void
convertLabelRecords(List<Record>
a collection of sheet's records.
records
,
int
the offset to search at
offset
)

This is basically a kludge to deal with the now obsolete Label records.

public static HSSFWorkbook
pack-priv HSSFName
createBuiltInName(byte builtinCode, int sheetIndex)

public HSSFCellStyle

Returns:

the new Cell Style object
createCellStyle
()

Implements org.apache.poi.ss.usermodel.Workbook.createCellStyle.

Create a new Cell style and add it to the workbook's style table.

public HSSFDataFormat

Returns:

the HSSFDataFormat object
createDataFormat
()

Implements org.apache.poi.ss.usermodel.Workbook.createDataFormat.

Returns the instance of HSSFDataFormat for this workbook.

public HSSFEvaluationWorkbook
public HSSFFont

Returns:

new font object
createFont
()

Implements org.apache.poi.ss.usermodel.Workbook.createFont.

create a new Font and add it to the workbook's font table

public HSSFName

Returns:

named range high level
createName
()

Implements org.apache.poi.ss.usermodel.Workbook.createName.

creates a new named range and add it to the model

public HSSFSheet

Returns:

HSSFSheet representing the new sheet.
createSheet
()

Implements org.apache.poi.ss.usermodel.Workbook.createSheet.

create an HSSFSheet for this HSSFWorkbook, adds it to the sheets and returns the high level representation.

public HSSFSheet

Returns:

Sheet representing the new sheet.
createSheet
(String
sheetname to set for the sheet.
sheetname
)

Implements org.apache.poi.ss.usermodel.Workbook.createSheet.

Create a new sheet for this Workbook and return the high level representation.

public void
dumpDrawingGroupRecords(boolean fat)

Spits out a list of all the drawing records in the workbook.

pack-priv void
encryptBytes(byte[] buf)

pack-priv int
findExistingBuiltinNameRecordIdx(int sheetIndex, byte builtinCode)

public HSSFFont
findFont(boolean bold, short color, short fontHeight, String name, boolean italic, boolean strikeout, short typeOffset, byte underline)

Implements org.apache.poi.ss.usermodel.Workbook.findFont.

Finds a font that matches the one with the supplied attributes

public int
getActiveSheetIndex()

Implements org.apache.poi.ss.usermodel.Workbook.getActiveSheetIndex.

gets the tab whose data is actually seen when the sheet is opened.

public List<HSSFObjectData>

Returns:

the list of embedded objects (a list of HSSFObjectData objects.)
getAllEmbeddedObjects
()

Gets all embedded OLE2 objects from the Workbook.

private void
getAllEmbeddedObjects(HSSFSheet
embedded object attached to
sheet
,
List<HSSFObjectData>
the list of embedded objects to populate.
objects
)

Gets all embedded OLE2 objects from the Workbook.

private void
getAllEmbeddedObjects(HSSFShapeContainer
the parent.
parent
,
List<HSSFObjectData>
the list of embedded objects to populate.
objects
)

Recursively iterates a shape container to get all embedded objects.

public List<HSSFName>
getAllNames()

Implements org.apache.poi.ss.usermodel.Workbook.getAllNames.

Returns all defined names.

public List<HSSFPictureData>

Returns:

the list of pictures (a list of HSSFPictureData objects.)
getAllPictures
()

Implements org.apache.poi.ss.usermodel.Workbook.getAllPictures.

Gets all pictures from the Workbook.

public boolean

Returns:

the current setting for backups.
getBackupFlag
()

determine whether the Excel GUI will backup the workbook when saving.

pack-priv HSSFName
getBuiltInName(byte builtinCode, int sheetIndex)

public byte[]

Returns:

byte[] array containing the binary representation of this workbook and all contained sheets, rows, cells, etc.
getBytes
()

Method getBytes - get the bytes of just the HSSF portions of the XLS file.

public CellReferenceType
public HSSFCellStyle

Returns:

HSSFCellStyle object at the index
getCellStyleAt
(int
index within the set of styles
idx
)

Implements org.apache.poi.ss.usermodel.Workbook.getCellStyleAt.

get the cell style object at the given index

public HSSFCreationHelper
getCreationHelper()

Implements org.apache.poi.ss.usermodel.Workbook.getCreationHelper.

Returns an object that handles instantiating concrete classes of the various instances one needs for HSSF and XSSF.

public HSSFPalette
public EncryptionInfo
public EncryptionMode

Returns:

the encryption mode or null if unset
getEncryptionMode
()

public int
getFirstVisibleTab()

Implements org.apache.poi.ss.usermodel.Workbook.getFirstVisibleTab.

sets the first tab that is displayed in the list of tabs in excel.

public HSSFFont
getFontAt(int
index number (0-based)
idx
)

Implements org.apache.poi.ss.usermodel.Workbook.getFontAt.

Get the font at the given index number

public boolean
getForceFormulaRecalculation()

Implements org.apache.poi.ss.usermodel.Workbook.getForceFormulaRecalculation.

Whether Excel will be asked to recalculate all formulas when the workbook is opened.

public InternalWorkbook
public static int

Returns:

the max image length allowed for HSSFWorkbook
getMaxImageLength
()

public static int

Returns:

the max record length allowed for HSSFWorkbook
getMaxRecordLength
()

public Row.MissingCellPolicy
getMissingCellPolicy()

Implements org.apache.poi.ss.usermodel.Workbook.getMissingCellPolicy.

Retrieves the current policy on what to do when getting missing or blank cells from a row.

public HSSFName
getName(String
the name of the defined name
name
)

Implements org.apache.poi.ss.usermodel.Workbook.getName.

pack-priv HSSFName
getNameAt(int nameIndex)

pack-priv int
pack-priv int

Returns:

an index value >= 0 if the name was found; -1, if the name was not found
getNameIndex
(HSSFName
the name whose index in the list of names of this workbook should be looked up.
name
)

As getNameIndex(String) is not necessarily unique (name + sheet index is unique), this method is more accurate.

public String

Returns:

named range name
getNameName
(int
the named range index (0 based)
index
)

gets the named range name

public NameRecord
getNameRecord(int nameIndex)

public List<HSSFName>
getNames(String
the name of the defined name
name
)

Implements org.apache.poi.ss.usermodel.Workbook.getNames.

Returns all defined names with the given name.

public int
getNumberOfFonts()

Implements org.apache.poi.ss.usermodel.Workbook.getNumberOfFonts.

Get the number of fonts in the font table

public int
getNumberOfFontsAsInt()

Implements org.apache.poi.ss.usermodel.Workbook.getNumberOfFontsAsInt.

Deprecated

Get the number of fonts in the font table

public int
public int

Returns:

number of sheets
getNumberOfSheets
()

Implements org.apache.poi.ss.usermodel.Workbook.getNumberOfSheets.

get the number of spreadsheets in the workbook (this will be three after serialization)

public int

Returns:

count of cell styles
getNumCellStyles
()

Implements org.apache.poi.ss.usermodel.Workbook.getNumCellStyles.

get the number of styles the workbook contains

pack-priv static Map<String, ClassID>
public String

Returns:

String Null if no print area has been defined
getPrintArea
(int
Zero-based sheet index (0 Represents the first sheet to keep consistent with java)
sheetIndex
)

Implements org.apache.poi.ss.usermodel.Workbook.getPrintArea.

Retrieves the reference for the printarea of the specified sheet, the sheet name is appended to the reference even if it was not specified.

public Collection<Integer>

Returns:

indices of selected sheets
getSelectedTabs
()

Gets the selected sheets (if more than one, Excel calls these a [Group]).

public HSSFSheet

Returns:

HSSFSheet with the name provided or null if it does not exist
getSheet
(String
of the sheet
name
)

Implements org.apache.poi.ss.usermodel.Workbook.getSheet.

Get sheet with the given name (case insensitive match)

public HSSFSheet

Returns:

HSSFSheet at the provided index
getSheetAt
(int
of the sheet number (0-based physical & logical)
index
)

Implements org.apache.poi.ss.usermodel.Workbook.getSheetAt.

Get the HSSFSheet object at the given index.

public int

Returns:

index of the sheet (0 based) or -1 if it was not found.
getSheetIndex
(String
the sheet name
name
)

Implements org.apache.poi.ss.usermodel.Workbook.getSheetIndex.

Returns the index of the sheet by his name

public int

Returns:

index of the sheet (0 based). -1 if not found
getSheetIndex
(Sheet
the sheet to look up
sheet
)

Implements org.apache.poi.ss.usermodel.Workbook.getSheetIndex.

Returns the index of the given sheet

public String

Returns:

Sheet name for the specified index
getSheetName
(int
sheet number (0 based)
sheetIndex
)

Implements org.apache.poi.ss.usermodel.Workbook.getSheetName.

Get the sheet name

private HSSFSheet[]
public SheetVisibility
getSheetVisibility(int
the index of the sheet
sheetIx
)

Implements org.apache.poi.ss.usermodel.Workbook.getSheetVisibility.

Get the visibility (visible, hidden, very hidden) of a sheet in this workbook

public SpreadsheetVersion

Returns:

EXCEL97 SpreadsheetVersion enum
getSpreadsheetVersion
()

Implements org.apache.poi.ss.usermodel.Workbook.getSpreadsheetVersion.

Returns the spreadsheet version (EXCEL97) of this workbook

pack-priv UDFFinder

Returns:

the locator of user-defined functions
getUDFFinder
()

Returns the locator of user-defined functions.

private String
public InternalWorkbook
public static String
pack-priv void
public void
insertChartRecord()

Test only.

public boolean
public boolean
isSheetHidden(int
Number
sheetIx
)

Implements org.apache.poi.ss.usermodel.Workbook.isSheetHidden.

Check whether a sheet is hidden.

public boolean
isSheetVeryHidden(int
sheet index to check
sheetIx
)

Implements org.apache.poi.ss.usermodel.Workbook.isSheetVeryHidden.

Check whether a sheet is very hidden.

public boolean
isWriteProtected()

Is the workbook protected with a password (not encrypted)?

public int
linkExternalWorkbook(String
The name the workbook will be referenced as in formulas
name
,
Workbook
The open workbook to fetch the link required information from
workbook
)

Implements org.apache.poi.ss.usermodel.Workbook.linkExternalWorkbook.

Adds the LinkTable records required to allow formulas referencing the specified external workbook to be added to this one.

pack-priv void
removeName(int index)

public void
removeName(Name
the name to remove.
name
)

Implements org.apache.poi.ss.usermodel.Workbook.removeName.

Remove a name.

public void
removePrintArea(int
Zero-based sheet index (0 = First Sheet)
sheetIndex
)

Implements org.apache.poi.ss.usermodel.Workbook.removePrintArea.

Delete the printarea for the sheet specified

public void
removeSheetAt(int
of the sheet (0-based)
index
)

Implements org.apache.poi.ss.usermodel.Workbook.removeSheetAt.

Removes sheet at the given index.

pack-priv void
resetFontCache()

Reset the fonts cache, causing all new calls to getFontAt() to create new objects.

private void
searchForPictures(List<EscherRecord>
the escher records.
escherRecords
,
List<HSSFPictureData>
the list to populate with the pictures.
pictures
)

Performs a recursive search for pictures in the given list of escher records.

public void
setActiveSheet(int
index of the active sheet (0-based)
index
)

Implements org.apache.poi.ss.usermodel.Workbook.setActiveSheet.

Convenience method to set the active sheet.

public void
setBackupFlag(boolean
true to indicate a backup will be performed.
backupValue
)

determine whether the Excel GUI will backup the workbook when saving.

public void
setCellReferenceType(CellReferenceType
the type of cell references used
cellReferenceType
)

Implements org.apache.poi.ss.usermodel.Workbook.setCellReferenceType.

Configure workbook to a specific cell reference type, e.g. R1C1 cell references (as opposed to A1 cell references).

public void
setEncryptionMode(EncryptionMode
the encryption mode
mode
)

Sets the encryption mode - if not set, defaults to EncryptionMode#cryptoAPI.

public void
setFirstVisibleTab(int
the sheet index of the tab that will become the first in the tab-bar
index
)

Implements org.apache.poi.ss.usermodel.Workbook.setFirstVisibleTab.

sets the first tab that is displayed in the list of tabs in excel.

public void
setForceFormulaRecalculation(boolean
true if the application will perform a full recalculation of workbook values when the workbook is opened
value
)

Implements org.apache.poi.ss.usermodel.Workbook.setForceFormulaRecalculation.

Whether the application shall perform a full recalculation when the workbook is opened.

public void
setHidden(boolean
pass false to make the workbook visible in the GUI
hiddenFlag
)

Implements org.apache.poi.ss.usermodel.Workbook.setHidden.

public static void
setMaxImageLength(int
the max image length allowed for HSSFWorkbook
length
)

public static void
setMaxRecordLength(int
the max record length allowed for HSSFWorkbook
length
)

public void
setMissingCellPolicy(Row.MissingCellPolicy missingCellPolicy)

Implements org.apache.poi.ss.usermodel.Workbook.setMissingCellPolicy.

Sets the policy on what to do when getting missing or blank cells from a row.

public void
setPrintArea(int
Zero-based sheet index (0 Represents the first sheet to keep consistent with java)
sheetIndex
,
String
Valid name Reference for the Print Area
reference
)

Implements org.apache.poi.ss.usermodel.Workbook.setPrintArea.

Sets the printarea for the sheet provided

i.e. Reference = $A$1:$B$2

public void
setPrintArea(int
Zero-based sheet index (0 = First Sheet)
sheetIndex
,
int
Column to begin printarea
startColumn
,
int
Column to end the printarea
endColumn
,
int
Row to begin the printarea
startRow
,
int
Row to end the printarea
endRow
)

Implements org.apache.poi.ss.usermodel.Workbook.setPrintArea.

For the Convenience of Java Programmers maintaining pointers.

private void
setPropertiesFromWorkbook(InternalWorkbook book)

used internally to set the workbook properties.

public void
setSelectedTab(int
the index of the sheet to select (0 based)
index
)

Implements org.apache.poi.ss.usermodel.Workbook.setSelectedTab.

Selects a single sheet.

public void
setSelectedTabs(int[]
Array of sheets to select, the index is 0-based.
indexes
)

Selects multiple sheets as a group.

public void
setSelectedTabs(Collection<Integer>
Collection of sheets to select, the index is 0-based.
indexes
)

Selects multiple sheets as a group.

public void
setSheetHidden(int
the sheet index (0-based)
sheetIx
,
boolean
True to mark the sheet as hidden, false otherwise
hidden
)

Implements org.apache.poi.ss.usermodel.Workbook.setSheetHidden.

Hide or unhide a sheet.

public void
setSheetName(int
number (0 based)
sheetIx
,
String name)

Implements org.apache.poi.ss.usermodel.Workbook.setSheetName.

Set the sheet name.

public void
setSheetOrder(String
the name of the sheet to reorder
sheetname
,
int
the position that we want to insert the sheet into (0 based)
pos
)

Implements org.apache.poi.ss.usermodel.Workbook.setSheetOrder.

sets the order of appearance for a given sheet.

public void
setSheetVisibility(int
the sheet index (0-based)
sheetIx
,
SheetVisibility
the sheet visibility to set
visibility
)

Implements org.apache.poi.ss.usermodel.Workbook.setSheetVisibility.

Hide or unhide a sheet.

public Iterator<Sheet>

Returns:

an iterator of the sheets.
sheetIterator
()

Implements org.apache.poi.ss.usermodel.Workbook.sheetIterator.

Returns an iterator of the sheets in the workbook in sheet order.

public Spliterator<Sheet>

Returns:

a spliterator of the sheets.
spliterator
()

Overrides default org.apache.poi.ss.usermodel.Workbook.spliterator.

Returns a spliterator of the sheets in the workbook in sheet order.

public void
unwriteProtectWorkbook()

removes the write protect flag

private void
updateActiveSheetAfterSheetReorder(int oldIndex, int newIndex)

private void
private void
updateNamedRangesAfterSheetReorder(int
the original index of the re-ordered sheet
oldIndex
,
int
the new index of the re-ordered sheet
newIndex
)

copy-pasted from XSSFWorkbook#updateNamedRangesAfterSheetReorder(int, int)

update sheet-scoped named ranges in this workbook after changing the sheet order of a sheet at oldIndex to newIndex.

private void
validateSheetIndex(int index)

public void
write()

Implements abstract org.apache.poi.POIDocument.write.

Write out this workbook to the currently open File via the writeable POIFSFileSystem it was opened as.

public void
write(File
The new File you wish to write the XLS to
newFile
)

Implements abstract org.apache.poi.POIDocument.write.

Method write - write out this workbook to a new File.

public void
write(OutputStream
- the java OutputStream you wish to write the XLS to
stream
)

Implements abstract org.apache.poi.POIDocument.write.

Implements org.apache.poi.ss.usermodel.Workbook.write.

Method write - write out this workbook to an OutputStream.

private void
write(POIFSFileSystem fs)

Writes the workbook out to a brand new, empty POIFS

public void
writeProtectWorkbook(String
to set
password
,
String username)

protect a workbook with a password (not encrypted, just sets writeprotect flags and the password.

Inherited from org.apache.poi.POIDocument:
clearDirectorycreateInformationPropertiesgetDirectorygetDocumentSummaryInformationgetEncryptedPropertyStreamNamegetPropertySetgetPropertySetgetSummaryInformationinitDirectoryreadPropertiesreadPropertiesreplaceDirectoryvalidateInPlaceWritePossiblewritePropertieswritePropertieswriteProperties