Read data from Excel using Java

In this example, we first load the Excel file called “example.xlsx” using the File class. Then we create a Workbook object using the create method of the WorkbookFactory class.

We select the first sheet in the workbook using the getSheetAt method of the Workbook class.

We read the data from cell A1 using the getRow and getCell methods of the Sheet class. The getStringCellValue method returns the value of the cell as a string.

We do the same thing for cell A2, and finally, we print the values of both cells using the System.out.println method. Note that you can modify the code to read data from any other cell by changing the row and column indices in the getRow and getCell methods.

import java.io.File;
import java.io.IOException;
import org.apache.poi.ss.usermodel.*;

public class ReadExcelFile {
    public static void main(String[] args) {
        // Load the Excel file
        File file = new File("example.xlsx");
        Workbook workbook = null;
        try {
            workbook = WorkbookFactory.create(file);
        } catch (IOException e) {
            e.printStackTrace();
        }

        // Select the sheet you want to read
        Sheet sheet = workbook.getSheetAt(0);

        // Read data from cell A1
        Cell cell_a1 = sheet.getRow(0).getCell(0);
        String cell_a1_value = cell_a1.getStringCellValue();
        System.out.println(cell_a1_value);

        // Read data from cell A2
        Cell cell_a2 = sheet.getRow(1).getCell(0);
        String cell_a2_value = cell_a2.getStringCellValue();
        System.out.println(cell_a2_value);
    }
}

Pamai Tech
Turning ideas into Reality

Products

Office Add-in

Enterprise Solutions

Cloud Consulting

UI UX Design

Data Transformation

Services

FAQ's

Privacy Policy

Terms & Condition

Team

Contact Us

Company

About Us

Services

Features

Our Pricing

Latest News

© 2025 Pamai Tech