In this example, we will take a look at how to read/parse .xlsand .xlsx Excel files using Apache POI Java Library,
POI Maven Dependency
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.2.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.2</version>
</dependency>
POI Gradle Dependency
implementation group: 'org.apache.poi', name: 'poi', version: '5.2.2'
implementation group: 'org.apache.poi', name: 'poi-ooxml', version: '5.2.2'
Reading/Parsing Excel file with .xlsx or .xls extension
Sample Excel File:
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Iterator;
/**
* Java Program to read a Excel .xlsx or .xls file
*/
public class ExcelReaderPOIExample {
public static void main(String[] args) {
String excelFileName = "/Users/code2care/Desktop/Students-Details.xlsx";
File excelXlsxFile = new File(excelFileName);
try (FileInputStream fileInputStream = new FileInputStream(excelXlsxFile);
XSSFWorkbook workbook = new XSSFWorkbook(fileInputStream)) {
//reading the 1st tab
XSSFSheet spreadsheet = workbook.getSheetAt(0);
for (Row cells : spreadsheet) {
XSSFRow row = (XSSFRow) cells;
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
if (cell.getCellType() == CellType.NUMERIC) {
System.out.print((int) cell.getNumericCellValue() + "\t");
} else if (cell.getCellType() == CellType.STRING) {
System.out.print(cell.getStringCellValue() + "\t");
} else if(cell.getCellType() == CellType.FORMULA) {
System.out.print(cell.getCellFormula() + "\t");
}
}
System.out.println(""); //break the line
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("Error occurred while reading Excel file!");
}
}
}
Output:
Sr.No Student Name City Age
1 Justin Chicago 23
2 Alex Austin 24
3 Brain New York 21
4 Samantha San Jose 24
5 Tiffany Chicago 23
-
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Java,
- Convert Java Map Collection Object to JSON String using Jackson
- Java Stream flatmap() Examples
- [Fix] Instant java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit: Years
- How to run Java Unit Test cases with Apache Maven?
- How to check if Java main thread is alive
- [Fix] java: incompatible types: incompatible parameter types in lambda expression error
- Parsing CSV file using Java code example (Comma Separated File)
- Unhandled exception type InterruptedException : Java Threads
- Native getClass() method from java.lang.Object Class Explained with examples.
- Java Jackson ObjectMapper Class with Examples
- Java 8 Streams map() with examples
- Java 8 - Convert List to Map Examples
- IntelliJ: Error: Could not find or load main class, java.lang.ClassNotFoundException
- Java Stream with Multiple Filters Example
- How to Clear StringJoiner in Java 8
- Spring 5 IoC Example with application Context XML (ClassPathXmlApplicationContext) and Gradle.
- How to get end of line (EOL) or new line character \r \n in Java
- Spring Boot CRUD Examples using JDBCTemplate
- Delete a File in Java with Examples
- Implementing Insertion Sort Algorithm in Java Program
- Java JDBC Batch Update Example with PreparedStatement
- Java JDBC Select Multiple Records from table as List using PreparedStatement
- [Hibernate] The method buildSessionFactory() from the type Configuration is deprecated
- How to fix Java HTTP java.net.UnknownHostException
- Java 8 Display time in 12 hour AM PM format
More Posts:
- How to Get the Relative Path of a file in Python Program - Python
- How to install Apache Maven on Ubuntu - Ubuntu
- Fix: hibernate.InstantiationException: No default constructor for entity: User - Java
- Disable EditText Cursor Android - Android
- Show Hide SharePoint column in List Library form with the conditional formula - SharePoint
- Fix: sudo: unable to open Read-only file system - Linux
- How to generate client id and secret to register SharePoint App with OAuth - SharePoint
- TextEdit alternative for Mac - MacOS