如何将值从 excel 存储到 Java 中的某个集合
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16479882/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to store values from excel to some collection in java
提问by Java Questions
i have a excel file like the following,
我有一个像下面这样的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
i want to save the above details in some collectionso that i can access details by providing username and get the details like Method Name, Status code,and password.
我想将上述详细信息保存在一些中,collection以便我可以访问details by providing username and get the details like Method Name, Status code,and password.
i tried some thing like this and able to store only method name and status code
我尝试过这样的事情并且只能存储 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<String, Integer> getKnownGoodMap(String filePath) {
String key = "";
int value = 0;
//filePath="TestMethodDetails.xls";
Map<String, Integer> knownGoodMap = new LinkedHashMap<String, Integer>();
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<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
// For each row, iterate through each columns
Iterator<Cell> 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"));
}
}
the above code prints the following :
上面的代码打印以下内容:
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
Kindly help me to do this.
请帮助我做到这一点。
回答by Juned Ahsan
You should use Object Oriented approach to create entities. Create a class representing your excel data row, lets say called Records and then put the objects of this class in a Hashmap against username as key. Here is sample class :
您应该使用面向对象的方法来创建实体。创建一个代表您的 excel 数据行的类,假设称为 Records,然后将此类的对象放在一个以用户名为键的哈希图中。这是示例类:
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;
}
}
Define a collection to store the records as
定义一个集合来存储记录
Map<String, Record> recordsMap = new <String, Record>HashMap();
Read the excel file, create a record for each row and save in this collection. It should be easy and quick to retrieve the records from the map.
读取excel文件,为每一行创建一个记录并保存在这个集合中。从地图中检索记录应该简单快捷。

