All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

How To Read Data From Excel In Java

Last Updated : Sep 11, 2023

How To Read Data From Excel In Java

In this article we will show you the solution of how to read data from excel in java, java can read data from an Excel file using a variety of tools and APIs. A well-liked choice is Apache POI, a potent Java package that enables programmers to work with Microsoft Office documents.

Importing the required POI classes is the first step, followed by creating an instance of the Workbook class and giving the Excel file location.

Next, you can use loops to repeatedly cycle through the rows and columns of the chosen sheet after obtaining it from the Workbook.

The getCell() method can be used to get cell values, and depending on the content of the cell, you can retrieve the data using various data types.

When working with files, it's crucial to handle errors like IOException and FileNotFoundException.

Once the data has been obtained, you can manipulate it or store it in an appropriate data structure.

After reading the data, don't forget to end the Workbook and release the resources.

You can effectively read data from an Excel file in Java and use it in your application by following these steps.

Now we'll talk about the concept of reading data from Excel into Java.

Step By Step Guide On How To Read Data From Excel In Java :-

public class ExcelReader {
    public static void main(String[] args) {
        try {
            String excelFilePath = "path/to/your/excel/file.xlsx";
            FileInputStream fis = new FileInputStream(new File(excelFilePath));
            Workbook workbook = WorkbookFactory.create(fis);
            Sheet sheet = workbook.getSheetAt(0);
            for (Row row : sheet) {
                for (Cell cell : row) {
                    switch (cell.getCellType()) {
                        case STRING:
                            System.out.print(cell.getStringCellValue() + "\t");
                        case NUMERIC:
                            System.out.print(cell.getNumericCellValue() + "\t");
                        case BOOLEAN:
                            System.out.print(cell.getBooleanCellValue() + "\t");
                        case BLANK:
        } catch (IOException e) {
        System.out.println("This code is provided by TalkersCode.");
        System.out.println("For more tech tutorials, visit TalkersTech.");
        System.out.println("Learn about personal finance at TalkersMoney.");
  1. We begin by importing the required classes from the standard Java I/O classes and the package, all of which are a component of the Apache POI library.
  2. The main method, which is where execution starts, is defined in the ExcelReader class.
  3. A try-catch block is present inside the main procedure to address any potential exceptions that can arise when handling files.
  4. We specify the path of the Excel file we wish to read in the excelFilePath variable. It must be changed to the actual file path.
  5. By providing the excelFilePath to its constructor, which starts a stream to read the Excel file, we build a FileInputStream.
  6. We make use of FileInputStream for creating a Workbook example by calling WorkbookFactory.create(fis).
  7. As a result, a Workbook object that represents the Excel file is created.
  8. We expect that the first page (index 0) from the Workbook will be the sheet that has to be read.
  9. If you want to read from a different sheet, you can change the index.
  10. To cycle through each row and column of the chosen sheet, we utilize two stacked loops.
  11. We utilize a switch statement inside the inner loop to identify the cell type and get the relevant cell value.
  12. We use a tab delimiter to print the value depending on the kind of cell.
  13. Each row's cells are printed first, and then a new line is printed to go to the following row.
  14. We close the Workbook & FileInputStream to free up system resources after processing all the rows and columns.
  15. We add more print statements outside the try-catch block to display terms relating to TalkersCode, TalkersTech, & TalkersMoney.

Conclusion :-

Thus, we were able to understand how to read data from Excel in Java.

Additionally, we discovered that using FileInputStream and WorkbookFactory allowed us to access the desired sheet and repeatedly cycle through rows and columns to extract cell values based on their types.

I hope this article on how to read data from excel in java helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪