2011-10-12 33 views
1

好吧,基本上我試圖在迭代時同時寫入3列和N行。問題是,3列中的2列是用值寫入的...即使它們出現在System.out.println()中,也沒有寫入Excel中。下面的代碼:爲什麼在使用Java編寫Excel時發生這種情況

for(int i = 0; i < versionsToGraph.length; i++) {   
        List<WsCallEntry> wfCalls = allCalls.get(i); 
        int cant = -1; 
        if(!wfCalls.isEmpty()) {        
         for (WsCallEntry wsCallEntry : wfCalls) { 
          String x = wsCallEntry.getKey(); 
          int y = wsCallEntry.getValue(); 
          cant++; 
          if(versionsToGraph[i].equals("15.6")) { 
           System.out.println(x + " " + y + " will be added."); 
           XSSFSheet sheet = (XSSFSheet) wb.getSheetAt(0);  
           XSSFRow row = sheet.createRow(27+cant);         

           XSSFCell celly = row.createCell(10); 

           celly.setCellValue(y); 
          }else{ 
           if(versionsToGraph[i].equals("15.9")) { 

            System.out.println(x + " " + y + " will be added."); 
            XSSFSheet sheet = (XSSFSheet) wb.getSheetAt(0);  
            XSSFRow row = sheet.createRow(27+cant);         

            XSSFCell celly = row.createCell(11); 

            celly.setCellValue(y); 
           } 
          }      


          xValues.add(x); 


         }       


         Collections.sort(xValues, new StringComparator()); 

       }       
       }      
       ArrayList<String> newList = eliminarRepetidos(xValues); 
       int cant = 0; 
       for (String newlist : newList) { 
        String x = newlist; 
        XSSFSheet sheet = (XSSFSheet) wb.getSheetAt(0);   
        XSSFRow row = sheet.createRow(27+cant);         
        XSSFCell cellx = row.createCell(9); 
        cellx.setCellValue(x); 
        cant++; 
       }    
      } 

所以,應該增加在這部分代碼,15.6,15.9 Y值,並且在最後的foreach在Excel中寫入X的值。 我得到的是X值和15.9 Y值。我沒有得到15.6的? :(

(從評論) I'm使用這些

import org.apache.poi.ss.usermodel.Cell; 
import org.apache.poi.ss.usermodel.Workbook; 
import org.apache.poi.ss.usermodel.WorkbookFactory; 
import org.apache.poi.xssf.usermodel.XSSFCell; 
import org.apache.poi.xssf.usermodel.XSSFRow; 
import org.apache.poi.xssf.usermodel.XSSFSheet; 
+1

你用什麼java包創建你的xls? – smp7d

+0

我正在使用這些 import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.WorkbookFactory; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; – msqar

+0

因爲我正在編輯創建的XLSX文件,這就是爲什麼我需要使用XSSF。 – msqar

回答

1

您是在每個迭代上創建相同的行3210,你正在擦除之前添加的內容。

XSSFRow row = sheet.createRow(27+cant); 

你應該在第一次迭代後做

sheet.getRow(27+cant); 

1

那麼,怎樣的wfCalls許多情況下有哪些?又有多少versionToGraph這些實例都等於15.6或15.9?如果cant由於版本不匹配而增加了很多次,因此表格中的行可能會被粘貼到Excel中的第一個可見行。我會仔細檢查您正在寫入的行號。

+0

只有一個wfCalls實例。事情是,在wfCalls內部是X,Y配對值。列表中的第一個「一半」是15.6,另一半是15.9。 現在它可以同時工作15.6,15.9和兩者。 我試過評論15.9的例子,並寫入15.6和X值,如果我評論15.6部分,它寫入15.9和X,如果我評論X部分,它寫入15.6和15.9,wtf :( – msqar

相關問題