I'm trying to fetch data from excel sheet using Apache poi jars. I have three rows and four columns in my excel. The actual data is in 2nd and 3rd row. 1st row is headers which I'm using as key for fetching the values. While I do this, the output I get always shows the 3rd row data. I have tried debugging it and observed that 2nd row data is getting overridden by 3rd row.
Here is my code snippet
public static void main(String[] args) throws IOException {
loadExcel();
Map<String, Map<String, String>> superMap = new HashMap<String, Map<String,String>>();
Map<String, String> hm = new HashMap<>();
for (int i = 1; i < excelSheet.getLastRowNum() + 1; i++) {
int lastColnum = excelSheet.getRow(0).getLastCellNum();
for (int j = 0; j < lastColnum; j++) {
System.out.println("reading excel column");
String key = excelSheet.getRow(0).getCell(j).getStringCellValue();
String value = excelSheet.getRow(i).getCell(j).getStringCellValue();
hm.put(key, value);
}
superMap.put("MASTERDATA", hm);
}
for (Entry<String, Map<String, String>> set : superMap.entrySet()) {
System.out.println(set.getKey() + " : " + set.getValue());
}
}
Output:
MASTERDATA : {Blood Group=test2bg, UserName=test2, Address=test2add, Password=test2pass}
My Data in excel:
Username | Password | Address | Blood Group
test1 | test1pass |test1add | test1bg
test2 | test2passs|test2add | test2bg
I have tried putting the map into a superMap, but still no luck. Any suggestions / advise would help. Thanks in advance.