Class HSSFWorkbook
- java.lang.Object
-
- org.apache.poi.POIDocument
-
- org.apache.poi.hssf.usermodel.HSSFWorkbook
-
- All Implemented Interfaces:
Closeable
,AutoCloseable
,Iterable<Sheet>
,Workbook
public final class HSSFWorkbook extends POIDocument implements Workbook
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
-
-
Field Summary
Fields Modifier and Type Field Description protected List<HSSFSheet>
_sheets
this holds the HSSFSheet objects attached to this workbookstatic int
INITIAL_CAPACITY
used for compile-time performance/memory optimization.-
Fields inherited from interface org.apache.poi.ss.usermodel.Workbook
MAX_SENSITIVE_SHEET_NAME_LEN, PICTURE_TYPE_DIB, PICTURE_TYPE_EMF, PICTURE_TYPE_JPEG, PICTURE_TYPE_PICT, PICTURE_TYPE_PNG, PICTURE_TYPE_WMF
-
-
Constructor Summary
Constructors Constructor Description HSSFWorkbook()
Creates new HSSFWorkbook from scratch (start here!)HSSFWorkbook(InputStream s)
Companion to HSSFWorkbook(POIFSFileSystem), this constructs the POI filesystem around yourInputStream
, including all nodes.HSSFWorkbook(InputStream s, boolean preserveNodes)
Companion to HSSFWorkbook(POIFSFileSystem), this constructs the POI filesystem around yourInputStream
.HSSFWorkbook(DirectoryNode directory, boolean preserveNodes)
given a POI POIFSFileSystem object, and a specific directory within it, read in its Workbook and populate the high and low level models.HSSFWorkbook(DirectoryNode directory, POIFSFileSystem fs, boolean preserveNodes)
given a POI POIFSFileSystem object, and a specific directory within it, read in its Workbook and populate the high and low level models.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.HSSFWorkbook(POIFSFileSystem fs, boolean preserveNodes)
Given a POI POIFSFileSystem object, read in its Workbook and populate the high and low level models.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description int
addOlePackage(byte[] oleData, String label, String fileName, String command)
Adds an OLE package manager object with the given content to the sheetint
addOlePackage(POIFSFileSystem poiData, String label, String fileName, String command)
Adds an OLE package manager object with the given POIFS to the sheetint
addPicture(byte[] pictureData, int format)
Adds a picture to the workbook.void
addToolPack(UDFFinder toolpack)
Register a new toolpack in this workbook.boolean
changeExternalReference(String oldUrl, String newUrl)
Changes an external referenced file to another file.HSSFSheet
cloneSheet(int sheetIndex)
create an HSSFSheet from an existing sheet in the HSSFWorkbook.void
close()
Closes the underlyingPOIFSFileSystem
from which the Workbook was read, if any.static HSSFWorkbook
create(InternalWorkbook book)
HSSFCellStyle
createCellStyle()
Create a new Cell style and add it to the workbook's style table.HSSFDataFormat
createDataFormat()
Returns the instance of HSSFDataFormat for this workbook.HSSFEvaluationWorkbook
createEvaluationWorkbook()
HSSFFont
createFont()
create a new Font and add it to the workbook's font tableHSSFName
createName()
creates a new named range and add it to the modelHSSFSheet
createSheet()
create an HSSFSheet for this HSSFWorkbook, adds it to the sheets and returns the high level representation.HSSFSheet
createSheet(String sheetname)
Create a new sheet for this Workbook and return the high level representation.void
dumpDrawingGroupRecords(boolean fat)
Spits out a list of all the drawing records in the workbook.HSSFFont
findFont(boolean bold, short color, short fontHeight, String name, boolean italic, boolean strikeout, short typeOffset, byte underline)
Finds a font that matches the one with the supplied attributesint
getActiveSheetIndex()
gets the tab whose data is actually seen when the sheet is opened.List<HSSFObjectData>
getAllEmbeddedObjects()
Gets all embedded OLE2 objects from the Workbook.List<HSSFName>
getAllNames()
Returns all defined names.List<HSSFPictureData>
getAllPictures()
Gets all pictures from the Workbook.boolean
getBackupFlag()
determine whether the Excel GUI will backup the workbook when saving.byte[]
getBytes()
Method getBytes - get the bytes of just the HSSF portions of the XLS file.CellReferenceType
getCellReferenceType()
HSSFCellStyle
getCellStyleAt(int idx)
get the cell style object at the given indexHSSFCreationHelper
getCreationHelper()
Returns an object that handles instantiating concrete classes of the various instances one needs for HSSF and XSSF.HSSFPalette
getCustomPalette()
EncryptionInfo
getEncryptionInfo()
EncryptionMode
getEncryptionMode()
int
getFirstVisibleTab()
sets the first tab that is displayed in the list of tabs in excel.HSSFFont
getFontAt(int idx)
Get the font at the given index numberboolean
getForceFormulaRecalculation()
Whether Excel will be asked to recalculate all formulas when the workbook is opened.InternalWorkbook
getInternalWorkbook()
static int
getMaxImageLength()
static int
getMaxRecordLength()
Row.MissingCellPolicy
getMissingCellPolicy()
Retrieves the current policy on what to do when getting missing or blank cells from a row.HSSFName
getName(String name)
String
getNameName(int index)
gets the named range nameNameRecord
getNameRecord(int nameIndex)
List<HSSFName>
getNames(String name)
Returns all defined names with the given name.int
getNumberOfFonts()
Get the number of fonts in the font tableint
getNumberOfFontsAsInt()
Deprecated.int
getNumberOfNames()
int
getNumberOfSheets()
get the number of spreadsheets in the workbook (this will be three after serialization)int
getNumCellStyles()
get the number of styles the workbook containsString
getPrintArea(int sheetIndex)
Retrieves the reference for the printarea of the specified sheet, the sheet name is appended to the reference even if it was not specified.Collection<Integer>
getSelectedTabs()
Gets the selected sheets (if more than one, Excel calls these a [Group]).HSSFSheet
getSheet(String name)
Get sheet with the given name (case insensitive match)HSSFSheet
getSheetAt(int index)
Get the HSSFSheet object at the given index.int
getSheetIndex(String name)
Returns the index of the sheet by his nameint
getSheetIndex(Sheet sheet)
Returns the index of the given sheetString
getSheetName(int sheetIndex)
Get the sheet nameSheetVisibility
getSheetVisibility(int sheetIx)
Get the visibility (visible, hidden, very hidden) of a sheet in this workbookSpreadsheetVersion
getSpreadsheetVersion()
Returns the spreadsheet version (EXCEL97) of this workbookInternalWorkbook
getWorkbook()
static String
getWorkbookDirEntryName(DirectoryNode directory)
void
insertChartRecord()
Test only.boolean
isHidden()
boolean
isSheetHidden(int sheetIx)
Check whether a sheet is hidden.boolean
isSheetVeryHidden(int sheetIx)
Check whether a sheet is very hidden.boolean
isWriteProtected()
Is the workbook protected with a password (not encrypted)?int
linkExternalWorkbook(String name, Workbook workbook)
Adds the LinkTable records required to allow formulas referencing the specified external workbook to be added to this one.void
removeName(Name name)
Remove a name.void
removePrintArea(int sheetIndex)
Delete the printarea for the sheet specifiedvoid
removeSheetAt(int index)
Removes sheet at the given index.void
setActiveSheet(int index)
Convenience method to set the active sheet.void
setBackupFlag(boolean backupValue)
determine whether the Excel GUI will backup the workbook when saving.void
setCellReferenceType(CellReferenceType cellReferenceType)
Configure workbook to a specific cell reference type, e.g.void
setEncryptionMode(EncryptionMode mode)
Sets the encryption mode - if not set, defaults toEncryptionMode.cryptoAPI
.void
setFirstVisibleTab(int index)
sets the first tab that is displayed in the list of tabs in excel.void
setForceFormulaRecalculation(boolean value)
Whether the application shall perform a full recalculation when the workbook is opened.void
setHidden(boolean hiddenFlag)
static void
setMaxImageLength(int length)
static void
setMaxRecordLength(int length)
void
setMissingCellPolicy(Row.MissingCellPolicy missingCellPolicy)
Sets the policy on what to do when getting missing or blank cells from a row.void
setPrintArea(int sheetIndex, int startColumn, int endColumn, int startRow, int endRow)
For the Convenience of Java Programmers maintaining pointers.void
setPrintArea(int sheetIndex, String reference)
Sets the printarea for the sheet providedvoid
setSelectedTab(int index)
Selects a single sheet.void
setSelectedTabs(int[] indexes)
Selects multiple sheets as a group.void
setSelectedTabs(Collection<Integer> indexes)
Selects multiple sheets as a group.void
setSheetHidden(int sheetIx, boolean hidden)
Hide or unhide a sheet.void
setSheetName(int sheetIx, String name)
Set the sheet name.void
setSheetOrder(String sheetname, int pos)
sets the order of appearance for a given sheet.void
setSheetVisibility(int sheetIx, SheetVisibility visibility)
Hide or unhide a sheet.Iterator<Sheet>
sheetIterator()
Returns an iterator of the sheets in the workbook in sheet order.Spliterator<Sheet>
spliterator()
Returns a spliterator of the sheets in the workbook in sheet order.void
unwriteProtectWorkbook()
removes the write protect flagvoid
write()
Write out this workbook to the currently openFile
via the writeablePOIFSFileSystem
it was opened as.void
write(File newFile)
Method write - write out this workbook to a newFile
.void
write(OutputStream stream)
Method write - write out this workbook to anOutputStream
.void
writeProtectWorkbook(String password, String username)
protect a workbook with a password (not encrypted, just sets writeprotect flags and the password.-
Methods inherited from class org.apache.poi.POIDocument
clearDirectory, createInformationProperties, getDirectory, getDocumentSummaryInformation, getEncryptedPropertyStreamName, getPropertySet, getPropertySet, getSummaryInformation, initDirectory, readProperties, readProperties, replaceDirectory, validateInPlaceWritePossible, writeProperties, writeProperties, writeProperties
-
-
-
-
Field Detail
-
INITIAL_CAPACITY
public static final int INITIAL_CAPACITY
used for compile-time performance/memory optimization. This determines the initial capacity for the sheet collection. Its currently set to 3. Changing it in this release will decrease performance since you're never allowed to have more or less than three sheets!
-
-
Constructor Detail
-
HSSFWorkbook
public HSSFWorkbook()
Creates new HSSFWorkbook from scratch (start here!)
-
HSSFWorkbook
public HSSFWorkbook(POIFSFileSystem fs) throws IOException
Given a POI POIFSFileSystem object, read in its Workbook along with all related nodes, and populate the high and low level models.This calls
HSSFWorkbook(POIFSFileSystem, boolean)
with preserve nodes set to true.- Throws:
IOException
- if the stream cannot be readIllegalStateException
- a number of runtime exceptions can be thrown, especially if there are problems with the input format- See Also:
HSSFWorkbook(POIFSFileSystem, boolean)
,POIFSFileSystem
-
HSSFWorkbook
public HSSFWorkbook(POIFSFileSystem fs, boolean preserveNodes) throws IOException
Given a POI POIFSFileSystem object, read in its Workbook and populate the high and low level models. If you're reading in a workbook... start here!- Parameters:
fs
- the POI filesystem that contains the Workbook stream.preserveNodes
- 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- Throws:
IOException
- if the stream cannot be readIllegalStateException
- a number of runtime exceptions can be thrown, especially if there are problems with the input format- See Also:
POIFSFileSystem
-
HSSFWorkbook
public HSSFWorkbook(DirectoryNode directory, POIFSFileSystem fs, boolean preserveNodes) throws IOException
given a POI POIFSFileSystem object, and a specific directory within it, read in its Workbook and populate the high and low level models. If you're reading in a workbook...start here.- Parameters:
directory
- the POI filesystem directory to process fromfs
- the POI filesystem that contains the Workbook stream.preserveNodes
- 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- Throws:
IOException
- if the stream cannot be readIllegalStateException
- a number of runtime exceptions can be thrown, especially if there are problems with the input format- See Also:
POIFSFileSystem
-
HSSFWorkbook
public HSSFWorkbook(DirectoryNode directory, boolean preserveNodes) throws IOException
given a POI POIFSFileSystem object, and a specific directory within it, read in its Workbook and populate the high and low level models. If you're reading in a workbook...start here.- Parameters:
directory
- the POI filesystem directory to process frompreserveNodes
- 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- Throws:
IOException
- if the stream cannot be readIllegalStateException
- a number of runtime exceptions can be thrown, especially if there are problems with the input format- See Also:
POIFSFileSystem
-
HSSFWorkbook
public HSSFWorkbook(InputStream s) throws IOException
Companion to HSSFWorkbook(POIFSFileSystem), this constructs the POI filesystem around yourInputStream
, including all nodes.This calls
HSSFWorkbook(InputStream, boolean)
with preserve nodes set to true.- Throws:
IOException
- if the stream cannot be readIllegalStateException
- a number of runtime exceptions can be thrown, especially if there are problems with the input format- See Also:
HSSFWorkbook(InputStream, boolean)
,HSSFWorkbook(POIFSFileSystem)
,POIFSFileSystem
-
HSSFWorkbook
public HSSFWorkbook(InputStream s, boolean preserveNodes) throws IOException
Companion to HSSFWorkbook(POIFSFileSystem), this constructs the POI filesystem around yourInputStream
.- Parameters:
s
- the POI filesystem that contains the Workbook stream.preserveNodes
- whether to preserve other nodes, such as macros. This takes more memory, so only say yes if you need to.- Throws:
IOException
- if the stream cannot be readIllegalStateException
- a number of runtime exceptions can be thrown, especially if there are problems with the input format- See Also:
POIFSFileSystem
,HSSFWorkbook(POIFSFileSystem)
-
-
Method Detail
-
create
public static HSSFWorkbook create(InternalWorkbook book)
-
setMaxRecordLength
public static void setMaxRecordLength(int length)
- Parameters:
length
- the max record length allowed for HSSFWorkbook
-
getMaxRecordLength
public static int getMaxRecordLength()
- Returns:
- the max record length allowed for HSSFWorkbook
-
setMaxImageLength
public static void setMaxImageLength(int length)
- Parameters:
length
- the max image length allowed for HSSFWorkbook
-
getMaxImageLength
public static int getMaxImageLength()
- Returns:
- the max image length allowed for HSSFWorkbook
-
getWorkbookDirEntryName
public static String getWorkbookDirEntryName(DirectoryNode directory)
-
getMissingCellPolicy
public Row.MissingCellPolicy getMissingCellPolicy()
Retrieves the current policy on what to do when getting missing or blank cells from a row. The default is to return blank and null cells.Row.MissingCellPolicy
- Specified by:
getMissingCellPolicy
in interfaceWorkbook
-
setMissingCellPolicy
public void setMissingCellPolicy(Row.MissingCellPolicy missingCellPolicy)
Sets the policy on what to do when getting missing or blank cells from a row. This will then apply to all calls toHSSFRow.getCell(int)
}. SeeRow.MissingCellPolicy
. Note that this has no effect on any iterators, only on when fetching Cells by their column index.- Specified by:
setMissingCellPolicy
in interfaceWorkbook
-
setSheetOrder
public void setSheetOrder(String sheetname, int pos)
sets the order of appearance for a given sheet.- Specified by:
setSheetOrder
in interfaceWorkbook
- Parameters:
sheetname
- the name of the sheet to reorderpos
- the position that we want to insert the sheet into (0 based)
-
setSelectedTab
public void setSelectedTab(int index)
Selects a single sheet. This may be different to the 'active' sheet (which is the sheet with focus).- Specified by:
setSelectedTab
in interfaceWorkbook
- Parameters:
index
- the index of the sheet to select (0 based)- See Also:
Sheet.setSelected(boolean)
-
setSelectedTabs
public void setSelectedTabs(int[] indexes)
Selects multiple sheets as a group. This is distinct from the 'active' sheet (which is the sheet with focus). Unselects sheets that are not inindexes
.- Parameters:
indexes
- Array of sheets to select, the index is 0-based.
-
setSelectedTabs
public void setSelectedTabs(Collection<Integer> indexes)
Selects multiple sheets as a group. This is distinct from the 'active' sheet (which is the sheet with focus). Unselects sheets that are not inindexes
.- Parameters:
indexes
- Collection of sheets to select, the index is 0-based.
-
getSelectedTabs
public Collection<Integer> getSelectedTabs()
Gets the selected sheets (if more than one, Excel calls these a [Group]).- Returns:
- indices of selected sheets
-
setActiveSheet
public void setActiveSheet(int index)
Convenience method to set the active sheet. The active sheet is is the sheet which is currently displayed when the workbook is viewed in Excel. 'Selected' sheet(s) is a distinct concept.- Specified by:
setActiveSheet
in interfaceWorkbook
- Parameters:
index
- index of the active sheet (0-based)
-
getActiveSheetIndex
public int getActiveSheetIndex()
gets the tab whose data is actually seen when the sheet is opened. This may be different from the "selected sheet" since excel seems to allow you to show the data of one sheet when another is seen "selected" in the tabs (at the bottom).- Specified by:
getActiveSheetIndex
in interfaceWorkbook
- Returns:
- the index of the active sheet (0-based)
- See Also:
HSSFSheet.setSelected(boolean)
-
setFirstVisibleTab
public void setFirstVisibleTab(int index)
sets the first tab that is displayed in the list of tabs in excel. This method does not hide, select or focus sheets. It just sets the scroll position in the tab-bar.- Specified by:
setFirstVisibleTab
in interfaceWorkbook
- Parameters:
index
- the sheet index of the tab that will become the first in the tab-bar
-
getFirstVisibleTab
public int getFirstVisibleTab()
sets the first tab that is displayed in the list of tabs in excel.- Specified by:
getFirstVisibleTab
in interfaceWorkbook
- Returns:
- the first tab that to display in the list of tabs (0-based).
-
setSheetName
public void setSheetName(int sheetIx, String name)
Set the sheet name.- Specified by:
setSheetName
in interfaceWorkbook
- Parameters:
sheetIx
- number (0 based)- Throws:
IllegalArgumentException
- if the name is null or invalid or workbook already contains a sheet with this name- See Also:
createSheet(String)
,WorkbookUtil.createSafeSheetName(String nameProposal)
-
getSheetName
public String getSheetName(int sheetIndex)
Description copied from interface:Workbook
Get the sheet name- Specified by:
getSheetName
in interfaceWorkbook
- Parameters:
sheetIndex
- sheet number (0 based)- Returns:
- Sheet name for the specified index
-
isHidden
public boolean isHidden()
-
setHidden
public void setHidden(boolean hiddenFlag)
-
isSheetHidden
public boolean isSheetHidden(int sheetIx)
Description copied from interface:Workbook
Check whether a sheet is hidden.Note that a sheet could instead be set to be very hidden, which is different (
Workbook.isSheetVeryHidden(int)
)- Specified by:
isSheetHidden
in interfaceWorkbook
- Parameters:
sheetIx
- Number- Returns:
true
if sheet is hidden- See Also:
Workbook.getSheetVisibility(int)
-
isSheetVeryHidden
public boolean isSheetVeryHidden(int sheetIx)
Description copied from interface:Workbook
Check whether a sheet is very hidden.This is different from the normal hidden status (
Workbook.isSheetHidden(int)
)- Specified by:
isSheetVeryHidden
in interfaceWorkbook
- Parameters:
sheetIx
- sheet index to check- Returns:
true
if sheet is very hidden- See Also:
Workbook.getSheetVisibility(int)
-
getSheetVisibility
public SheetVisibility getSheetVisibility(int sheetIx)
Description copied from interface:Workbook
Get the visibility (visible, hidden, very hidden) of a sheet in this workbook- Specified by:
getSheetVisibility
in interfaceWorkbook
- Parameters:
sheetIx
- the index of the sheet- Returns:
- the sheet visibility
-
setSheetHidden
public void setSheetHidden(int sheetIx, boolean hidden)
Description copied from interface:Workbook
Hide or unhide a sheet. Please note that the sheet currently set as active sheet (sheet 0 in a newly created workbook or the one set via setActiveSheet()) cannot be hidden.- Specified by:
setSheetHidden
in interfaceWorkbook
- Parameters:
sheetIx
- the sheet index (0-based)hidden
- True to mark the sheet as hidden, false otherwise- See Also:
Workbook.setSheetVisibility(int, SheetVisibility)
-
setSheetVisibility
public void setSheetVisibility(int sheetIx, SheetVisibility visibility)
Description copied from interface:Workbook
Hide or unhide a sheet. Please note that the sheet currently set as active sheet (sheet 0 in a newly created workbook or the one set via setActiveSheet()) cannot be hidden.- Specified by:
setSheetVisibility
in interfaceWorkbook
- Parameters:
sheetIx
- the sheet index (0-based)visibility
- the sheet visibility to set
-
getSheetIndex
public int getSheetIndex(String name)
Returns the index of the sheet by his name- Specified by:
getSheetIndex
in interfaceWorkbook
- Parameters:
name
- the sheet name- Returns:
- index of the sheet (0 based) or -1 if it was not found.
-
getSheetIndex
public int getSheetIndex(Sheet sheet)
Returns the index of the given sheet- Specified by:
getSheetIndex
in interfaceWorkbook
- Parameters:
sheet
- the sheet to look up- Returns:
- index of the sheet (0 based).
-1
if not found
-
createSheet
public HSSFSheet createSheet()
create an HSSFSheet for this HSSFWorkbook, adds it to the sheets and returns the high level representation. Use this to create new sheets.- Specified by:
createSheet
in interfaceWorkbook
- Returns:
- HSSFSheet representing the new sheet.
-
cloneSheet
public HSSFSheet cloneSheet(int sheetIndex)
create an HSSFSheet from an existing sheet in the HSSFWorkbook.- Specified by:
cloneSheet
in interfaceWorkbook
- Returns:
- HSSFSheet representing the cloned sheet.
-
createSheet
public HSSFSheet createSheet(String sheetname)
Create a new sheet for this Workbook and return the high level representation. Use this to create new sheets.Note that Excel allows sheet names up to 31 chars in length but other applications (such as OpenOffice) allow more. Some versions of Excel crash with names longer than 31 chars, others - truncate such names to 31 character.
POI's SpreadsheetAPI silently truncates the input argument to 31 characters. Example:
Sheet sheet = workbook.createSheet("My very long sheet name which is longer than 31 chars"); // will be truncated assert 31 == sheet.getSheetName().length(); assert "My very long sheet name which i" == sheet.getSheetName();
Except the 31-character constraint, Excel applies some other rules:
Sheet name MUST be unique in the workbook and MUST NOT contain the any of the following characters:
- 0x0000
- 0x0003
- colon (:)
- backslash (\)
- asterisk (*)
- question mark (?)
- forward slash (/)
- opening square bracket ([)
- closing square bracket (])
- Specified by:
createSheet
in interfaceWorkbook
- Parameters:
sheetname
- sheetname to set for the sheet.- Returns:
- Sheet representing the new sheet.
- Throws:
IllegalArgumentException
- if the name is null or invalid or workbook already contains a sheet with this name- See Also:
WorkbookUtil.createSafeSheetName(String nameProposal)
-
sheetIterator
public Iterator<Sheet> sheetIterator()
Returns an iterator of the sheets in the workbook in sheet order. Includes hidden and very hidden sheets.- Specified by:
sheetIterator
in interfaceWorkbook
- Returns:
- an iterator of the sheets.
-
spliterator
public Spliterator<Sheet> spliterator()
Returns a spliterator of the sheets in the workbook in sheet order. Includes hidden and very hidden sheets.- Specified by:
spliterator
in interfaceIterable<Sheet>
- Specified by:
spliterator
in interfaceWorkbook
- Returns:
- a spliterator of the sheets.
- Since:
- POI 5.2.0
-
getNumberOfSheets
public int getNumberOfSheets()
get the number of spreadsheets in the workbook (this will be three after serialization)- Specified by:
getNumberOfSheets
in interfaceWorkbook
- Returns:
- number of sheets
-
getSheetAt
public HSSFSheet getSheetAt(int index)
Get the HSSFSheet object at the given index.- Specified by:
getSheetAt
in interfaceWorkbook
- Parameters:
index
- of the sheet number (0-based physical & logical)- Returns:
- HSSFSheet at the provided index
- Throws:
IllegalArgumentException
- if the index is out of range (index < 0 || index >= getNumberOfSheets()).
-
getSheet
public HSSFSheet getSheet(String name)
Get sheet with the given name (case insensitive match)
-
removeSheetAt
public void removeSheetAt(int index)
Removes sheet at the given index.Care must be taken if the removed sheet is the currently active or only selected sheet in the workbook. There are a few situations when Excel must have a selection and/or active sheet. (For example when printing - see Bug 40414).
This method makes sure that if the removed sheet was active, another sheet will become active in its place. Furthermore, if the removed sheet was the only selected sheet, another sheet will become selected. The newly active/selected sheet will have the same index, or one less if the removed sheet was the last in the workbook.
- Specified by:
removeSheetAt
in interfaceWorkbook
- Parameters:
index
- of the sheet (0-based)
-
setBackupFlag
public void setBackupFlag(boolean backupValue)
determine whether the Excel GUI will backup the workbook when saving.- Parameters:
backupValue
- true to indicate a backup will be performed.
-
getBackupFlag
public boolean getBackupFlag()
determine whether the Excel GUI will backup the workbook when saving.- Returns:
- the current setting for backups.
-
createFont
public HSSFFont createFont()
create a new Font and add it to the workbook's font table- Specified by:
createFont
in interfaceWorkbook
- Returns:
- new font object
-
findFont
public HSSFFont findFont(boolean bold, short color, short fontHeight, String name, boolean italic, boolean strikeout, short typeOffset, byte underline)
Finds a font that matches the one with the supplied attributes
-
getNumberOfFonts
public int getNumberOfFonts()
Description copied from interface:Workbook
Get the number of fonts in the font table- Specified by:
getNumberOfFonts
in interfaceWorkbook
- Returns:
- number of fonts (as int since POI 5.0.0)
-
getNumberOfFontsAsInt
@Deprecated @Removal(version="6.0.0") public int getNumberOfFontsAsInt()
Deprecated.Description copied from interface:Workbook
Get the number of fonts in the font table- Specified by:
getNumberOfFontsAsInt
in interfaceWorkbook
- Returns:
- number of fonts
-
getFontAt
public HSSFFont getFontAt(int idx)
Description copied from interface:Workbook
Get the font at the given index number
-
createCellStyle
public HSSFCellStyle createCellStyle()
Create a new Cell style and add it to the workbook's style table. You can define up to 4000 unique styles in a .xls workbook.- Specified by:
createCellStyle
in interfaceWorkbook
- Returns:
- the new Cell Style object
- Throws:
IllegalStateException
- if the number of cell styles exceeded the limit for this type of Workbook.
-
getNumCellStyles
public int getNumCellStyles()
get the number of styles the workbook contains- Specified by:
getNumCellStyles
in interfaceWorkbook
- Returns:
- count of cell styles
-
getCellStyleAt
public HSSFCellStyle getCellStyleAt(int idx)
get the cell style object at the given index- Specified by:
getCellStyleAt
in interfaceWorkbook
- Parameters:
idx
- index within the set of styles- Returns:
- HSSFCellStyle object at the index
-
close
public void close() throws IOException
Closes the underlyingPOIFSFileSystem
from which the Workbook was read, if any.Once this has been called, no further operations, updates or reads should be performed on the Workbook.
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Specified by:
close
in interfaceWorkbook
- Overrides:
close
in classPOIDocument
- Throws:
IOException
-
write
public void write() throws IOException
Write out this workbook to the currently openFile
via the writeablePOIFSFileSystem
it was opened as.This will fail (with an
IllegalStateException
if the Workbook was opened read-only, opened from anInputStream
instead of a File, or if this is not the root document. For those cases, you must usewrite(OutputStream)
orwrite(File)
to write to a brand new document.- Specified by:
write
in classPOIDocument
- Throws:
IOException
- thrown on errors writing to the file
-
write
public void write(File newFile) throws IOException
Method write - write out this workbook to a newFile
. Constructs a new POI POIFSFileSystem, passes in the workbook binary representation and writes it out. If the file exists, it will be replaced, otherwise a new one will be created.Note that you cannot write to the currently open File using this method. If you opened your Workbook from a File, you must use the
write()
method instead!- Specified by:
write
in classPOIDocument
- Parameters:
newFile
- The new File you wish to write the XLS to- Throws:
IOException
- if anything can't be written.- See Also:
POIFSFileSystem
-
write
public void write(OutputStream stream) throws IOException
Method write - write out this workbook to anOutputStream
. Constructs a new POI POIFSFileSystem, passes in the workbook binary representation and writes it out.If
stream
is aFileOutputStream
on a networked drive or has a high cost/latency associated with each written byte, consider wrapping the OutputStream in aBufferedOutputStream
to improve write performance.- Specified by:
write
in interfaceWorkbook
- Specified by:
write
in classPOIDocument
- Parameters:
stream
- - the java OutputStream you wish to write the XLS to- Throws:
IOException
- if anything can't be written.- See Also:
POIFSFileSystem
-
getBytes
public byte[] getBytes()
Method getBytes - get the bytes of just the HSSF portions of the XLS file. Use this to construct a POI POIFSFileSystem yourself.- Returns:
- byte[] array containing the binary representation of this workbook and all contained sheets, rows, cells, etc.
-
getWorkbook
@Internal public InternalWorkbook getWorkbook()
-
getNumberOfNames
public int getNumberOfNames()
- Specified by:
getNumberOfNames
in interfaceWorkbook
- Returns:
- the total number of defined names in this workbook
-
getNames
public List<HSSFName> getNames(String name)
Description copied from interface:Workbook
Returns all defined names with the given name.
-
getAllNames
public List<HSSFName> getAllNames()
Description copied from interface:Workbook
Returns all defined names.- Specified by:
getAllNames
in interfaceWorkbook
- Returns:
- a list of the defined names. An empty list is returned if none is found.
-
getNameRecord
public NameRecord getNameRecord(int nameIndex)
-
getNameName
public String getNameName(int index)
gets the named range name- Parameters:
index
- the named range index (0 based)- Returns:
- named range name
-
setPrintArea
public void setPrintArea(int sheetIndex, String reference)
Sets the printarea for the sheet providedi.e. Reference = $A$1:$B$2
- Specified by:
setPrintArea
in interfaceWorkbook
- Parameters:
sheetIndex
- Zero-based sheet index (0 Represents the first sheet to keep consistent with java)reference
- Valid name Reference for the Print Area
-
setPrintArea
public void setPrintArea(int sheetIndex, int startColumn, int endColumn, int startRow, int endRow)
For the Convenience of Java Programmers maintaining pointers.- Specified by:
setPrintArea
in interfaceWorkbook
- Parameters:
sheetIndex
- Zero-based sheet index (0 = First Sheet)startColumn
- Column to begin printareaendColumn
- Column to end the printareastartRow
- Row to begin the printareaendRow
- Row to end the printarea- See Also:
setPrintArea(int, String)
-
getPrintArea
public String getPrintArea(int sheetIndex)
Retrieves the reference for the printarea of the specified sheet, the sheet name is appended to the reference even if it was not specified.- Specified by:
getPrintArea
in interfaceWorkbook
- Parameters:
sheetIndex
- Zero-based sheet index (0 Represents the first sheet to keep consistent with java)- Returns:
- String Null if no print area has been defined
-
removePrintArea
public void removePrintArea(int sheetIndex)
Delete the printarea for the sheet specified- Specified by:
removePrintArea
in interfaceWorkbook
- Parameters:
sheetIndex
- Zero-based sheet index (0 = First Sheet)
-
createName
public HSSFName createName()
creates a new named range and add it to the model- Specified by:
createName
in interfaceWorkbook
- Returns:
- named range high level
-
getCellReferenceType
public CellReferenceType getCellReferenceType()
- Specified by:
getCellReferenceType
in interfaceWorkbook
- Returns:
- the type of cell references used
-
setCellReferenceType
public void setCellReferenceType(CellReferenceType cellReferenceType)
Configure workbook to a specific cell reference type, e.g. R1C1 cell references (as opposed to A1 cell references).Note that HSSF format stores this information at sheet level - so if the workbook has no sheets, this call will have no effect. It is recommended that you call this (possibly again) just before writing HSSFWorkbook.
- Specified by:
setCellReferenceType
in interfaceWorkbook
- Parameters:
cellReferenceType
- the type of cell references used- Since:
- POI 5.2.1
-
createDataFormat
public HSSFDataFormat createDataFormat()
Returns the instance of HSSFDataFormat for this workbook.- Specified by:
createDataFormat
in interfaceWorkbook
- Returns:
- the HSSFDataFormat object
- See Also:
FormatRecord
,Record
-
removeName
public void removeName(Name name)
Remove a name.- Specified by:
removeName
in interfaceWorkbook
- Parameters:
name
- the name to remove.
-
getCustomPalette
public HSSFPalette getCustomPalette()
-
insertChartRecord
public void insertChartRecord()
Test only. Do not use
-
dumpDrawingGroupRecords
public void dumpDrawingGroupRecords(boolean fat)
Spits out a list of all the drawing records in the workbook.
-
addPicture
public int addPicture(byte[] pictureData, int format)
Adds a picture to the workbook.- Specified by:
addPicture
in interfaceWorkbook
- Parameters:
pictureData
- The bytes of the pictureformat
- The format of the picture. One ofPICTURE_TYPE_*
- Returns:
- the index to this picture (1 based).
- See Also:
Workbook.PICTURE_TYPE_WMF
,Workbook.PICTURE_TYPE_EMF
,Workbook.PICTURE_TYPE_PICT
,Workbook.PICTURE_TYPE_PNG
,Workbook.PICTURE_TYPE_JPEG
,Workbook.PICTURE_TYPE_DIB
-
getAllPictures
public List<HSSFPictureData> getAllPictures()
Gets all pictures from the Workbook.- Specified by:
getAllPictures
in interfaceWorkbook
- Returns:
- the list of pictures (a list of
HSSFPictureData
objects.)
-
addOlePackage
public int addOlePackage(POIFSFileSystem poiData, String label, String fileName, String command) throws IOException
Adds an OLE package manager object with the given POIFS to the sheet- Parameters:
poiData
- an POIFS containing the embedded document, to be addedlabel
- the label of the payloadfileName
- the original filenamecommand
- the command to open the payload- Returns:
- the index of the added ole object
- Throws:
IOException
- if the object can't be embedded
-
addOlePackage
public int addOlePackage(byte[] oleData, String label, String fileName, String command) throws IOException
Description copied from interface:Workbook
Adds an OLE package manager object with the given content to the sheet- Specified by:
addOlePackage
in interfaceWorkbook
- Parameters:
oleData
- the payloadlabel
- the label of the payloadfileName
- the original filenamecommand
- the command to open the payload- Returns:
- the index of the added ole object, i.e. the storage id
- Throws:
IOException
- if the object can't be embedded
-
linkExternalWorkbook
public int linkExternalWorkbook(String name, Workbook workbook)
Adds the LinkTable records required to allow formulas referencing the specified external workbook to be added to this one. Allows formulas such as "[MyOtherWorkbook]Sheet3!$A$5" to be added to the file, for workbooks not already referenced.- Specified by:
linkExternalWorkbook
in interfaceWorkbook
- Parameters:
name
- The name the workbook will be referenced as in formulasworkbook
- The open workbook to fetch the link required information from
-
isWriteProtected
public boolean isWriteProtected()
Is the workbook protected with a password (not encrypted)?
-
writeProtectWorkbook
public void writeProtectWorkbook(String password, String username)
protect a workbook with a password (not encrypted, just sets writeprotect flags and the password.- Parameters:
password
- to set
-
unwriteProtectWorkbook
public void unwriteProtectWorkbook()
removes the write protect flag
-
getAllEmbeddedObjects
public List<HSSFObjectData> getAllEmbeddedObjects()
Gets all embedded OLE2 objects from the Workbook.- Returns:
- the list of embedded objects (a list of
HSSFObjectData
objects.)
-
getCreationHelper
public HSSFCreationHelper getCreationHelper()
Description copied from interface:Workbook
Returns an object that handles instantiating concrete classes of the various instances one needs for HSSF and XSSF.- Specified by:
getCreationHelper
in interfaceWorkbook
-
addToolPack
public void addToolPack(UDFFinder toolpack)
Register a new toolpack in this workbook.- Specified by:
addToolPack
in interfaceWorkbook
- Parameters:
toolpack
- the toolpack to register
-
setForceFormulaRecalculation
public void setForceFormulaRecalculation(boolean value)
Whether the application shall perform a full recalculation when the workbook is opened.Typically you want to force formula recalculation when you modify cell formulas or values of a workbook previously created by Excel. When set to true, this flag will tell Excel that it needs to recalculate all formulas in the workbook the next time the file is opened.
Note, that recalculation updates cached formula results and, thus, modifies the workbook. Depending on the version, Excel may prompt you with "Do you want to save the changes in filename?" on close.
- Specified by:
setForceFormulaRecalculation
in interfaceWorkbook
- Parameters:
value
- true if the application will perform a full recalculation of workbook values when the workbook is opened- Since:
- 3.8
-
getForceFormulaRecalculation
public boolean getForceFormulaRecalculation()
Whether Excel will be asked to recalculate all formulas when the workbook is opened.- Specified by:
getForceFormulaRecalculation
in interfaceWorkbook
- Since:
- 3.8
-
changeExternalReference
public boolean changeExternalReference(String oldUrl, String newUrl)
Changes an external referenced file to another file. A formula in Excel which references a cell in another file is saved in two parts: The referenced file is stored in an reference table. the row/cell information is saved separate. This method invocation will only change the reference in the lookup-table itself.- Parameters:
oldUrl
- The old URL to search for and which is to be replacednewUrl
- The URL replacement- Returns:
- true if the oldUrl was found and replaced with newUrl. Otherwise false
-
getInternalWorkbook
@Internal public InternalWorkbook getInternalWorkbook()
-
getSpreadsheetVersion
public SpreadsheetVersion getSpreadsheetVersion()
Returns the spreadsheet version (EXCEL97) of this workbook- Specified by:
getSpreadsheetVersion
in interfaceWorkbook
- Returns:
- EXCEL97 SpreadsheetVersion enum
- Since:
- 3.14 beta 2
-
getEncryptionInfo
public EncryptionInfo getEncryptionInfo()
- Overrides:
getEncryptionInfo
in classPOIDocument
- Returns:
- the encryption info if the document is encrypted, otherwise
null
-
createEvaluationWorkbook
public HSSFEvaluationWorkbook createEvaluationWorkbook()
- Specified by:
createEvaluationWorkbook
in interfaceWorkbook
- Returns:
- an evaluation workbook
-
setEncryptionMode
public void setEncryptionMode(EncryptionMode mode)
Sets the encryption mode - if not set, defaults toEncryptionMode.cryptoAPI
. Allowed modes arenull
= remove encryption - same asBiff8EncryptionKey.setCurrentUserPassword(null)
,EncryptionMode.cryptoAPI
,EncryptionMode.binaryRC4
,EncryptionMode.xor
. UseEncryptionMode.binaryRC4
for Libre Office, but better use OOXML format/encryption for real security.- Parameters:
mode
- the encryption mode
-
getEncryptionMode
public EncryptionMode getEncryptionMode()
- Returns:
- the encryption mode or
null
if unset
-
-