2016-10-11 20 views
1

私は 'transactionBatch'パラメータによって 'グループ'の取引をしようとしています。私はHashMapがユニークなバッチを集めることに成功していることを知っています。以下のコードをデバッグすると、すべてが正しく行われているようです。 Excelファイルをチェックすると、トランザクションはそれに応じてグループ化されません。彼らは、グループ化する必要があります。Apache POIで文書とソートを生成中

batch # 1: 
- transaction with batch # 1 
- transaction with batch # 1 

batch # 2: 
- transaction with batch # 2 
- transaction with batch # 2 

結果は、ファイルに含まれていエクセル:

batch # 1: 
- transaction with batch # 1 
- transaction with batch # 2 

batch # 2: 
- transaction with batch # 4 
- transaction with batch # 1 

これはコードです:

HashMap<String, String> hashMap = new HashMap<>(); 

     for (int i = 0; i < nodeList.getLength(); i++) { 
      hashMap.put(((Element) (nodeList.item(i))).getElementsByTagName("transactionBatch").item(0) 
        .getFirstChild().getNodeValue(), ((Element) (nodeList.item(i))).getElementsByTagName("transactionBatchDate").item(0) 
        .getFirstChild().getNodeValue()); 
     } 

     for (Map.Entry<String, String> entry : hashMap.entrySet()) { 
      for (int i = 0; i < nodeList.getLength(); i++) { 
       String transactionBatch = ((Element) (nodeList.item(i))).getElementsByTagName("transactionBatch").item(0) 
         .getFirstChild().getNodeValue(); 
       String key = entry.getKey(); 
       if (transactionBatch.equals(key)) { 
        HSSFRow dynamicRow = spreadSheet.createRow(i + 2); 

        if (nodeList.getLength() != 0) { 
         cell = dynamicRow.createCell((short) 0); 
         cell.setCellValue(((Element) (nodeList.item(i))).getElementsByTagName("transactionNumber").item(0) 
           .getFirstChild().getNodeValue()); 
         cell.setCellStyle(styleWithDataCentered); 

         ... 

         cell = dynamicRow.createCell((short) 8); 
         cell.setCellValue(((Element) (nodeList.item(i))).getElementsByTagName("transactionBatch").item(0) 
           .getFirstChild().getNodeValue()); 
         cell.setCellStyle(styleWithDataCentered); 
        } 
       } 
      } 
     } 

答えて

0

問題は、この行にあった:

HSSFRow dynamicRow = spreadSheet.createRow(i + 2); 

私は別の変数をforメソッドがintを保持する:

そして、各行でその値を増やすだけです。

HSSFRow dynamicRow = spreadSheet.createRow(rowNumber += 1);