Interface Workbook
-
- All Superinterfaces:
AutoCloseable,Closeable,Iterable<Sheet>
- All Known Implementing Classes:
HSSFWorkbook
public interface Workbook extends Closeable, Iterable<Sheet>
High level representation of a Excel 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.
-
-
Field Summary
Fields Modifier and Type Field Description static intMAX_SENSITIVE_SHEET_NAME_LENExcel silently truncates long sheet names to 31 chars.static intPICTURE_TYPE_DIBDevice independent bitmapstatic intPICTURE_TYPE_EMFExtended windows meta filestatic intPICTURE_TYPE_JPEGJPEG formatstatic intPICTURE_TYPE_PICTMac PICT formatstatic intPICTURE_TYPE_PNGPNG formatstatic intPICTURE_TYPE_WMFWindows Meta File
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Deprecated Methods Modifier and Type Method Description intaddOlePackage(byte[] oleData, String label, String fileName, String command)Adds an OLE package manager object with the given content to the sheetintaddPicture(byte[] pictureData, int format)Adds a picture to the workbook.voidaddToolPack(UDFFinder toolpack)Register a new toolpack in this workbook.SheetcloneSheet(int sheetNum)Create a Sheet from an existing sheet in the Workbook.voidclose()Close the underlying input resource (File or Stream), from which the Workbook was read.CellStylecreateCellStyle()Create a new Cell style and add it to the workbook's style tableDataFormatcreateDataFormat()Returns the instance of DataFormat for this workbook.EvaluationWorkbookcreateEvaluationWorkbook()FontcreateFont()Create a new Font and add it to the workbook's font tableNamecreateName()Creates a new (uninitialised) defined name in this workbookSheetcreateSheet()Create a Sheet for this Workbook, adds it to the sheets and returns the high level representation.SheetcreateSheet(String sheetname)Create a new sheet for this Workbook and return the high level representation.FontfindFont(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 attributesintgetActiveSheetIndex()Convenience method to get the active sheet.List<? extends Name>getAllNames()Returns all defined names.List<? extends PictureData>getAllPictures()Gets all pictures from the Workbook.CellReferenceTypegetCellReferenceType()CellStylegetCellStyleAt(int idx)Get the cell style object at the given indexCreationHelpergetCreationHelper()Returns an object that handles instantiating concrete classes of the various instances one needs for HSSF and XSSF.intgetFirstVisibleTab()Gets the first tab that is displayed in the list of tabs in excel.FontgetFontAt(int idx)Get the font at the given index numberbooleangetForceFormulaRecalculation()Whether Excel will be asked to recalculate all formulas when the workbook is opened.Row.MissingCellPolicygetMissingCellPolicy()Retrieves the current policy on what to do when getting missing or blank cells from a row.NamegetName(String name)List<? extends Name>getNames(String name)Returns all defined names with the given name.intgetNumberOfFonts()Get the number of fonts in the font tableintgetNumberOfFontsAsInt()Deprecated.intgetNumberOfNames()intgetNumberOfSheets()Get the number of spreadsheets in the workbookintgetNumCellStyles()Get the number of styles the workbook containsStringgetPrintArea(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.SheetgetSheet(String name)Get sheet with the given nameSheetgetSheetAt(int index)Get the Sheet object at the given index.intgetSheetIndex(String name)Returns the index of the sheet by his nameintgetSheetIndex(Sheet sheet)Returns the index of the given sheetStringgetSheetName(int sheet)Get the sheet nameSheetVisibilitygetSheetVisibility(int sheetIx)Get the visibility (visible, hidden, very hidden) of a sheet in this workbookSpreadsheetVersiongetSpreadsheetVersion()Returns the spreadsheet version of this workbookbooleanisHidden()booleanisSheetHidden(int sheetIx)Check whether a sheet is hidden.booleanisSheetVeryHidden(int sheetIx)Check whether a sheet is very hidden.default Iterator<Sheet>iterator()Alias forsheetIterator()to allow foreach loopsintlinkExternalWorkbook(String name, Workbook workbook)Adds the linking required to allow formulas referencing the specified external workbook to be added to this one.voidremoveName(Name name)Remove a defined namevoidremovePrintArea(int sheetIndex)Delete the printarea for the sheet specifiedvoidremoveSheetAt(int index)Removes sheet at the given indexvoidsetActiveSheet(int sheetIndex)Convenience method to set the active sheet.voidsetCellReferenceType(CellReferenceType cellReferenceType)voidsetFirstVisibleTab(int sheetIndex)Sets the first tab that is displayed in the list of tabs in excel.voidsetForceFormulaRecalculation(boolean value)Whether the application shall perform a full recalculation when the workbook is opened.voidsetHidden(boolean hiddenFlag)voidsetMissingCellPolicy(Row.MissingCellPolicy missingCellPolicy)Sets the policy on what to do when getting missing or blank cells from a row.voidsetPrintArea(int sheetIndex, int startColumn, int endColumn, int startRow, int endRow)For the Convenience of Java Programmers maintaining pointers.voidsetPrintArea(int sheetIndex, String reference)Sets the printarea for the sheet providedvoidsetSelectedTab(int index)Sets the tab whose data is actually seen when the sheet is opened.voidsetSheetHidden(int sheetIx, boolean hidden)Hide or unhide a sheet.voidsetSheetName(int sheet, String name)Set the sheet name.voidsetSheetOrder(String sheetname, int pos)Sets the order of appearance for a given sheet.voidsetSheetVisibility(int sheetIx, SheetVisibility visibility)Hide or unhide a sheet.Iterator<Sheet>sheetIterator()Returns an iterator of the sheets in the workbook in sheet order.default Spliterator<Sheet>spliterator()Returns a spliterator of the sheets in the workbook in sheet order.voidwrite(OutputStream stream)Write out this workbook to an OutputStream.
-
-
-
Field Detail
-
PICTURE_TYPE_EMF
static final int PICTURE_TYPE_EMF
Extended windows meta file- See Also:
- Constant Field Values
-
PICTURE_TYPE_WMF
static final int PICTURE_TYPE_WMF
Windows Meta File- See Also:
- Constant Field Values
-
PICTURE_TYPE_PICT
static final int PICTURE_TYPE_PICT
Mac PICT format- See Also:
- Constant Field Values
-
PICTURE_TYPE_JPEG
static final int PICTURE_TYPE_JPEG
JPEG format- See Also:
- Constant Field Values
-
PICTURE_TYPE_PNG
static final int PICTURE_TYPE_PNG
PNG format- See Also:
- Constant Field Values
-
PICTURE_TYPE_DIB
static final int PICTURE_TYPE_DIB
Device independent bitmap- See Also:
- Constant Field Values
-
MAX_SENSITIVE_SHEET_NAME_LEN
static final int MAX_SENSITIVE_SHEET_NAME_LEN
Excel silently truncates long sheet names to 31 chars. This constant is used to ensure uniqueness in the first 31 chars- See Also:
- Constant Field Values
-
-
Method Detail
-
getActiveSheetIndex
int getActiveSheetIndex()
Convenience method to get 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.- Returns:
- the index of the active sheet (0-based)
-
setActiveSheet
void setActiveSheet(int sheetIndex)
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.- Parameters:
sheetIndex- index of the active sheet (0-based)
-
getFirstVisibleTab
int getFirstVisibleTab()
Gets the first tab that is displayed in the list of tabs in excel.- Returns:
- the first tab that to display in the list of tabs (0-based).
-
setFirstVisibleTab
void setFirstVisibleTab(int sheetIndex)
Sets the first tab that is displayed in the list of tabs in excel.- Parameters:
sheetIndex- the first tab that to display in the list of tabs (0-based)
-
setSheetOrder
void setSheetOrder(String sheetname, int pos)
Sets the order of appearance for a given sheet.- Parameters:
sheetname- the name of the sheet to reorderpos- the position that we want to insert the sheet into (0 based)
-
setSelectedTab
void setSelectedTab(int index)
Sets 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).- Parameters:
index- the index of the sheet to select (0 based)- See Also:
Sheet.setSelected(boolean)
-
setSheetName
void setSheetName(int sheet, String name)Set the sheet name.See
WorkbookUtil.createSafeSheetName(String nameProposal)for a safe way to create valid names- Parameters:
sheet- 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
String getSheetName(int sheet)
Get the sheet name- Parameters:
sheet- sheet number (0 based)- Returns:
- Sheet name
-
getSheetIndex
int getSheetIndex(String name)
Returns the index of the sheet by his name- Parameters:
name- the sheet name- Returns:
- index of the sheet (0 based)
-
getSheetIndex
int getSheetIndex(Sheet sheet)
Returns the index of the given sheet- Parameters:
sheet- the sheet to look up- Returns:
- index of the sheet (0 based)
-
createSheet
Sheet createSheet()
Create a Sheet for this Workbook, adds it to the sheets and returns the high level representation. Use this to create new sheets.- Returns:
- Sheet representing the new sheet.
-
createSheet
Sheet 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:
Except the 31-character constraint, Excel applies some other rules: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();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 (])
See
WorkbookUtil.createSafeSheetName(String nameProposal)for a safe way to create valid names- Parameters:
sheetname- The name 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)
-
cloneSheet
Sheet cloneSheet(int sheetNum)
Create a Sheet from an existing sheet in the Workbook.- Returns:
- Sheet representing the cloned sheet.
-
sheetIterator
Iterator<Sheet> sheetIterator()
Returns an iterator of the sheets in the workbook in sheet order. Includes hidden and very hidden sheets.- Returns:
- an iterator of the sheets.
-
iterator
default Iterator<Sheet> iterator()
Alias forsheetIterator()to allow foreach loops
-
spliterator
default Spliterator<Sheet> spliterator()
Returns a spliterator of the sheets in the workbook in sheet order. Includes hidden and very hidden sheets.- Specified by:
spliteratorin interfaceIterable<Sheet>- Returns:
- a spliterator of the sheets.
- Since:
- POI 5.2.0
-
getNumberOfSheets
int getNumberOfSheets()
Get the number of spreadsheets in the workbook- Returns:
- the number of sheets
-
getSheetAt
Sheet getSheetAt(int index)
Get the Sheet object at the given index.- Parameters:
index- of the sheet number (0-based physical & logical)- Returns:
- Sheet at the provided index
- Throws:
IllegalArgumentException- if the index is out of range (index < 0 || index >= getNumberOfSheets()).
-
getSheet
Sheet getSheet(String name)
Get sheet with the given name- Parameters:
name- of the sheet- Returns:
- Sheet with the name provided or
nullif it does not exist
-
removeSheetAt
void removeSheetAt(int index)
Removes sheet at the given index- Parameters:
index- of the sheet to remove (0-based)
-
createFont
Font createFont()
Create a new Font and add it to the workbook's font table- Returns:
- new font object
-
findFont
Font 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- Returns:
- the font with the matched attributes or
null
-
getNumberOfFonts
int getNumberOfFonts()
Get the number of fonts in the font table- Returns:
- number of fonts (as int since POI 5.0.0)
-
getNumberOfFontsAsInt
@Deprecated @Removal(version="6.0.0") int getNumberOfFontsAsInt()
Deprecated.Get the number of fonts in the font table- Returns:
- number of fonts
- Since:
- 4.0.0
-
getFontAt
Font getFontAt(int idx)
Get the font at the given index number- Parameters:
idx- index number (0-based)- Returns:
- font at the index
- Since:
- 4.0.0
-
createCellStyle
CellStyle createCellStyle()
Create a new Cell style and add it to the workbook's style table- Returns:
- the new Cell Style object
- Throws:
IllegalStateException- if the number of cell styles exceeded the limit for this type of Workbook.
-
getNumCellStyles
int getNumCellStyles()
Get the number of styles the workbook contains- Returns:
- count of cell styles
-
getCellStyleAt
CellStyle getCellStyleAt(int idx)
Get the cell style object at the given index- Parameters:
idx- index within the set of styles (0-based)- Returns:
- CellStyle object at the index
-
write
void write(OutputStream stream) throws IOException
Write out this workbook to an OutputStream.- Parameters:
stream- - the java OutputStream you wish to write to- Throws:
IOException- if anything can't be written.
-
close
void close() throws IOExceptionClose the underlying input resource (File or Stream), from which the Workbook was read.Once this has been called, no further operations, updates or reads should be performed on the Workbook.
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException
-
getNumberOfNames
int getNumberOfNames()
- Returns:
- the total number of defined names in this workbook
-
getName
Name getName(String name)
- Parameters:
name- the name of the defined name- Returns:
- the defined name with the specified name.
nullif not found.
-
getNames
List<? extends Name> getNames(String name)
Returns all defined names with the given name.- Parameters:
name- the name of the defined name- Returns:
- a list of the defined names with the specified name. An empty list is returned if none is found.
-
getAllNames
List<? extends Name> getAllNames()
Returns all defined names.- Returns:
- a list of the defined names. An empty list is returned if none is found.
-
createName
Name createName()
Creates a new (uninitialised) defined name in this workbook- Returns:
- new defined name object
-
removeName
void removeName(Name name)
Remove a defined name- Parameters:
name- the name of the defined name
-
linkExternalWorkbook
int linkExternalWorkbook(String name, Workbook workbook)
Adds the linking required to allow formulas referencing the specified external workbook to be added to this one.In order for formulas such as "[MyOtherWorkbook]Sheet3!$A$5" to be added to the file, some linking information must first be recorded. Once a given external workbook has been linked, then formulas using it can added. Each workbook needs linking only once.
This linking only applies for writing formulas. To link things for evaluation, see
FormulaEvaluator.setupReferencedWorkbooks(java.util.Map)- Parameters:
name- The name the workbook will be referenced as in formulasworkbook- The open workbook to fetch the link required information from
-
setPrintArea
void setPrintArea(int sheetIndex, String reference)Sets the printarea for the sheet providedi.e. Reference = $A$1:$B$2
- 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
void setPrintArea(int sheetIndex, int startColumn, int endColumn, int startRow, int endRow)For the Convenience of Java Programmers maintaining pointers.- 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
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.- 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
void removePrintArea(int sheetIndex)
Delete the printarea for the sheet specified- Parameters:
sheetIndex- Zero-based sheet index (0 = First Sheet)
-
getMissingCellPolicy
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
-
setMissingCellPolicy
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 toRow.getCell(int)}. SeeRow.MissingCellPolicy
-
createDataFormat
DataFormat createDataFormat()
Returns the instance of DataFormat for this workbook.- Returns:
- the DataFormat object
-
addPicture
int addPicture(byte[] pictureData, int format)Adds a picture to the workbook.- Parameters:
pictureData- The bytes of the pictureformat- The format of the picture.- Returns:
- the index to this picture (1 based).
- See Also:
PICTURE_TYPE_EMF,PICTURE_TYPE_WMF,PICTURE_TYPE_PICT,PICTURE_TYPE_JPEG,PICTURE_TYPE_PNG,PICTURE_TYPE_DIB
-
getAllPictures
List<? extends PictureData> getAllPictures()
Gets all pictures from the Workbook.- Returns:
- the list of pictures (a list of
PictureDataobjects.)
-
getCreationHelper
CreationHelper getCreationHelper()
Returns an object that handles instantiating concrete classes of the various instances one needs for HSSF and XSSF.
-
isHidden
boolean isHidden()
- Returns:
falseif this workbook is not visible in the GUI
-
setHidden
void setHidden(boolean hiddenFlag)
- Parameters:
hiddenFlag- passfalseto make the workbook visible in the GUI
-
isSheetHidden
boolean isSheetHidden(int sheetIx)
Check whether a sheet is hidden.Note that a sheet could instead be set to be very hidden, which is different (
isSheetVeryHidden(int))- Parameters:
sheetIx- Number- Returns:
trueif sheet is hidden- See Also:
getSheetVisibility(int)
-
isSheetVeryHidden
boolean isSheetVeryHidden(int sheetIx)
Check whether a sheet is very hidden.This is different from the normal hidden status (
isSheetHidden(int))- Parameters:
sheetIx- sheet index to check- Returns:
trueif sheet is very hidden- See Also:
getSheetVisibility(int)
-
setSheetHidden
void setSheetHidden(int sheetIx, boolean hidden)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.- Parameters:
sheetIx- the sheet index (0-based)hidden- True to mark the sheet as hidden, false otherwise- See Also:
setSheetVisibility(int, SheetVisibility)
-
getSheetVisibility
SheetVisibility getSheetVisibility(int sheetIx)
Get the visibility (visible, hidden, very hidden) of a sheet in this workbook- Parameters:
sheetIx- the index of the sheet- Returns:
- the sheet visibility
- Since:
- POI 3.16 beta 2
-
setSheetVisibility
void setSheetVisibility(int sheetIx, SheetVisibility visibility)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.- Parameters:
sheetIx- the sheet index (0-based)visibility- the sheet visibility to set- Since:
- POI 3.16 beta 2
-
addToolPack
void addToolPack(UDFFinder toolpack)
Register a new toolpack in this workbook.- Parameters:
toolpack- the toolpack to register
-
setForceFormulaRecalculation
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.
- Parameters:
value- true if the application will perform a full recalculation of workbook values when the workbook is opened- Since:
- 3.8
-
getForceFormulaRecalculation
boolean getForceFormulaRecalculation()
Whether Excel will be asked to recalculate all formulas when the workbook is opened.- Since:
- 3.8
-
getSpreadsheetVersion
SpreadsheetVersion getSpreadsheetVersion()
Returns the spreadsheet version of this workbook- Returns:
- SpreadsheetVersion enum
- Since:
- 3.14 beta 2
-
addOlePackage
int addOlePackage(byte[] oleData, String label, String fileName, String command) throws IOExceptionAdds an OLE package manager object with the given content to the sheet- 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
-
createEvaluationWorkbook
EvaluationWorkbook createEvaluationWorkbook()
- Returns:
- an evaluation workbook
-
getCellReferenceType
CellReferenceType getCellReferenceType()
- Returns:
- the type of cell references used
- Since:
- POI 5.2.1
-
setCellReferenceType
void setCellReferenceType(CellReferenceType cellReferenceType)
- Parameters:
cellReferenceType- the type of cell references used- Since:
- POI 5.2.1
-
-