以下是我的要求。
3.我能够打印包括标题在内的所有值。但我想将标题作为键和值存储在哈希映射中,并希望将这些值(通过调用该键)用于我的函数。
请有人帮助我实现此目的。我附上了我的代码。它逐行打印所有值,包括标题。
Output:
Studentname
studentno
Registration no
john
1234
7654
如果我调用map.get(studentname),我需要分别得到john。请有人帮助我:(
public static void ReadExcelData(String fileName, String sheetName, String Testcasename) throws IOException {
String dataFile = resourceLocation + pointerData.getProperty("ENVIRONMENT") + "\\" + fileName;
File file = new File(dataFile);
FileInputStream inputStream = new FileInputStream(file);
Workbook workbook = new XSSFWorkbook(inputStream);
try {
// Read sheet inside the workbook by its name
org.apache.poi.ss.usermodel.Sheet sh = workbook.getSheet(sheetName);
int totalNoOfRows = sh.getLastRowNum() - sh.getFirstRowNum();
Row headerrow = sh.getRow(0);
HashMap<String, String> map = new HashMap<String, String>();
for (int row = 1; row < totalNoOfRows; row++) {
Row row1 = sh.getRow(row);
if ((row1.getCell(0).getStringCellValue()).equals(Testcasename)) {
for (int col = 1; col < row1.getLastCellNum(); col++) {
map.put(headerrow.getCell(col).getStringCellValue(), row1.getCell(col).getStringCellValue());
}
}
System.out.println(map);// TODO Auto-generated method stub
}
} catch (Exception e) {
e.printStackTrace();
}
}