Apache POI | Getting Started

11 Jul, 2022
POI stands For “Poor Obfuscation Implementation”. Apache POI is an API provided by Apache foundation which is a collection of different java libraries. These libraries gives the facility to read, write and manipulate different Microsoft files such as excel sheet, power-point, and word files. It’s first version release on 30 December 2001.
Apache POI have different classes and method to work upon different MS Office Document.
POIFSIt’s Stand for “Poor Obfuscation Implementation File System”.This component is the basic factor of all other POI elements. It is used to read different files explicitly.
HSSFIt’s Stand for “Horrible Spreadsheet Format”. It is used to read and write xls format of MS-Excel files.
XSSFIt’s Stand for “XML Spreadsheet Format”. It is used for xlsx file format of MS-Excel.
HPSFIt’s Stand for “Horrible Property Set Format”.It is used to extract property sets of the MS-Office files.
HWPFIt’s Stand for “Horrible Word Processor Format”.It is used to read and write doc extension files of MS-Word.
XWPFIt’s Stand for “XML Word Processor Format”.It is used to read and write docx extension files of MS-Word.
HSLFIt’s Stand for “Horrible Slide Layout Format”.It is used for read, create, and edit PowerPoint presentations.
HDGFIt’s Stand for “Horrible Diagram Format”.It contains classes and methods for MS-Visio binary files.
HPBFIt’s Stand for “Horrible Publisher Format”. use for read and write MS-Publisher files.
Their are two ways for installing apache jar file depending upon the type of project:
Maven ProjectIf the project is MAVEN then add dependency in pom.xml file in the project.The dependency is to be added is as given below:<!-- https://mvnrepository.com/artifact/org.apache.poi/poi --><dependency>      <groupId>org.apache.poi</groupId>      <artifactId>poi</artifactId>      <version>3.12</version>    </dependency>    <dependency>      <groupId>org.apache.poi</groupId>      <artifactId>poi-ooxml</artifactId>      <version>3.12</version>    </dependency>Steps to create a maven project in eclipse and add dependencyClick on file->new->maven projectA new window appears, Click on NextSelect maven-archetype-webappGive name of the projectA project is formed in the workspace and a pom.xml file automatically appearsOpen this file in the existing structure of pom.xml fileCopy apache poi dependency in pom.xml fileMaven dependency is added when the pom.xml file is saved after copying the maven dependency.Simple Java ProjectIf not using maven, then one can download maven jar files from POI download. Include following jar files minimum to run the sample code:poi-3.10-FINAL.jarpoi-ooxml-3.10-FINAL.jarcommons-codec-1.5.jarpoi-ooxml-schemas-3.10-FINAL.jarxml-apis-1.0.b2.jarstax-api-1.0.1.jarxmlbeans-2.3.0.jardom4j-1.6.1.jarFollow this Link to see how to add external jars in eclipse.
Maven ProjectIf the project is MAVEN then add dependency in pom.xml file in the project.The dependency is to be added is as given below:<!-- https://mvnrepository.com/artifact/org.apache.poi/poi --><dependency>      <groupId>org.apache.poi</groupId>      <artifactId>poi</artifactId>      <version>3.12</version>    </dependency>    <dependency>      <groupId>org.apache.poi</groupId>      <artifactId>poi-ooxml</artifactId>      <version>3.12</version>    </dependency>Steps to create a maven project in eclipse and add dependencyClick on file->new->maven projectA new window appears, Click on NextSelect maven-archetype-webappGive name of the projectA project is formed in the workspace and a pom.xml file automatically appearsOpen this file in the existing structure of pom.xml fileCopy apache poi dependency in pom.xml fileMaven dependency is added when the pom.xml file is saved after copying the maven dependency.
If the project is MAVEN then add dependency in pom.xml file in the project.The dependency is to be added is as given below:
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi --><dependency>      <groupId>org.apache.poi</groupId>      <artifactId>poi</artifactId>      <version>3.12</version>    </dependency>    <dependency>      <groupId>org.apache.poi</groupId>      <artifactId>poi-ooxml</artifactId>      <version>3.12</version>    </dependency>
Steps to create a maven project in eclipse and add dependency
Click on file->new->maven project
A new window appears, Click on Next
Select maven-archetype-webapp
Give name of the project
A project is formed in the workspace and a pom.xml file automatically appears
Open this file in the existing structure of pom.xml file
Copy apache poi dependency in pom.xml file
Maven dependency is added when the pom.xml file is saved after copying the maven dependency.
Simple Java ProjectIf not using maven, then one can download maven jar files from POI download. Include following jar files minimum to run the sample code:poi-3.10-FINAL.jarpoi-ooxml-3.10-FINAL.jarcommons-codec-1.5.jarpoi-ooxml-schemas-3.10-FINAL.jarxml-apis-1.0.b2.jarstax-api-1.0.1.jarxmlbeans-2.3.0.jardom4j-1.6.1.jarFollow this Link to see how to add external jars in eclipse.
If not using maven, then one can download maven jar files from POI download. Include following jar files minimum to run the sample code:
poi-3.10-FINAL.jarpoi-ooxml-3.10-FINAL.jarcommons-codec-1.5.jarpoi-ooxml-schemas-3.10-FINAL.jarxml-apis-1.0.b2.jarstax-api-1.0.1.jarxmlbeans-2.3.0.jardom4j-1.6.1.jar
Follow this Link to see how to add external jars in eclipse.
WorkbookIt’s the super-interface of all classes that create or maintain Excel workbooks. Following are the two classes that implement this interface
HSSFWorkbookIt implements the Workbook interface and is used for Excel files in .xls format. Listed below are some of the methods and constructors under this class.Methods and ConstructorsHSSFWorkbook()HSSFWorkbook(DirectoryNode directory, boolean preserveNodes)HSSFWorkbook(DirectoryNode directory, POIFSFileSystem fs, boolean preserveNodes)HSSFWorkbook(java.io.InputStream s)HSSFWorkbook(java.io.InputStream s, boolean preserveNodes)HSSFWorkbook(POIFSFileSystem fs)HSSFWorkbook(POIFSFileSystem fs, boolean preserveNodes)where:directory-It is the POI filesystem directory to process from.fs -It is the POI filesystem that contains the workbook stream.preservenodes – This is an optional parameter that decides whether to preserve other nodes like macros. It consumes a lot of memory as it stores all the POIFileSystem in memory (if set).XSSFWorkbookIt is a class that is used to represent both high and low level Excel file formats. It belongs to the org.apache.xssf.usemodel package and implements the Workbook interface. Listed below are the methods and constructors under this class.ClassesXSSFWorkbook()XSSFWorkbook(java.io.File file)XSSFWorkbook(java.io.InputStream is)XSSFWorkbook(java.lang.String path)MethodscreateSheet()createSheet(java.lang.String sheetname)createFont()createCellStyle()createFont()setPrintArea(int sheetIndex, int startColumn, int endColumn, int startRow, int endRow)
HSSFWorkbookIt implements the Workbook interface and is used for Excel files in .xls format. Listed below are some of the methods and constructors under this class.Methods and ConstructorsHSSFWorkbook()HSSFWorkbook(DirectoryNode directory, boolean preserveNodes)HSSFWorkbook(DirectoryNode directory, POIFSFileSystem fs, boolean preserveNodes)HSSFWorkbook(java.io.InputStream s)HSSFWorkbook(java.io.InputStream s, boolean preserveNodes)HSSFWorkbook(POIFSFileSystem fs)HSSFWorkbook(POIFSFileSystem fs, boolean preserveNodes)where:directory-It is the POI filesystem directory to process from.fs -It is the POI filesystem that contains the workbook stream.preservenodes – This is an optional parameter that decides whether to preserve other nodes like macros. It consumes a lot of memory as it stores all the POIFileSystem in memory (if set).
Methods and ConstructorsHSSFWorkbook()HSSFWorkbook(DirectoryNode directory, boolean preserveNodes)HSSFWorkbook(DirectoryNode directory, POIFSFileSystem fs, boolean preserveNodes)HSSFWorkbook(java.io.InputStream s)HSSFWorkbook(java.io.InputStream s, boolean preserveNodes)HSSFWorkbook(POIFSFileSystem fs)HSSFWorkbook(POIFSFileSystem fs, boolean preserveNodes)where:directory-It is the POI filesystem directory to process from.fs -It is the POI filesystem that contains the workbook stream.preservenodes – This is an optional parameter that decides whether to preserve other nodes like macros. It consumes a lot of memory as it stores all the POIFileSystem in memory (if set).
HSSFWorkbook()HSSFWorkbook(DirectoryNode directory, boolean preserveNodes)HSSFWorkbook(DirectoryNode directory, POIFSFileSystem fs, boolean preserveNodes)HSSFWorkbook(java.io.InputStream s)HSSFWorkbook(java.io.InputStream s, boolean preserveNodes)HSSFWorkbook(POIFSFileSystem fs)HSSFWorkbook(POIFSFileSystem fs, boolean preserveNodes)
where:directory-It is the POI filesystem directory to process from.fs -It is the POI filesystem that contains the workbook stream.preservenodes – This is an optional parameter that decides whether to preserve other nodes like macros. It consumes a lot of memory as it stores all the POIFileSystem in memory (if set).
XSSFWorkbookIt is a class that is used to represent both high and low level Excel file formats. It belongs to the org.apache.xssf.usemodel package and implements the Workbook interface. Listed below are the methods and constructors under this class.ClassesXSSFWorkbook()XSSFWorkbook(java.io.File file)XSSFWorkbook(java.io.InputStream is)XSSFWorkbook(java.lang.String path)MethodscreateSheet()createSheet(java.lang.String sheetname)createFont()createCellStyle()createFont()setPrintArea(int sheetIndex, int startColumn, int endColumn, int startRow, int endRow)
ClassesXSSFWorkbook()XSSFWorkbook(java.io.File file)XSSFWorkbook(java.io.InputStream is)XSSFWorkbook(java.lang.String path)
XSSFWorkbook()XSSFWorkbook(java.io.File file)XSSFWorkbook(java.io.InputStream is)XSSFWorkbook(java.lang.String path)
MethodscreateSheet()createSheet(java.lang.String sheetname)createFont()createCellStyle()createFont()setPrintArea(int sheetIndex, int startColumn, int endColumn, int startRow, int endRow)
createSheet()createSheet(java.lang.String sheetname)createFont()createCellStyle()createFont()setPrintArea(int sheetIndex, int startColumn, int endColumn, int startRow, int endRow)
It’s suitable for large files and use less memoryThe main advantage of apache poi is that it’s support both HSSFWorkbook and XSSFWorkbook.It’s contain HSSF implementation of excel file format
It’s suitable for large files and use less memory
The main advantage of apache poi is that it’s support both HSSFWorkbook and XSSFWorkbook.
It’s contain HSSF implementation of excel file format
kk773572498
nandinigujral
Articles
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.