Java Read Excel Workbook from InputStream using Apache POI

Tags: Apache POI Excel InputStream

In this Java Apache POI tutorial we learn how to use the WorkbookFactory class of Apache POI library to read an Excel Workbook from a given java.io.InputStream object.

Table of contents

  1. Add Apache POI dependencies to Java project
  2. How to read Excel Workbook from InputStream in Java
  3. Example Java Program to Read Excel Workbook from InputStream

Add Apache POI dependencies to Java project

If you use Gradle build project, add the following dependencies to the build.gradle file.

implementation group: 'org.apache.poi', name: 'poi', version: '5.2.2'
implementation group: 'org.apache.poi', name: 'poi-ooxml', version: '5.2.2'

If you use Maven build project, add the following dependencies to the pom.xml file.

<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>

How to read Excel Workbook from InputStream in Java

With a given InputStream object, we can use the WorkbookFactory.create(InputStream inp) static method to create an appropriate HSSFWorkbook / XSSFWorkbook object from a given InputStream as Java code below.

String fileName = "contacts.xlsx";
try (InputStream inputStream = new FileInputStream(fileName)) {
    try(Workbook workbook = WorkbookFactory.create(inputStream)) {
			// Read Excel file content
    } catch (IOException e) {
    }
} catch (IOException e) {
}

Example Java Program to Read Excel Workbook from InputStream

For example, we have an Excel file named contacts.xlsx with data as the screenshot below.

Java Read Excel Workbook from InputStream using Apache POI

In the following example Java program, we show how use the WorkbookFactory.create(InputStream inp) method to get the Excel Workbook and read Excel file data.

ReadExcelWorkbookFromFileExample1.java

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;

public class ReadExcelWorkbookFromInputStreamExample1 {
    public static void main(String... args) {
        String fileName = "contacts.xlsx";

        try (InputStream inputStream = new FileInputStream(fileName)) {
            try(Workbook workbook = WorkbookFactory.create(inputStream)) {
                Sheet sheet = workbook.getSheetAt(0);
                Iterator<Row> rowIterator = sheet.rowIterator();
                while (rowIterator.hasNext()) {
                    Row row = rowIterator.next();
                    Iterator<Cell> cellIterator = row.cellIterator();
                    while(cellIterator.hasNext()) {
                        Cell cell = cellIterator.next();
                        String cellData = cell.getStringCellValue();
                        System.out.print(cellData + "\t");
                    }
                    System.out.println();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
The output as below.
First Name	Last Name	Email	
Rayhan	Harmon	rayhan@simplesolution.dev	
Paddy	Sheridan	paddy@simplesolution.dev	
Clara	Callaghan	clara@simplesolution.dev	
Josephine	Amos	josephine@simplesolution.dev	
Sheikh	Tucker	sheikh@simplesolution.dev

Happy Coding 😊

Java Read Excel Workbook from File using Apache POI

Java Read Excel File using Apache POI

Java Create Excel File .xlsx using Apache POI

Java Apache POI Tutorial

Spring Boot Download Excel File Export from MySQL Database

Spring Boot Web Application Download Excel File

Java Read Password Protected Excel File using Apache POI

Java How to Iterate over Sheets Rows and Cells of Excel file using Apache POI

Java Add Rows to Existing Excel File using Apache POI

Java Add Sheet to Existing Excel File using Apache POI

Java Remove Sheet from Existing Excel File using Apache POI

Java Create Formula Excel Cells using Apache POI