2017-02-25 62 views
2

我需要使用poi將HashMaps轉換爲xlsx。對於片DATA2我需要類似的東西:將Java HashMap導出到xlsx

表1:

enter image description here

但我有表2:

enter image description here

這裏是我的包含HashMap的名單:

行= [{kol2 = s,kol1 = s},{kol2 = bbbb,kol3 = bbbb,kol1 = aaaa},{kol2 = BBBB,kol3 = BBBB,kol1 = AAAA},{kol2 = BBBB,kol3 = BBBB,kol1 = AAAA},{kol2 = S,kol1 = S}]}

這裏是我的代碼:

  XSSFWorkbook workBook = new XSSFWorkbook(); 
      XSSFSheet sheet = workBook.createSheet("data"); 
      XSSFSheet sheet2 = workBook.createSheet("data2"); 
      int rowCount = 0; 
      int help = 1; 


       List<HashMap<String, Object>> rows = ((List<HashMap<String, Object>>) x); 
       int rowCount2 = 0; 
       int header = 1; 
       Row header2 = sheet2.createRow(0); 
       for (int i = 0; i < rows.size(); i++) { 
        int li = 0; 
        Row row2 = sheet2.createRow(++rowCount2); 
        HashMap<String, Object> row = rows.get(i); 
        int columnCount2 = 0; 

        for (HashMap.Entry<String, Object> subElement : row.entrySet()) { 

         if (subElement.getValue() != null) { 

          if (i == li) { 
           Cell cell = header2.createCell(header); 
           cell.setCellValue(subElement.getKey().toString()); 
           header++; 
          } 
          li++; 
          Cell cell2 = row2.createCell(++columnCount2); 
          cell2.setCellValue(subElement.getValue().toString()); 
         } 
        } 
       } 

有人可以幫忙嗎?

回答

1

遍歷一個HashMap的的entrySet

第一個問題是,你遍歷你HashMap

for (HashMap.Entry<String, Object> subElement : row.entrySet()) { 
    // no guaranteed order 
} 

中的entrySet望着Set#iterator()方法,你會看到這一點的JavaDoc:

返回此集合中元素的迭代器。 元素以特定順序返回(除非此集合是某個提供擔保的類的實例)。

有其序集(如TreeSet),但由於您使用的是HashMap,你會的entrySet不會太訂購。

注意工作表中的列順序是kol2-kol3-kol1。你不想讓它成爲kol1-kol2-kol3嗎?

沒有創造空列

你忘了創建空細胞列你不要在你的地圖有。

if (subElement.getValue() != null) { 
    // there won't be an empty cell if you e.g. don't have kol2 in your rows Map, 
    // since this just skips your current value 
} 

這就是爲什麼你最終的東西,如:

kol2 kol3 kol1 
s  s  
bbbb bbbb aaaa 
... 

代替:

kol2 kol3 kol1 
s    s  
bbbb bbbb aaaa 
... 

創建循環

裏面的標題行通過創建您的循環內的標題行,您正在使您的解決方案更多比必要的複雜。只需要創建標題行,然後循環在List中的條目就會更容易。

if (i == li) { 
    Cell cell = header2.createCell(header); 
    cell.setCellValue(subElement.getKey().toString()); 
    header++; 
} 

如果您在循環外這樣做,就沒有必要對liheader可變

建議的解決方案

我會(一開始)來的東西像這樣(我添加了一些額外的評論,我通常不會在那裏更清楚地表達意圖是什麼以及需要了解哪些方面的解決方案):

XSSFSheet sheet2 = workBook.createSheet("data2"); 
    List<HashMap<String, Object>> rows = ((List<HashMap<String, Object>>) x); 

    List<String> headers = Arrays.asList("kol1", "kol2", "kol3"); 
    int currentRowNumber = 0; 

    // create header row 
    Row header = sheet2.createRow(currentRowNumber); 
    for (int i = 0; i < headers.size(); i++) { 
     Cell headerCell = header.createCell(i); 
     headerCell.setCellValue(headers.get(i)); 
    } 

    // create data rows (we loop over the rows List) 
    for (int i = 0; i < rows.size(); i++) { 
     HashMap<String, Object> row = rows.get(i); 

     // we neet to increment the rowNumber for the row in the sheet at the beginning of 
     // each row. entry 0 in the rows List is in sheetRow 1, entry 1 in sheetRow 2, etc. 
     currentRowNumber++; 
     Row sheetRow = sheet2.createRow(currentRowNumber); 

     // we can now loop over the columns inside the row loop (using the headers List) 
     // we create a Cell for each column, but only fill it if there is 
     for (int j = 0; j < headers.size(); j++) { 
      Cell cell = sheetRow.createCell(j); 

      // only fill the cell if we are having data in the row map for the current column 
      String currentColumnName = headers.get(j); 
      if (row.containsKey(currentColumnName)) { 
       cell.setCellValue(row.get(currentColumnName).toString()); 
      } 
     } 
    } 

如果您需要不同的列順序,只需更改標題列表即可完成(例如, Arrays.asList("kol2", "kol3", "kol1"))。