Java 需要从excel中提取值并将值存储到Hashmap中
以下是我的要求Java 需要从excel中提取值并将值存储到Hashmap中,java,selenium,selenium-webdriver,data-structures,hashmap,Java,Selenium,Selenium Webdriver,Data Structures,Hashmap,以下是我的要求 我想从excel中读取值 我的excel有如下标题和值 3.我能够打印所有值,包括标题。但我想在哈希映射中将头存储为键,将值存储为值,并希望将这些值(通过调用该键)用于我的函数中 请有人帮我实现这一点。我已附上我的代码。它逐行打印所有值,包括标题 Output: Studentname studentno Registration no john 1234 7654 如果我调用map.get(studentname),我需要分别找到john。请有人帮帮我:
Output:
Studentname
studentno
Registration no
john
1234
7654
如果我调用map.get(studentname),我需要分别找到john。请有人帮帮我:(
public static void ReadExcelData(字符串文件名、字符串sheetName、字符串Testcasename)引发IOException异常{
String dataFile=resourceLocation+pointerData.getProperty(“环境”)+“\\\”+文件名;
文件文件=新文件(数据文件);
FileInputStream inputStream=新的FileInputStream(文件);
工作簿=新XSSF工作簿(inputStream);
试一试{
//按工作表的名称阅读工作表
org.apache.poi.ss.usermodel.Sheet sh=workbook.getSheet(sheetName);
int totalNoOfRows=sh.getLastRowNum()-sh.getFirstRowNum();
Row headerrow=sh.getRow(0);
HashMap=newHashMap();
for(int row=1;row
什么不起作用?我的代码正在逐行打印值和标题。我想知道,标题是否存储在键中?值是否也存储在hashmap的值中?…我还想将此hashmap作为结果返回给我的函数。我想在那里使用这些值(通过调用该键)。您能发布一个格式化的输入示例吗?HashMap map=newhashmap();map.put(“1”,“abc”);map.put(“2”,“def”);System.out.println(map);System.out.println(map.get(“1”);
打印两行。第一行是{1=abc,2=def}
第二行是abc
可能您的关键是您的标题名您是只有一组数据还是要处理多组数据?
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();
}
}