Once you have finished working with the Workbook, you should close the package by calling either close() or OPCPackage.close(), to avoid leaving file handles open. Creating a XSSFWorkbook from a file-backed OPC Package has a lower memory footprint than an InputStream backed one.Parameters:pkg - the OpenXML4J OPC Package object.Throws:java.io.IOException - If reading data from the package failsPOIXMLException - a RuntimeException that can be caused by invalid OOXML datajava.lang.RuntimeException - a number of other runtime exceptions can be thrown, especially if there are problems with the input formatXSSFWorkbookpublic XSSFWorkbook(java.io.InputStream is) throws java.io.IOExceptionConstructs a XSSFWorkbook object, by buffering the whole stream into memory and then opening an OPCPackage object for it. Using an InputStream requires more memory than using a File, so if a File is available then you should instead do something like OPCPackage pkg = OPCPackage.open(path); XSSFWorkbook wb = new XSSFWorkbook(pkg); // work with the wb object ...... pkg.close(); // gracefully closes the underlying zip file Throws:java.io.IOException - If reading data from the stream failsPOIXMLException - a RuntimeException that can be caused by invalid OOXML datajava.lang.RuntimeException - a number of other runtime exceptions can be thrown, especially if there are problems with the input formatXSSFWorkbookpublic XSSFWorkbook(java.io.File file) throws java.io.IOException, InvalidFormatExceptionConstructs a XSSFWorkbook object from a given file. Once you have finished working with the Workbook, you should close the package by calling close(), to avoid leaving file handles open. Opening a XSSFWorkbook from a file has a lower memory footprint than opening from an InputStreamParameters:file - the file to openThrows:java.io.IOException - If reading data from the file failsInvalidFormatException - If the file has a format that cannot be read or if the file is corruptedPOIXMLException - a RuntimeException that can be caused by invalid OOXML datajava.lang.RuntimeException - a number of other runtime exceptions can be thrown, especially if there are problems with the input formatXSSFWorkbookpublic XSSFWorkbook(java.lang.String path) throws java.io.IOExceptionConstructs a XSSFWorkbook object given a file name. Once you have finished working with the Workbook, you should close the package by calling close(), to avoid leaving file handles open. Opening a XSSFWorkbook from a file has a lower memory footprint than opening from an InputStreamParameters:path - the file name.Throws:java.io.IOException - If reading data from the file failsPOIXMLException - a RuntimeException that can be caused by invalid OOXML datajava.lang.RuntimeException - a number of other runtime exceptions can be thrown, especially if there are problems with the input formatXSSFWorkbookpublic XSSFWorkbook(PackagePart part) throws java.io.IOExceptionConstructs a XSSFWorkbook object using Package Part.Parameters:part - package partThrows:java.io.IOException - If reading data from the Package Part failsPOIXMLException - a RuntimeException that can be caused by invalid OOXML datajava.lang.RuntimeException - a number of other runtime exceptions can be thrown, especially if there are problems with the input formatSince:POI 4.0.0Method DetailgetXssfFactorypublic XSSFFactory getXssfFactory()Returns:the XSSFFactorySince:POI 5.1.0beforeDocumentReadprotected void beforeDocumentRead()onDocumentReadprotected void onDocumentRead() throws java.io.IOExceptionDescription copied from class: POIXMLDocumentPartFired when a package part is readOverrides:onDocumentRead in class POIXMLDocumentPartThrows:java.io.IOException - a subclass may throw an IOException when a document is readparseSheetpublic void parseSheet(java.util.Map shIdMap, org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet ctSheet)Not normally to be called externally, but possibly to be overridden to avoid the DOM based parse of large sheets (see examples).Throws:POIXMLException - a RuntimeException that can be caused by invalid OOXML datajava.lang.RuntimeException - a number of other runtime exceptions can be thrown, especially if there are problems with the input formatnewPackageprotected static OPCPackage newPackage(XSSFWorkbookType workbookType)Create a new SpreadsheetML package and setup the default minimal contentgetCTWorkbook@Internalpublic org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbook getCTWorkbook()Return the underlying XML beanReturns:the underlying CTWorkbook beanaddPicturepublic int addPicture(byte[] pictureData, int format)Adds a picture to the workbook.Specified by:addPicture in interface WorkbookParameters:pictureData - The bytes of the pictureformat - The format of the picture.Returns:the index to this picture (0 based), the added picture can be obtained from getAllPictures() .See Also:Workbook.PICTURE_TYPE_EMF, Workbook.PICTURE_TYPE_WMF, Workbook.PICTURE_TYPE_PICT, Workbook.PICTURE_TYPE_JPEG, Workbook.PICTURE_TYPE_PNG, Workbook.PICTURE_TYPE_DIB, getAllPictures()addPicturepublic int addPicture(java.io.InputStream is, int format) throws java.io.IOExceptionAdds a picture to the workbook.Parameters:is - The sream to read image fromformat - The format of the picture.Returns:the index to this picture (0 based), the added picture can be obtained from getAllPictures() .Throws:java.io.IOExceptionSee Also:Workbook.PICTURE_TYPE_EMF, Workbook.PICTURE_TYPE_WMF, Workbook.PICTURE_TYPE_PICT, Workbook.PICTURE_TYPE_JPEG, Workbook.PICTURE_TYPE_PNG, Workbook.PICTURE_TYPE_DIB, getAllPictures()cloneSheetpublic XSSFSheet cloneSheet(int sheetNum)Create an XSSFSheet from an existing sheet in the XSSFWorkbook. The cloned sheet is a deep copy of the original.Specified by:cloneSheet in interface WorkbookParameters:sheetNum - The index of the sheet to cloneReturns:XSSFSheet representing the cloned sheet.Throws:java.lang.IllegalArgumentException - if the sheet index in invalidPOIXMLException - if there were errors when cloningclosepublic void close() throws java.io.IOExceptionDescription copied from class: POIXMLDocumentCloses the underlying OPCPackage from which this document was read, if there is one Once this has been called, no further operations, updates or reads should be performed on the document.Specified by:close in interface java.io.CloseableSpecified by:close in interface java.lang.AutoCloseableSpecified by:close in interface WorkbookOverrides:close in class POIXMLDocumentThrows:java.io.IOException - for writable packages, if an IO exception occur during the saving process.cloneSheetpublic XSSFSheet cloneSheet(int sheetNum, java.lang.String newName)Create an XSSFSheet from an existing sheet in the XSSFWorkbook. The cloned sheet is a deep copy of the original but with a new given name.Parameters:sheetNum - The index of the sheet to clonenewName - The name to set for the newly created sheetReturns:XSSFSheet representing the cloned sheet.Throws:java.lang.IllegalArgumentException - if the sheet index or the sheet name is invalidPOIXMLException - if there were errors when cloningcreateCellStylepublic XSSFCellStyle createCellStyle()Create a new XSSFCellStyle and add it to the workbook's style tableSpecified by:createCellStyle in interface WorkbookReturns:the new XSSFCellStyle objectcreateDataFormatpublic XSSFDataFormat createDataFormat()Returns the workbook's data format table (a factory for creating data format strings).Specified by:createDataFormat in interface WorkbookReturns:the XSSFDataFormat objectSee Also:DataFormatcreateFontpublic XSSFFont createFont()Create a new Font and add it to the workbook's font tableSpecified by:createFont in interface WorkbookReturns:new font objectcreateNamepublic XSSFName createName()Description copied from interface: WorkbookCreates a new (uninitialised) defined name in this workbookSpecified by:createName in interface WorkbookReturns:new defined name objectcreateSheetpublic XSSFSheet createSheet()Create an XSSFSheet for this workbook, adds it to the sheets and returns the high level representation. Use this to create new sheets.Specified by:createSheet in interface WorkbookReturns:XSSFSheet representing the new sheet.createSheetpublic XSSFSheet createSheet(java.lang.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 (])
The string MUST NOT begin or end with the single quote (') character. See WorkbookUtil.createSafeSheetName(String nameProposal) for a safe way to create valid namesSpecified by:createSheet in interface WorkbookParameters:sheetname - sheetname to set for the sheet.Returns:Sheet representing the new sheet.Throws:java.lang.IllegalArgumentException - if the name is null or invalid or workbook already contains a sheet with this nameSee Also:WorkbookUtil.createSafeSheetName(String nameProposal)createDialogsheetprotected XSSFDialogsheet createDialogsheet(java.lang.String sheetname, org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDialogsheet dialogsheet)findFontpublic XSSFFont findFont(boolean bold, short color, short fontHeight, java.lang.String name, boolean italic, boolean strikeout, short typeOffset, byte underline)Finds a font that matches the one with the supplied attributesSpecified by:findFont in interface WorkbookReturns:the font with the matched attributes or nullgetActiveSheetIndexpublic 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.Specified by:getActiveSheetIndex in interface WorkbookReturns:the index of the active sheet (0-based)getAllPicturespublic java.util.List getAllPictures()Gets all pictures from the Workbook.Specified by:getAllPictures in interface WorkbookReturns:the list of pictures (a list of XSSFPictureData objects.)See Also:addPicture(byte[], int)getCellStyleAtpublic XSSFCellStyle getCellStyleAt(int idx)Get the cell style object at the given indexSpecified by:getCellStyleAt in interface WorkbookParameters:idx - index within the set of stylesReturns:XSSFCellStyle object at the indexgetFontAtpublic XSSFFont getFontAt(int idx)Description copied from interface: WorkbookGet the font at the given index numberSpecified by:getFontAt in interface WorkbookParameters:idx - index number (0-based)Returns:font at the indexgetNamepublic XSSFName getName(java.lang.String name)Get the first named range with the given name. Note: names of named ranges are not unique as they are scoped by sheet. getNames(String name) returns all named ranges with the given name.Specified by:getName in interface WorkbookParameters:name - named range nameReturns:XSSFName with the given name. null is returned no named range could be found.getNamespublic java.util.List getNames(java.lang.String name)Get the named ranges with the given name.
Note:Excel named ranges are case-insensitive and this method performs a case-insensitive search.Specified by:getNames in interface WorkbookParameters:name - named range nameReturns:list of XSSFNames with the given name. An empty list if no named ranges could be foundgetAllNamespublic java.util.List getAllNames()Get a list of all the named ranges in the workbook.Specified by:getAllNames in interface WorkbookReturns:list of XSSFNames in the workbookgetNumCellStylespublic int getNumCellStyles()Get the number of styles the workbook containsSpecified by:getNumCellStyles in interface WorkbookReturns:count of cell stylesgetNumberOfFontspublic int getNumberOfFonts()Description copied from interface: WorkbookGet the number of fonts in the font tableSpecified by:getNumberOfFonts in interface WorkbookReturns: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: WorkbookGet the number of fonts in the font tableSpecified by:getNumberOfFontsAsInt in interface WorkbookReturns:number of fontsgetNumberOfNamespublic int getNumberOfNames()Get the number of named ranges in the this workbookSpecified by:getNumberOfNames in interface WorkbookReturns:number of named rangesgetNumberOfSheetspublic int getNumberOfSheets()Get the number of worksheets in the this workbookSpecified by:getNumberOfSheets in interface WorkbookReturns:number of worksheetsgetPrintAreapublic java.lang.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 interface WorkbookParameters:sheetIndex - Zero-based sheet index (0 Represents the first sheet to keep consistent with java)Returns:String Null if no print area has been definedgetSheetpublic XSSFSheet getSheet(java.lang.String name)Get sheet with the given name (case insensitive match)Specified by:getSheet in interface WorkbookParameters:name - of the sheetReturns:XSSFSheet with the name provided or null if it does not existgetSheetAtpublic XSSFSheet getSheetAt(int index)Get the XSSFSheet object at the given index.Specified by:getSheetAt in interface WorkbookParameters:index - of the sheet number (0-based physical & logical)Returns:XSSFSheet at the provided indexThrows:java.lang.IllegalArgumentException - if the index is out of range (index < 0 index >= getNumberOfSheets()).getSheetIndexpublic int getSheetIndex(java.lang.String name)Returns the index of the sheet by his name (case insensitive match)Specified by:getSheetIndex in interface WorkbookParameters:name - the sheet nameReturns:index of the sheet (0 based) or -1 if not foundgetSheetIndexpublic int getSheetIndex(Sheet sheet)Returns the index of the given sheetSpecified by:getSheetIndex in interface WorkbookParameters:sheet - the sheet to look upReturns:index of the sheet (0 based). -1 if not foundgetSheetNamepublic java.lang.String getSheetName(int sheetIx)Get the sheet nameSpecified by:getSheetName in interface WorkbookParameters:sheetIx - NumberReturns:Sheet namesheetIteratorpublic java.util.Iterator sheetIterator()Returns an iterator of the sheets in the workbook in sheet order. Includes hidden and very hidden sheets. Note: remove() is not supported on this iterator. Use removeSheetAt(int) to remove sheets instead.Specified by:sheetIterator in interface WorkbookReturns:an iterator of the sheets.iteratorpublic java.util.Iterator iterator()Alias for sheetIterator() to allow foreach loops Note: remove() is not supported on this iterator. Use removeSheetAt(int) to remove sheets instead.Specified by:iterator in interface java.lang.IterableSpecified by:iterator in interface WorkbookReturns:an iterator of the sheets.spliteratorpublic java.util.Spliterator spliterator()Returns a spliterator of the sheets in the workbook in sheet order. Includes hidden and very hidden sheets.Specified by:spliterator in interface java.lang.IterableSpecified by:spliterator in interface WorkbookReturns:a spliterator of the sheets.Since:POI 5.2.0isMacroEnabledpublic boolean isMacroEnabled()Are we a normal workbook (.xlsx), or a macro enabled workbook (.xlsm)?removeNamepublic void removeName(Name name)Description copied from interface: WorkbookRemove a defined nameSpecified by:removeName in interface WorkbookParameters:name - the name to remove.Throws:java.lang.IllegalArgumentException - if the named range is not a part of this XSSFWorkbookremovePrintAreapublic void removePrintArea(int sheetIndex)Delete the printarea for the sheet specifiedSpecified by:removePrintArea in interface WorkbookParameters:sheetIndex - 0-based sheet index (0 = First Sheet)removeSheetAtpublic 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 interface WorkbookParameters:index - of the sheet (0-based)getMissingCellPolicypublic 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.MissingCellPolicySpecified by:getMissingCellPolicy in interface WorkbooksetMissingCellPolicypublic 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 to Row.getCell(int)}. See Row.MissingCellPolicySpecified by:setMissingCellPolicy in interface WorkbooksetActiveSheetpublic 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 interface WorkbookParameters:index - index of the active sheet (0-based)getFirstVisibleTabpublic int getFirstVisibleTab()Gets the first tab that is displayed in the list of tabs in excel.Specified by:getFirstVisibleTab in interface WorkbookReturns:integer that contains the index to the active sheet in this book view.setFirstVisibleTabpublic void setFirstVisibleTab(int index)Sets the first tab that is displayed in the list of tabs in excel.Specified by:setFirstVisibleTab in interface WorkbookParameters:index - integer that contains the index to the active sheet in this book view.setPrintAreapublic void setPrintArea(int sheetIndex, java.lang.String reference)Sets the printarea for the sheet provided i.e. Reference = $A$1:$B$2Specified by:setPrintArea in interface WorkbookParameters:sheetIndex - Zero-based sheet index (0 Represents the first sheet to keep consistent with java)reference - Valid name Reference for the Print AreasetPrintAreapublic void setPrintArea(int sheetIndex, int startColumn, int endColumn, int startRow, int endRow)For the Convenience of Java Programmers maintaining pointers.Specified by:setPrintArea in interface WorkbookParameters:sheetIndex - Zero-based sheet index (0 = First Sheet)startColumn - Colu