Package loci.poi.hssf.usermodel
Class HSSFWorkbook
java.lang.Object
loci.poi.POIDocument
loci.poi.hssf.usermodel.HSSFWorkbook
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.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final byteDeprecated.POI will now properly handle unicode strings without forceing an encodingstatic final byteDeprecated.POI will now properly handle unicode strings without forceing an encodingstatic final intused for compile-time performance/memory optimization.static final intDevice independant bitmapstatic final intExtended windows meta filestatic final intJPEG formatstatic final intMac PICT formatstatic final intPNG formatstatic final intWindows Meta Fileprotected ArrayListthis holds the HSSFSheet objects attached to this workbookFields inherited from class loci.poi.POIDocument
dsInf, filesystem, logger, sInf -
Constructor Summary
ConstructorsModifierConstructorDescriptionCreates new HSSFWorkbook from scratch (start here!)HSSFWorkbook(loci.common.RandomAccessInputStream s) HSSFWorkbook(loci.common.RandomAccessInputStream s, boolean preserveNodes) Companion to HSSFWorkbook(POIFSFileSystem), this constructs the POI filesystem around your inputstream.protectedHSSFWorkbook(Workbook book) HSSFWorkbook(POIFSFileSystem fs, boolean preserveNodes) given a POI POIFSFileSystem object, read in its Workbook and populate the high and low level models. -
Method Summary
Modifier and TypeMethodDescriptionintaddPicture(byte[] pictureData, int format) Adds a picture to the workbook.intaddSSTString(String string) Deprecated.Do not call this method from your applications.cloneSheet(int sheetNum) create an HSSFSheet from an existing sheet in the HSSFWorkbook.create a new Cell style and add it to the workbook's style tableReturns the instance of HSSFDataFormat for this workbook.create a new Font and add it to the workbook's font tablecreates a new named range and add it to the modelcreate an HSSFSheet for this HSSFWorkbook, adds it to the sheets and returns the high level representation.createSheet(String sheetname) create an HSSFSheet for this HSSFWorkbook, adds it to the sheets and returns the high level representation.voiddumpDrawingGroupRecords(boolean fat) Spits out a list of all the drawing records in the workbook.findFont(short boldWeight, short color, short fontHeight, String name, boolean italic, boolean strikeout, short typeOffset, byte underline) Finds a font that matches the one with the supplied attributesGets all embedded OLE2 objects from the Workbook.Gets all pictures from the Workbook.booleandetermine 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.getCellStyleAt(short idx) get the cell style object at the given indexshortsets the first tab that is displayed in the list of tabs in excel.getFontAt(short idx) get the font at the given index numbergetNameAt(int index) gets the Named rangeintgetNameIndex(String name) gets the named range index by his name Note:Excel named ranges are case-insensitive and this method performs a case-insensitive search.getNameName(int index) gets the named range nameshortget the number of fonts in the font tableintgets the total number of named ranges in the workbokointget the number of spreadsheets in the workbook (this will be three after serialization)shortget the number of styles the workbook containsgetPrintArea(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.shortgets the tab whose data is actually seen when the sheet is opened.Get sheet with the given namegetSheetAt(int index) Get the HSSFSheet object at the given index.intgetSheetIndex(String name) Returns the index of the sheet by his nameintgetSheetIndex(HSSFSheet sheet) Returns the index of the given sheetgetSheetName(int sheet) get the sheet namegetSSTString(int index) Deprecated.Do not call this method from your applications.protected WorkbookvoidTest only.voidremoveName(int index) remove the named range by his indexvoidremoveName(String name) remove the named range by his namevoidremovePrintArea(int sheetIndex) Delete the printarea for the sheet specifiedvoidremoveSheetAt(int index) removes sheet at the given indexvoidsetBackupFlag(boolean backupValue) determine whether the Excel GUI will backup the workbook when saving.voidsetDisplayedTab(short index) sets the first tab that is displayed in the list of tabs in excel.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 providedvoidsetRepeatingRowsAndColumns(int sheetIndex, int startColumn, int endColumn, int startRow, int endRow) Sets the repeating rows and columns for a sheet (as found in File->PageSetup->Sheet).voidsetSelectedTab(short index) sets the tab whose data is actually seen when the sheet is opened.voidsetSheetName(int sheet, String name) set the sheet name.voidsetSheetName(int sheet, String name, short encoding) Deprecated.3-Jan-2006 POI now automatically detects unicode and sets the encoding appropriately.voidsetSheetOrder(String sheetname, int pos) sets the order of appearance for a given sheet.voidremoves the write protect flagvoidwrite(OutputStream stream) Method write - write out this workbook to an Outputstream.voidwriteProtectWorkbook(String password, String username) protect a workbook with a password (not encypted, just sets writeprotect flags and the password.Methods inherited from class loci.poi.POIDocument
copyNodes, getDocumentSummaryInformation, getPropertySet, getSummaryInformation, readProperties, writeProperties, writeProperties, writePropertySet
-
Field Details
-
INITIAL_CAPACITY
public static final int INITIAL_CAPACITYused 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!- See Also:
-
sheets
this holds the HSSFSheet objects attached to this workbook -
PICTURE_TYPE_EMF
public static final int PICTURE_TYPE_EMFExtended windows meta file- See Also:
-
PICTURE_TYPE_WMF
public static final int PICTURE_TYPE_WMFWindows Meta File- See Also:
-
PICTURE_TYPE_PICT
public static final int PICTURE_TYPE_PICTMac PICT format- See Also:
-
PICTURE_TYPE_JPEG
public static final int PICTURE_TYPE_JPEGJPEG format- See Also:
-
PICTURE_TYPE_PNG
public static final int PICTURE_TYPE_PNGPNG format- See Also:
-
PICTURE_TYPE_DIB
public static final int PICTURE_TYPE_DIBDevice independant bitmap- See Also:
-
ENCODING_COMPRESSED_UNICODE
public static final byte ENCODING_COMPRESSED_UNICODEDeprecated.POI will now properly handle unicode strings without forceing an encoding- See Also:
-
ENCODING_UTF_16
public static final byte ENCODING_UTF_16Deprecated.POI will now properly handle unicode strings without forceing an encoding- See Also:
-
-
Constructor Details
-
HSSFWorkbook
public HSSFWorkbook()Creates new HSSFWorkbook from scratch (start here!) -
HSSFWorkbook
-
HSSFWorkbook
- Throws:
IOException
-
HSSFWorkbook
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 preseve 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 read- See Also:
-
HSSFWorkbook
- Throws:
IOException
-
HSSFWorkbook
public HSSFWorkbook(loci.common.RandomAccessInputStream s, boolean preserveNodes) throws IOException Companion to HSSFWorkbook(POIFSFileSystem), this constructs the POI filesystem around your inputstream.- Parameters:
s- the POI filesystem that contains the Workbook stream.preserveNodes- whether to preseve other nodes, such as macros. This takes more memory, so only say yes if you need to.- Throws:
IOException- if the stream cannot be read- See Also:
-
-
Method Details
-
setSheetOrder
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
public void setSelectedTab(short 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-- See Also:
-
getSelectedTab
public short getSelectedTab()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).- See Also:
-
setDisplayedTab
public void setDisplayedTab(short index) sets the first tab that is displayed in the list of tabs in excel.- Parameters:
index-
-
getDisplayedTab
public short getDisplayedTab()sets the first tab that is displayed in the list of tabs in excel. -
setSheetName
set the sheet name. Will throw IllegalArgumentException if the name is greater than 31 chars or contains /\?*[]- Parameters:
sheet- number (0 based)
-
setSheetName
Deprecated.3-Jan-2006 POI now automatically detects unicode and sets the encoding appropriately. Simply use setSheetName(int sheet, String encoding)set the sheet name forcing the encoding. Forcing the encoding IS A BAD IDEA!!!- Parameters:
sheet- number (0 based)- Throws:
IllegalArgumentException- if the name is greater than 31 chars or contains /\?*[]
-
getSheetName
get the sheet name- Parameters:
sheet- Number- Returns:
- Sheet name
-
getSheetIndex
Returns the index of the sheet by his name- Parameters:
name- the sheet name- Returns:
- index of the sheet (0 based)
-
getSheetIndex
Returns the index of the given sheet- Parameters:
sheet- the sheet to look up- Returns:
- index of the sheet (0 based)
-
createSheet
create an HSSFSheet for this HSSFWorkbook, adds it to the sheets and returns the high level representation. Use this to create new sheets.- Returns:
- HSSFSheet representing the new sheet.
-
cloneSheet
create an HSSFSheet from an existing sheet in the HSSFWorkbook.- Returns:
- HSSFSheet representing the cloned sheet.
-
createSheet
create an HSSFSheet for this HSSFWorkbook, adds it to the sheets and returns the high level representation. Use this to create new sheets.- Parameters:
sheetname- sheetname to set for the sheet.- Returns:
- HSSFSheet representing the new sheet.
-
getNumberOfSheets
public int getNumberOfSheets()get the number of spreadsheets in the workbook (this will be three after serialization)- Returns:
- number of sheets
-
getSheetAt
Get the HSSFSheet object at the given index.- Parameters:
index- of the sheet number (0-based physical & logical)- Returns:
- HSSFSheet at the provided index
-
getSheet
Get sheet with the given name- Parameters:
name- of the sheet- Returns:
- HSSFSheet with the name provided or null if it does not exist
-
removeSheetAt
public void removeSheetAt(int index) removes sheet at the given index- 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.
-
setRepeatingRowsAndColumns
public void setRepeatingRowsAndColumns(int sheetIndex, int startColumn, int endColumn, int startRow, int endRow) Sets the repeating rows and columns for a sheet (as found in File->PageSetup->Sheet). This is function is included in the workbook because it creates/modifies name records which are stored at the workbook level.To set just repeating columns:
workbook.setRepeatingRowsAndColumns(0,0,1,-1-1);
To set just repeating rows:workbook.setRepeatingRowsAndColumns(0,-1,-1,0,4);
To remove all repeating rows and columns for a sheet.workbook.setRepeatingRowsAndColumns(0,-1,-1,-1,-1);
- Parameters:
sheetIndex- 0 based index to sheet.startColumn- 0 based start of repeating columns.endColumn- 0 based end of repeating columns.startRow- 0 based start of repeating rows.endRow- 0 based end of repeating rows.
-
createFont
create a new Font and add it to the workbook's font table- Returns:
- new font object
-
findFont
public HSSFFont findFont(short boldWeight, 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 short getNumberOfFonts()get the number of fonts in the font table- Returns:
- number of fonts
-
getFontAt
get the font at the given index number- Parameters:
idx- index number- Returns:
- HSSFFont at the index
-
createCellStyle
create a new Cell style and add it to the workbook's style table- Returns:
- the new Cell Style object
-
getNumCellStyles
public short getNumCellStyles()get the number of styles the workbook contains- Returns:
- count of cell styles
-
getCellStyleAt
get the cell style object at the given index- Parameters:
idx- index within the set of styles- Returns:
- HSSFCellStyle object at the index
-
write
Method write - write out this workbook to an Outputstream. Constructs a new POI POIFSFileSystem, passes in the workbook binary representation and writes it out.- Parameters:
stream- - the java OutputStream you wish to write the XLS to- Throws:
IOException- if anything can't be written.- See Also:
-
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. -
addSSTString
Deprecated.Do not call this method from your applications. Use the methods available in the HSSFRow to add string HSSFCells -
getSSTString
Deprecated.Do not call this method from your applications. Use the methods available in the HSSFRow to get string HSSFCells -
getWorkbook
-
getNumberOfNames
public int getNumberOfNames()gets the total number of named ranges in the workboko- Returns:
- number of named ranges
-
getNameAt
gets the Named range- Parameters:
index- position of the named range- Returns:
- named range high level
-
getNameName
gets the named range name- Parameters:
index- the named range index (0 based)- Returns:
- named range name
-
setPrintArea
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
public 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:
-
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.- 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- Parameters:
sheetIndex- Zero-based sheet index (0 = First Sheet)
-
createName
creates a new named range and add it to the model- Returns:
- named range high level
-
getNameIndex
gets the named range index by his name Note:Excel named ranges are case-insensitive and this method performs a case-insensitive search.- Parameters:
name- named range name- Returns:
- named range index
-
removeName
public void removeName(int index) remove the named range by his index- Parameters:
index- named range index (0 based)
-
createDataFormat
Returns the instance of HSSFDataFormat for this workbook.- Returns:
- the HSSFDataFormat object
- See Also:
-
removeName
remove the named range by his name- Parameters:
name- named range name
-
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.- Parameters:
pictureData- The bytes of the pictureformat- The format of the picture. One ofPICTURE_TYPE_*- Returns:
- the index to this picture (1 based).
-
getAllPictures
Gets all pictures from the Workbook.- Returns:
- the list of pictures (a list of
HSSFPictureDataobjects.)
-
writeProtectWorkbook
protect a workbook with a password (not encypted, just sets writeprotect flags and the password.- Parameters:
password- to set
-
unwriteProtectWorkbook
public void unwriteProtectWorkbook()removes the write protect flag -
getAllEmbeddedObjects
Gets all embedded OLE2 objects from the Workbook.- Returns:
- the list of embedded objects (a list of
HSSFObjectDataobjects.)
-