2013-04-25 66 views
0

我有一个程序,我必须从Excel文件中读取数据并将它们存储在数据库中。我正在使用LinkedHashmap以字符串形式读取每个单元格。我的excel文件包含上述数据:treemap升序ID

ID NAME SALARY

5恭2349000

6波莱纳1000

7劳拉12587458

8 EFI 34567

43吉姆45878

当我运行程序时,我已经成功地得到了我想要的结果。我现在的问题是,我想通过升序ID将数据存储在数据库中。 我将如何做到这一点?我知道我必须使用treeMap,但究竟如何? 下面的代码用于将数据存储在数据库中。我正在读取Excel文件第二行的数据。

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

将您的树形图键(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()你会树形图中得到的值的分类收集。