How to Read Excel File in JavaIn 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 POIInterfaces
ClassesXLS Classes
XLSX Classes
Steps to read data from XLS fileStep 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 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 FileAll 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 Next TopicJava Tutorial |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India