2013-01-03 56 views
0

我正在從Excel電子表格讀取每行和單元格,然後從特定行的每個單元格中獲取數據並使用該數據進行Web服務調用。一旦我得到Web服務XML響應的返回,我將解析它以獲得成功的布爾值以及發生失敗時的消息。當保存XSSFWorkbook只有一行更新

我能夠解析數據,並且抓取每一行響應,但是當試圖編輯最後兩個單元格時,它只更新第一行成功和結果消息單元格。

下面是我的一些代碼片段:

private static XSSFRow[] retrieveRows(XSSFWorkbook wb) 
{ 
    XSSFSheet xslSheet = wb.getSheetAt(0); 
    XSSFRow[] sheetRows = new XSSFRow[xslSheet.getLastRowNum()]; 

    for (int i = 1; i < xslSheet.getLastRowNum()+1; i++) 
    { 
     sheetRows[i-1] = xslSheet.getRow(i); 
    } 

    return sheetRows; 
} 

public static String[] processRow(XSSFRow xslRow) 
{ 
    String[] cellValues = new String[totalCells]; 

    for (int i = 0; i < totalCells; i++) 
    { 
     XSSFCell currentCell = xslRow.getCell(i); 
     String cellVal; 
     try 
     { 
      cellVal = currentCell.getStringCellValue(); 
     } 
     catch (Exception e) 
     { 
      cellVal = ""; 
     } 
     cellValues[i] = cellVal; 
     System.out.println("Cell "+i+": "+cellVal); 
    } 

    return cellValues; 
} 

而且像所有的魔法發生的最後部分...什麼的。

try 
    { 
     XSSFRow[] xslRows = retrieveRows(theWorkbook); 
     ArrayList<String[]> rowCellVals = new ArrayList<String[]>(); 

     String sessionKey[] = WQSServices.sessionToken(); 

     for (int r = 0; r < xslRows.length; r++) 
     {    
      System.out.println("R Value: "+r); 
      rowCellVals.add(processRow(xslRows[r])); 

      if(sessionKey[0].equals("true")) 
      { 
       String sessionData = sessionKey[1]; 
       String[] cellValCurrRow = rowCellVals.get(r); 
       String attachmentData[] = WQSServices.uploadAttachment(sessionData, cellValCurrRow); 

       XSSFCell cell = xslRows[r].getCell(7); 

       if(cell == null) 
       { 
        cell = xslRows[r].createCell(7); 
       } 

       XSSFCell cell2 = xslRows[r].getCell(8); 

       if(cell2 == null) 
       { 
        cell2 = xslRows[r].createCell(8); 
       } 

       cell.setCellType(Cell.CELL_TYPE_STRING); 
       cell2.setCellType(Cell.CELL_TYPE_STRING); 
       cell.setCellValue(attachmentData[0]); 
       cell2.setCellValue(attachmentData[1]); 

       System.out.println("New Cell Data: 1-"+cell.getStringCellValue()+" 2-"+cell2.getStringCellValue()); 

       FileOutputStream fos = new FileOutputStream(xslFile); 
       theWorkbook.write(fos); 
       fos.close(); 
      } 
      else 
      {     

       XSSFCell cell = xslRows[r].getCell(7); 

       if(cell == null) 
       { 
        cell = xslRows[r].createCell(7); 
       } 

       System.out.println("The Cell: "+cell.getStringCellValue()); 

       cell.setCellType(Cell.CELL_TYPE_STRING); 
       cell.setCellValue(sessionKey[0]); 

       FileOutputStream fis = new FileOutputStream(xslFile); 
       theWorkbook.write(fis); 
       fis.close(); 

      } 

      readFile(); 
     } 

    } 
    catch (IOException e) 
    { 
     e.printStackTrace(); 
    } 
+1

你似乎是從* forloop裏面的*裏面寫出文件,不應該在最後? – Gagravarr

+0

謝謝,我的大腦被炸了。欣賞快速評估和簡單的答案:) –

回答

1

基於Gagravarr觀察我的問題是基於讓我的文件輸出流在for循環中。一旦我將它移出for循環,就會按預期寫入工作簿。