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.
import org.apache.poi.ss.usermodel.*; import java.io.File; import java.io.FileInputStream; import java.io.IOException; 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"); break; case NUMERIC: System.out.print(cell.getNumericCellValue() + "\t"); break; case BOOLEAN: System.out.print(cell.getBooleanCellValue() + "\t"); break; default: System.out.print("\t"); break; } } System.out.println(); } } catch (IOException e) { e.printStackTrace(); } } }
- We import the required classes and interfaces from the Java IO classes (java.io.File, java.io.FileInputStream, and java.io.IOException) as well as the org.apache.poi.ss.usermodel.* Apache POI library.
- A public class called XLSXReader is defined.
- The main method serves as our program's entrance point.
- 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".
- 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.
- Using the getSheetAt(0) method, we can retrieve the first sheet of the workbook and assign it to the sheet variable in a Sheet.
- To go through each row and cell in the sheet, we utilise stacked loops.
- We obtain the cell type for each cell using the getCellType method, then we assign it to the cellType CellType variable.
- To handle several cell kinds, a switch statement is employed.
- If the cell type is STRING, we use cell to print the cell's string value.getStringCellValue().
- If the cell type is NUMERIC, we use the cell function to print the numeric value of the cell.getNumericCellValue().
- If the cell type is BOOLEAN, we use cell to print the cell's boolean value.getBooleanCellValue().
- To preserve the tabular format, we print a tab character if the cell type is not any of the ones listed above.
- We print a new line character to advance to the next row after printing all the cells in a row.
- During file reading, if an IOException happens, we catch it and print the stack trace.
- 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.