Java Read Password Protected Excel File using Apache POI

Tags: Apache POI Excel

In this Java Apache POI tutorial we learn how to read password protected Excel file using the WorkbookFactory class of Apache POI library.

Table of contents

  1. Add Apache POI dependencies to Java project
  2. Example Password Protected Excel File
  3. How to Read Password Protected Excel File in Java

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>

Example Password Protected Excel File

For example, we have a password protected Excel file located at D:\SimpleSolution\Data\Customers.xlsx and it has the password is ‘simplesolution’

Open this Excel file we have the file content as screenshots below.

Java Read Password Protected Excel File using Apache POI

Java Read Password Protected Excel File using Apache POI

How to Read Password Protected Excel File in Java

With Apache POI library, we can use the WorkbookFactory.create(File file, String password) method to read password protected Excel file and return appropriate HSSFWorkbook / XSSFWorkbook object as Java code below.

String fileName = "D:\\SimpleSolution\\Data\\Customers.xlsx";
String password = "simplesolution";
File excelFile = new File(fileName);

// Read password protected Excel file
try(Workbook workbook = WorkbookFactory.create(excelFile, password)) {
	// Read Excel file content
} catch (IOException e) {
}

In the following example Java application, we show to use the Apache POI WorkbookFactory class to read the password protected Excel file and print its content to the standard output console.

ReadPasswordProtectedExcelFileExample1.java

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.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;

public class ReadPasswordProtectedExcelFileExample1 {
    public static void main(String... args) {
        String fileName = "D:\\SimpleSolution\\Data\\Customers.xlsx";
        String password = "simplesolution";
        File excelFile = new File(fileName);

        // Read password protected Excel file
        try(Workbook workbook = WorkbookFactory.create(excelFile, password)) {
            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();
                    CellType cellType = cell.getCellType();
                    if(cellType == CellType.STRING) {
                        System.out.print(cell.getStringCellValue() + "\t");
                    } else if(cellType == CellType.NUMERIC) {
                        System.out.print(cell.getNumericCellValue() + "\t");
                    }
                }
                System.out.println();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
The output as below.
First Name	Last Name	Age	
Candice	Garcia	30.0	
Corban	Berg	23.0	
Joshua	Lawson	42.0	
Izabela	Moreno	27.0	
Aleisha	Zuniga	53.0

Happy Coding 😊

Java How to Iterate over Sheets Rows and Cells of Excel file 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 Excel File using Apache POI

Java Read Excel Workbook from File using Apache POI

Java Read Excel Workbook from InputStream using Apache POI

Java Create Excel File .xlsx 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