All TalkersCode Topics

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

How To Read XLSX File In Java

Last Updated : Mar 11, 2024

How To Read XLSX File In Java

In this article we will show you the solution of how to read XLSX file in java, for many applications, reading Excel files (XLSX) with Java is a typical requirement. For storing spreadsheet data, Microsoft Excel's XLSX format is extensively used.

Java offers a number of modules and APIs that make it easier to read XLSX files to carry out this task.

One well-known library for processing Microsoft Office documents is Apache POI, which provides a collection of Java APIs.

Developers may easily read XLSX files by gaining access to the workbook, sheets, and specific cells using Apache POI.

This library offers classes and methods that make it easier to extract data, making it possible to integrate it into Java programs with ease. Now we'll talk about the idea of reading XLSX files in Java.

Step By Step Guide On How To Read XLSX File In Java :-

The fundamental actions for reading an XLSX file in Java include opening the file, gaining access to the desired sheet, iterating through the columns and rows, and extracting the necessary data.

Developers may quickly parse XLSX files and conduct several operations on the data, such as data analysis, reporting, or data migration, by utilising the features of Apache POI or comparable libraries.

public class XLSXReader {
    public static void main(String[] args) {
        String filePath = "path/to/your/file.xlsx";
        try (FileInputStream fis = new FileInputStream(new File(filePath));
             Workbook workbook = WorkbookFactory.create(fis)) {
            Sheet sheet = workbook.getSheetAt(0); // Accessing the first sheet
            for (Row row : sheet) {
                for (Cell cell : row) {
                    CellType cellType = cell.getCellType();
                    switch (cellType) {
                        case STRING:
                            System.out.print(cell.getStringCellValue() + "\t");
                        case NUMERIC:
                            System.out.print(cell.getNumericCellValue() + "\t");
                        case BOOLEAN:
                            System.out.print(cell.getBooleanCellValue() + "\t");
        } catch (IOException e) {
  1. We import the required classes and interfaces from the Java IO classes (,, and as well as the* Apache POI library.
  2. A public class called XLSXReader is defined.
  3. The main method serves as our program's entrance point.
  4. With the path to the Excel file that has to be read, we initialize the string variable filePath. The real path to your Excel file must be used in place of "path/to/your/file.xlsx".
  5. We define a FileInputStream to read the Excel file indicated by filePath inside a try-with-resources block. The WorkbookFactory is then used to build a Workbook object.FileInputStream is passed to the create method.
  6. Using the getSheetAt(0) method, we can retrieve the first sheet of the workbook and assign it to the sheet variable in a Sheet.
  7. To go through each row and cell in the sheet, we utilise stacked loops.
  8. We obtain the cell type for each cell using the getCellType method, then we assign it to the cellType CellType variable.
  9. To handle several cell kinds, a switch statement is employed.
  10. If the cell type is STRING, we use cell to print the cell's string value.getStringCellValue().
  11. If the cell type is NUMERIC, we use the cell function to print the numeric value of the cell.getNumericCellValue().
  12. If the cell type is BOOLEAN, we use cell to print the cell's boolean value.getBooleanCellValue().
  13. To preserve the tabular format, we print a tab character if the cell type is not any of the ones listed above.
  14. We print a new line character to advance to the next row after printing all the cells in a row.
  15. During file reading, if an IOException happens, we catch it and print the stack trace.
  16. The try-catch block marks the end of the program's execution.

Conclusion :-

As a result, we have successfully learnt the Java notion of reading XLSX files.

We also discovered that libraries like Apache POI may be used to read XLSX files in Java.

Opening the file, selecting the desired sheet, iterating through the columns and rows, and extracting the required data are the steps in the procedure.

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

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪