Apache POI is a powerful API that enables the user to create, manipulate, and display various file formats based on Microsoft Office using java programs. Using POI, one should be able to perform create, modify, and display/read operations on the following file formats. For Example, Java doesn’t provide built-in support for working with Excel files, so we need to look for open-source APIs for the job. It is an open-source library developed and distributed by Apache Software Foundation to design or modify Microsoft Office files using Java program. It contains classes and methods to decode the user input data or a file into MS Office documents. Here different styles can be applied like fonts, colors, cell merging, alignments, etc. to a cell in Excel using this concept in the Java program.
Apache POI Architecture: It consists of various components that make an architecture to form a working system:
- POIFS (Poor Obfuscation Implementation File System): This component is the basic factor of all other POI elements. It is used to read different files explicitly.
- HSSF (Horrible Spreadsheet Format): It is used to read and write xls format of MS-Excel files.
- XSSF (XML Spreadsheet Format): It is used for xlsx file format of MS-Excel.
- HPSF (Horrible Property Set Format): It is used to extract property sets of the MS-Office files.
- HWPF (Horrible Word Processor Format): It is used to read and write doc extension files of MS-Word.
- XWPF (XML Word Processor Format): It is used to read and write Docx extension files of MS-Word.
- HSLF (Horrible Slide Layout Format): It is used to read, create, and edit PowerPoint presentations.
- HDGF (Horrible Diagram Format): It contains classes and methods for MS-Visio binary files.
- HPBF (Horrible Publisher Format): It is used to read and write MS-Publisher files.
A Jar file is a Zip archive containing one or multiple java class files. This makes the usage of libraries handier. Directories and Jar files are added to the build path and available to the ClassLoader at runtime to find particular classes inside it. We generally use .jar files to distribute Java applications or libraries, in the form of Java class files and associated metadata and resources (text, images, etc.).
One can say JAR = JavaARchive
- Step 1: Import the necessary .jar files like HSSF, XML and add them to your build path.
- Step 2: Create a workbook using “new XSSFWorkbook()” in which we have to create the spreadsheet or the Excel file using “workbook.createSheet(‘Sheet1’)” in which we are going to apply different styles.
- Step 3: Apply the styles, here merge the cells using the command spreadsheet.addMergedRegion. We have to provide the range addresses of rows and columns as well as its parameter.
- Step 4: Now the next style in determining the cell alignment. For this, we have two commands
- “style1.setAlignment(XSSFCellStyle.ALIGN_LEFT)” for determining the alignment and
- “style1.setVerticalAlignment(XSSFCellStyle.VERTICAL_TOP)” for determining vertical alignment
- Step 5: For applying border to a cell, we can use “setBorderBottom/Left/Top/Right(XSSFCleeName.BorderName)”.
- Step 6: Change the border name in the parameter to go through different border styles.
- Step 7: For filling colors and adding patterns, first set the background color of the cell using “setFillBackgroundColor(HSSFColor.COLOR_NAME.index)”.
- Step 8: Then set the pattern whichever you like by “setFillPattern(XSSFCellStyle.PATTER_NAME)”.
- Step 9: Finally, set the alignment using”setAlignment(XSSFCellStyle.ALIGN_TYPE);
Implementation: Carrying out the above procedural steps over the empty Excel file at the local directory already created.
- Create a Spread Sheet by creating an object of XSSFSheet
- Creating a row in the above XSSFSheet using createRow() method.
- Later on, setting the height of a row
- Creating an object of type XSSFCell and typecasting above row created to it.
- Setting cell values.
- Merging the cells.
- Aligning the cells.
- Justify the alignment.
- Bordering the cells.
- Filling colors in the cells.
- Creating a new file in the local directory by creating the object of FileOutputStream.
- Write to the above workbook created in the initial step.
- Close the connection of the file.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.