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: Java Code: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,
- Deep Dive into Java 8 Predicate Interface
- Read and Parse XML file using Java DOM Parser [Java Tutorial]
- Java 8 Predicate Functional Interface isEqual() Method Example
- Convert Multidimensional Array toString In Java
- How to read int value using Scanner Class Java
- Spring Boot AI + LLM + Java Code Example
- Write to a File using Java Stream API
- Implementing Bubble Sort Algorithm using Java Program
- How to Fix XmlBeanDefinitionStoreException in Java SpringBoot ApplicationConfig.xml
- YAML Parser using Java Jackson Library Example
- [Fix] java: integer number too large compilation error
- Convert JSON String to Java GSON Object Example
- Read a file using Java 8 Stream
- Java Spring Boot 3 Web Hello World with Gradle in IntelliJ
- Ways Compare Dates in Java Programming with Examples
- Pretty Print JSON String in Java Console Output
- Java JDBC with Join Queries Example
- How to Check For Updates on Windows 11 (Step-by-Step)
- [Fix] java.net.MalformedURLException: unknown protocol
- How to display date and time in GMT Timezone in Java
- Error: LinkageError occurred while loading main class UnsupportedClassVersionError [Eclipse Java]
- How to convert a String to Java 8 Stream of Char?
- RabbitMQ Queue Listener Java Spring Boot Code Example
- 5+ Fibonacci number Series Java Program Examples [ 0 1 1 2 3 ..]
- Handling NullPointerException with Java Predicate
More Posts:
- Fix: Xbox Error Code: 0x80190190 - Microsoft
- Hide cURL Outputs and Errors on Terminal - cURL
- Android Toolbar example with appcompat_v7 21 - Android
- Parsing CSV file using Java code example (Comma Separated File) - Java
- How to export bookmarks from Google Chrome Browser - Chrome
- 10 FTP SFTP Clients and Alternatives - FTP
- Android : IOException: Unable to open sync connection! - Android
- How to enable more encoding support in Mac Terminal - MacOS