如何将excel中的值存储到java中的某些集合

我有一个excel文件,如下所示,

**Method Name** **Status Code** **user** **password** getLoggedinUserDetails 400 anto test createRequisition 400 mayank hexgen excelMDM 400 xxxxx hexgen createOrder 400 yyyyy hexgen confirmOrder 400 zzzzz hexgen 

我想在一些collection保存上述细节,以便我可以details by providing username and get the details like Method Name, Status code,and password.访问details by providing username and get the details like Method Name, Status code,and password.

我试过这样的东西,只能存储method name and status code

 package com.hexgen.tools; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.Map; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; public class TestMethodsDetails { public Map getKnownGoodMap(String filePath) { String key = ""; int value = 0; //filePath="TestMethodDetails.xls"; Map knownGoodMap = new LinkedHashMap(); try { FileInputStream file = new FileInputStream(new File(filePath)); // Get the workbook instance for XLS file HSSFWorkbook workbook = new HSSFWorkbook(file); // Get first sheet from the workbook HSSFSheet sheet = workbook.getSheetAt(0); // Iterate through each rows from first sheet Iterator rowIterator = sheet.iterator(); while (rowIterator.hasNext()) { Row row = rowIterator.next(); // For each row, iterate through each columns Iterator cellIterator = row.cellIterator(); while (cellIterator.hasNext()) { Cell cell = cellIterator.next(); switch (cell.getCellType()) { case Cell.CELL_TYPE_NUMERIC: value = (int) cell.getNumericCellValue(); break; case Cell.CELL_TYPE_STRING: key = cell.getStringCellValue(); break; } if (key != null && value != Integer.MIN_VALUE) { knownGoodMap.put(key, value); key = null; value = Integer.MIN_VALUE; } } } file.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return knownGoodMap; } public static void main(String[] args) { TestMethodsDetails details = new TestMethodsDetails(); System.out.println("Method Details : "+details.getKnownGoodMap("TestMethodDetails.xls")); } } 

上面的代码打印出以下内容:

 Method Details : {Method Name=0, getLoggedinUserDetails=400, createRequisition=400, excelMDM=400, createOrder=400, confirmOrder=400} to be frank i really don't know what mechanism to use to store these details so that i enables easy access for me to process details, the user name and password will be different, i have given here some user and password for sample only 

请帮我这样做。

您应该使用面向对象的方法来创建实体。 创建一个表示excel数据行的类,假设称为Records,然后将此类的对象放在Hashmap中,并将用户名作为键。 这是示例类:

 public class Record { private String methodName; private String statusCode; private String user; private String password; public String getMethodName() { return methodName; } public void setMethodName(String methodName) { this.methodName = methodName; } public String getStatusCode() { return statusCode; } public void setStatusCode(String statusCode) { this.statusCode = statusCode; } public String getUser() { return user; } public void setUser(String user) { this.user = user; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } } 

定义一个集合以将记录存储为

 Map recordsMap = new HashMap(); 

阅读excel文件,为每行创建一条记录并保存在此集合中。 从地图中检索记录应该简单快捷。