I need to read an Excel file that contains 3 sheets and I need to represent the data as follows: each row spans across all 3 sheets, for example
row1 = sheet[0].getRow(1) + sheet[1].getRow(1) + sheet[2].getRow(1)
Below is what I have done so far but as you can see, the row is defined per sheet, which is wrong, I need this to get data from the other sheets as well. How can this be elegantly done ?
LinkedHashMap<String, List<LinkedHashMap<String, Object>>> records =
new LinkedHashMap<String, List<LinkedHashMap<String, Object>>>();
for (int sIndex = 0; sIndex < wb.getNumberOfSheets(); sIndex++) {
// System.out.println(sIndex);
Sheet sheet = wb.getSheetAt(sIndex);
String key;
String value = null;
List<LinkedHashMap<String, Object>> sheetRecords = new ArrayList<LinkedHashMap<String, Object>>();
for (Row row : sheet) {
LinkedHashMap<String, Object> record = new LinkedHashMap<>();
for (Cell cell : row) {
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
key = sheet.getRow(sheet.getFirstRowNum())
.getCell(cell.getColumnIndex()).toString();
value = cell.getStringCellValue();
record.put(key, value);
sheetRecords.add(record);
break;
case Cell.CELL_TYPE_NUMERIC:
key = sheet.getRow(sheet.getFirstRowNum())
.getCell(cell.getColumnIndex()).toString();
double number = cell.getNumericCellValue();
record.put(key, number);
sheetRecords.add(record);
case Cell.CELL_TYPE_BLANK:
break;
default:
break;
}
}
}
records.put(sheet.getSheetName(), sheetRecords);
}
You’ll have to use the outer for-loop to iterate the rows by index. In pseudo code: