java - 如何将值从excel存储到java中的某个集合

标签 java collections apache-poi

<分区>

我有一个 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

我想将上述详细信息保存在一些集合中,以便我可以通过提供用户名访问详细信息,并获取方法名称、状态代码和密码等详细信息。

我试过这样的东西,只能存储方法名和状态码

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"));
    }
}

上面的代码打印如下:

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<String, Record> recordsMap = new <String, Record>HashMap();

读取excel文件,为每一行创建一条记录并保存在这个集合中。从 map 中检索记录应该简单快捷。

关于java - 如何将值从excel存储到java中的某个集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16479882/

相关文章:

java - 使用 DataJpaTest spring boot 2.1.0 更新失败

java - 不可变集和映射上的 JDK9 随机化

java - 如何使用 Apache POI 对 XSSFTable 列启用排序/过滤?

java - 应该如何连接数组流?

c# - 初始化集合的简洁方法

java - Apache POI : How to set font style for a field (PAGE, PAGENUM, PAGEREF...)

java - 异常 org.apache.poi.poifs.filesystem.OfficeXmlFileException - apache.Poi 4.0.0

java - 我如何知道一个类是否被映射为一个 Hibernate 实体?

java - 无法执行二进制文件 - 可执行 Jar 文件

java - 如何保持 JavaFX Spinner 值的比率