How to Read Excel File in Java
In this section, we are going to learn how we can read data from an excel file.
In Java, reading excel file is not similar to read word file because of cells in excel file. JDK does not provide direct API to read or write Microsoft Excel or Word document. We have to rely on the third-party library that is Apache POI.
What is Apache POI?
Apache POI (Poor Obfuscation Implementation) is a Java API for reading and writing Microsoft Documents in both formats .xls and .xlsx. It contains classes and interfaces. The Apache POI library provides two implementations for reading excel files:
- HSSF (Horrible SpreadSheet Format) Implementation: It denotes an API that is working with Excel 2003 or earlier versions.
- XSSF (XML SpreadSheet Format) Implementation: It denotes an API that is working with Excel 2007 or later versions.
Interfaces and Classes in Apache POI
Interfaces
- Workbook: It represents an Excel Workbook. It is an interface implement by HSSFWorkbook and XSSFWorkbook.
- Sheet: It is an interface that represents an Excel worksheet. A sheet is a central structure of a workbook, which represents a grid of cells. The Sheet interface extends java.lang.Iterable.
- Row: It is also an interface that represents the row of the spreadsheet. The Row interface extends java.lang.Iterable. There are two concrete classes: HSSFRow and XSSFRow.
- Cell: It is an interface. It is a high-level representation of a cell in a row of the spreadsheet. HSSFCell and XSSFCell implement Cell interface.
Classes
XLS Classes
- HSSFWorkbook: It is a class representing the XLS file.
- HSSFSheet: It is a class representing the sheet in an XLS file.
- HSSFRow: It is a class representing a row in the sheet of XLS file.
- HSSFCell: It is a class representing a cell in a row of XLS file.
XLSX Classes
- XSSFWorkbook: It is a class representing the XLSX file.
- XSSFSheet: It is a class representing the sheet in an XLSX file.
- XSSFRow: It is a class representing a row in the sheet of XLSX file.
- XSSFCell: It is a class representing a cell in a row of XLSX file.
Steps to read data from XLS file
Step 1: Create a simple Java project in eclipse.
Step 2: Now, create a lib folder in the project.
Step 3: Download and add the following jar files in the lib folder:
- commons-collections4-4.1.jar Click Here
- poi-3.17.jar Click Here
- poi-ooxml-3.17.jar Click Here
- poi-ooxml-schemas-3.17.jar Click Here
- xmlbeans-2.6.0.jar Click Here
Step 4: Set the Class Path:
Right-click on the project ->Build Path ->Add External JARs -> select all the above jar files -> Apply and close.
Step 5: Now create a class file with the name ReadExcelFileDemo and write the following code in the file.
Step 6: Create an excel file with the name “student.xls” and write some data into it.
Step 7: Save and run the program.
Example of reading excel file (.xls) file
Output:
Name Age Height Swarit 23.0 5" Puneet 25.0 6'1" Swastik 22.0 5'5" Tejas 12.0 4'9"
Reading XLSX File
All steps will remain same except file format.
Table: employee.xslx
Example of read excel file (.xlsx)
In this example we use XSSFWorkbook class.
Output:
Employee ID Employee Name Salary Designation Department 1223.0 Harsh 20000.0 Marketing Manager Marketing 3213.0 Vivek 15000.0 Financial Advisor Finance 6542.0 Krishna 21000.0 HR Manager HR 9213.0 Sarika 34000.0 Sales Manager Sales
Reading a particular cell value from a excel file (.xlsx)
Table: EmployeeData.xlsx
Example
In the following example, we read the value of the 2nd row and the 2nd column. The row and column counting start from 0. So the program returns “Software Engineer.”
Output:
Software Engineer