Skip to content

Instantly share code, notes, and snippets.

@conholdate-gists
Created January 17, 2024 09:30
Show Gist options
  • Save conholdate-gists/4c8d5b40c98469c8606f9e9330a200a8 to your computer and use it in GitHub Desktop.
Save conholdate-gists/4c8d5b40c98469c8606f9e9330a200a8 to your computer and use it in GitHub Desktop.
Read Excel File in Java | Excel Spreadsheet and Worksheet Reader
// 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(" ");
}
}
// 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