如何从Excel的值存储在Java中收集的一些 [英] How to store values from excel to some collection in java

查看:152
本文介绍了如何从Excel的值存储在Java中收集的一些的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个excel文件类似以下,

i have a excel file like the following,

**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

我要救一些收集上述信息,这样我可以访问通过提供用户名和获得的细节,如方法名称,状态详细信息code和密码。

我想这样的,并能够只存放一些东西方法名称和状态code

i tried some thing like this and able to store only 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"));
    }
}

以上code打印以下内容:

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.

推荐答案

您应该使用面向对象的方法来创建实体。创建一个类重presenting您的Excel数据行,可以说所谓的记录,然后把这个类的对象在HashMap对用户名作为重点。下面是示例类:

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 :

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();

读取Excel文件,创建的每一行记录,并保存收藏。它应该是方便,快捷,从地图检索记录。

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的值存储在Java中收集的一些的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆