How To Read Data From Excel In Java
Last Updated : Mar 11, 2024
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 :-
import org.apache.poi.ss.usermodel.*; import java.io.File; import java.io.FileInputStream; import java.io.IOException; 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"); break; case NUMERIC: System.out.print(cell.getNumericCellValue() + "\t"); break; case BOOLEAN: System.out.print(cell.getBooleanCellValue() + "\t"); break; case BLANK: System.out.print("BLANK\t"); break; default: System.out.print("\t"); } } System.out.println(); } workbook.close(); fis.close(); } catch (IOException e) { e.printStackTrace(); } 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."); } }
- We begin by importing the required classes from the standard Java I/O classes and the org.apache.poi.ss.usermodel package, all of which are a component of the Apache POI library.
- The main method, which is where execution starts, is defined in the ExcelReader class.
- A try-catch block is present inside the main procedure to address any potential exceptions that can arise when handling files.
- 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.
- By providing the excelFilePath to its constructor, which starts a stream to read the Excel file, we build a FileInputStream.
- We make use of FileInputStream for creating a Workbook example by calling WorkbookFactory.create(fis).
- As a result, a Workbook object that represents the Excel file is created.
- We expect that the first page (index 0) from the Workbook will be the sheet that has to be read.
- If you want to read from a different sheet, you can change the index.
- To cycle through each row and column of the chosen sheet, we utilize two stacked loops.
- We utilize a switch statement inside the inner loop to identify the cell type and get the relevant cell value.
- We use a tab delimiter to print the value depending on the kind of cell.
- Each row's cells are printed first, and then a new line is printed to go to the following row.
- We close the Workbook & FileInputStream to free up system resources after processing all the rows and columns.
- 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.