public static ConcurrentHashMap<String, ConcurrentHashMap<String, String>> dataMap = new ConcurrentHashMap<>();
public static ConcurrentHashMap<String, String> cellValues = null;
public static void main(String[] args) {
getData();
for (String key : dataMap.keySet()) {
System.out.println(dataMap.get(key));
}
}
private static Object getCellValue(Cell cell) {
switch (cell.getCellType()) {
case STRING:
return cell.getStringCellValue();
case NUMERIC:
return cell.getNumericCellValue();
case BOOLEAN:
return cell.getBooleanCellValue();
default:
break;
}
return null;
}
public static void getData() {
try {
FileInputStream fis = new FileInputStream("Data/ServiceTesting.xlsx");
XSSFWorkbook workbook = new XSSFWorkbook(fis);
XSSFSheet sheet = workbook.getSheetAt(0);
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
Iterator<Cell> cellIterator = row.cellIterator();
String key = row.getCell(0).toString();
cellValues = new ConcurrentHashMap<>();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
if (cell.getRowIndex() != 0) {
String header = sheet.getRow(0).getCell(cell.getColumnIndex()).getStringCellValue();
String clellValue = (String) getCellValue(cell);
cellValues.put(header, clellValue);
}
}
if (row.getRowNum() != 0) {
dataMap.put(key, cellValues);
}
}
workbook.close();
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
}
Leave a comment