2013-04-25 4 views
0

나는 Excel에서 데이터를 읽고 데이터베이스에 저장해야하는 프로그램이 있습니다. LinkedHashmap을 사용하여 각 셀을 문자열로 읽습니다. 내 엑셀 파일은, 상기 데이터가 포함오름차순 ID를위한 트리 맵

ID 명칭 급여

5 크리스틴 2,349,000

6 폴리나 1,000

7 로라 12,587,458

8 EFI 34,567

43 짐 45878

내가 프로그램을 실행할 때 나는 내가 원하는 결과를 성공적으로 얻었습니다. 내가 지금 가지고있는 문제는 오름차순 ID로 데이터베이스에 데이터를 저장하려고한다는 것입니다. 나는 이것을 어떻게 할 것인가? 나는 treeMap을 사용해야한다는 것을 알고 있지만 정확히 어떻게 사용합니까? 아래 코드는 데이터베이스에 데이터를 저장하는 코드입니다. 엑셀 파일의 두 번째 행에서 데이터를 읽습니다.

private static LinkedHashMap[] parseExcelColumnData(List sheetData) { 

     LinkedHashMap[] tousRows = new LinkedHashMap[sheetData.size() - 1]; 
     for (int rowCounter = 1; rowCounter < sheetData.size(); rowCounter++) { 

      List list = (List) sheetData.get(rowCounter); 

      LinkedHashMap<String, Integer> tableFields = new LinkedHashMap(list.size()); 
      String str; 
      String[] tousFields = new String[list.size()]; 
      int i = 0; 

      for (int j = 0; j < list.size(); j++) { 
       Cell cell = (Cell) list.get(j); 
       if (cell != null) { 
        if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) { 
         tableFields.put(String.valueOf(cell 
           .getNumericCellValue()), cell.getCellType()); 
        } else if (cell.getCellType() == Cell.CELL_TYPE_STRING) { 
         tableFields.put(cell.getStringCellValue(), cell 
           .getCellType()); 
        } else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) { 
         tableFields.put(String.valueOf(cell 
           .getBooleanCellValue()), cell.getCellType()); 
        } 
       } 

      } 
      tousRows[rowCounter - 1] = tableFields; 
     } 

답변

0

treemap 키 (Ids)를 문자열이 아닌 정수로 저장하십시오. 그렇지 않으면 숫자 순서가 아니라 사전 식 순서로 비교됩니다.

Integer intId = new Integer(stringId); 

이제 그냥 TreeMap<Integer, Object> (또는 TreeMap<Integer, String> 또는 무엇이든 당신이 저장하고 무엇에 따라) 할당해야 키가 정수 ID이고 값이 저장되는 객체입니다.

TreeMap<Integer, Object> treeMap = new TreeMap<Integer, Object>(); 
treeMap.put(intId, obj); 

는, 값을 읽어 treeMap.values()를 사용하고 트리 맵에서 값의 정렬 된 집합을 얻을 것이다 시간이 온다.