Apache POI (Poor Obfuscation Implementation File System) is the Java API for Microsoft Documents such as XLS,XLSX,DOC and PPT as well.
HSSF (Horrible Spreadsheet Format)
Read or write an Excel file format – XLS
XSSF (XML Spreadhsheet Format)
Read or write an Excel file format – XLSX
To download the Apache POI Library access the below link -> https://poi.apache.org/download.html
Download the Binary Distribution
- Linux : poi-bin-xx.tar.gz
- Windows : poi-bin-xx.zip
Writing the XLSX in JAVA project
1. Create JAVA Project
2. Add the dependencies of the Apache POI to the Build Path of the project.
3. Create a XLSX file with rows and columns
4. Write the logic to write the data to the XLSX.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
package total.qa.apache.poi.readxlsx; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import org.apache.poi.ss.usermodel.CellType; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; /** * Write XLSX using Apache POI API * @author qa * */ public class WriteXLSX { public static void main(String args[]) throws IOException { File f = new File("TestCaseData.xlsx"); FileInputStream fis = new FileInputStream(f); XSSFWorkbook excelWorkbook = new XSSFWorkbook(fis); XSSFSheet excelSheet = excelWorkbook.getSheetAt(0); XSSFCell cell=excelSheet.getRow(0).createCell(2);//row index - 0,column index-2 cell.setCellType(CellType.STRING); cell.setCellValue("Pass"); FileOutputStream fos = new FileOutputStream("TestCaseData.xlsx"); excelWorkbook.write(fos); fos.close(); fis.close(); } } |
Writing the XLSX in maven project
1. Create MAVEN Project
2. Add the following dependencies in the MAVEN pom.xml
https://mvnrepository.com/artifact/org.apache.poi/poi
https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml
3. Write the logic to write the data to the XLSX.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
package total.qa.apache.poi.readxlsx; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import org.apache.poi.ss.usermodel.CellType; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; /** * Write XLSX using Apache POI API * @author qa * */ public class WriteXLSX { public static void main(String args[]) throws IOException { File f = new File("TestCaseData.xlsx"); FileInputStream fis = new FileInputStream(f); XSSFWorkbook excelWorkbook = new XSSFWorkbook(fis); XSSFSheet excelSheet = excelWorkbook.getSheetAt(0); XSSFCell cell=excelSheet.getRow(0).createCell(2);//row index - 0,column index-2 cell.setCellType(CellType.STRING); cell.setCellValue("Pass"); FileOutputStream fos = new FileOutputStream("TestCaseData.xlsx"); excelWorkbook.write(fos); fos.close(); fis.close(); } } |
Conclusion:
Reading the XLSX is easy using the Apache POI and Java API.
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
Output