Created
January 17, 2024 09:30
-
-
Save conholdate-gists/4c8d5b40c98469c8606f9e9330a200a8 to your computer and use it in GitHub Desktop.
Read Excel File in Java | Excel Spreadsheet and Worksheet Reader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Load Excel file | |
Workbook wb = new Workbook("workbook.xlsx"); | |
// Get all worksheets | |
WorksheetCollection collection = wb.getWorksheets(); | |
// Loop through all the worksheets | |
for (int worksheetIndex = 0; worksheetIndex < collection.getCount(); worksheetIndex++) { | |
// Get worksheet using its index | |
Worksheet worksheet = collection.get(worksheetIndex); | |
// Print worksheet name | |
System.out.print("Worksheet: " + worksheet.getName()); | |
// Get number of rows and columns | |
int rows = worksheet.getCells().getMaxDataRow(); | |
int cols = worksheet.getCells().getMaxDataColumn(); | |
// Loop through rows | |
for (int i = 0; i < rows; i++) { | |
// Loop through each column in selected row | |
for (int j = 0; j < cols; j++) { | |
// Pring cell value | |
System.out.print(worksheet.getCells().get(i, j).getValue() + " | "); | |
} | |
// Print line break | |
System.out.println(" "); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Load Excel file | |
Workbook wb = new Workbook("workbook.xlsx"); | |
// Get reference of the worksheet | |
Worksheet worksheet = wb.getWorksheets().get(0); | |
// Get row and column count | |
int rows = worksheet.getCells().getMaxDataRow(); | |
int cols = worksheet.getCells().getMaxDataColumn(); | |
// Loop through rows | |
for (int i = 0; i < rows; i++) { | |
// Loop through each column in selected row | |
for (int j = 0; j < cols; j++) { | |
// Print cell value | |
System.out.print(worksheet.getCells().get(i, j).getValue() + " | "); | |
} | |
// Print line break | |
System.out.println(" "); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment