2013-05-10 64 views
1

我有一個excel文件中像下面如何值從Excel中存儲一些集合中的Java

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

我想是這樣,能有些東西只存儲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")); 
    } 
} 

上面的代碼打印如下:

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 

請幫助我做到這一點。

+1

我很想說你想找到類似於「數據結構和算法101」的在線講義,並通讀這些內容 - 你的問題似乎不是POI特有的,而是基本的計算機科學設計查詢... – Gagravarr 2013-05-10 10:13:58

+0

我更喜歡Andy Khan的JExcel到Apache庫。我用了很多次,取得了巨大的成功。閱讀Excel並選擇正確的對象/數據結構來操作它是兩件不同的事情。你不清楚你的問題是什麼。 – duffymo 2013-05-10 10:14:17

+0

這可能會幫助你http://stackoverflow.com/questions/857380/good-way-to-represent-a-excel-sheet-value-in-java – 2013-05-10 10:14:43

回答

8

您應該使用面向對象的方法來創建實體。創建一個代表你的excel數據行的類,讓我們說叫記錄,然後把這個類的對象放在一個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文件,創建於各行的記錄並保存此收藏。從地圖上檢索記錄應該很容易和快捷。

+0

感謝您的帖子,非常好的一點開始+1 – 2013-05-10 10:26:14

相關問題