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:
Interfaces and Classes in Apache POI
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:
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
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.
Example of read excel file (.xlsx)
In this example we use XSSFWorkbook class.
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)
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."
Next TopicJava Tutorial
JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week