2012-06-30 33 views
11

我想創建一個可追加的excel表。 像我有四列stream1 stream2 stream3 stream4如何使用java創建可追加的excelsheet

第一次我插入數據只有第一列(stream1) 之後,我想充分填寫其他列逐一。

這是我使用的代碼:

public void createFile(Jqueue stream1, Jqueue stream2, Jqueue stream3, Jqueue stream4) { 
    try { 

     String filename = "path"; 
     boolean alreadyExists = (new File(filename)).exists(); 

     HSSFRow rowhead = sheet1.createRow((short) 0); 
     rowhead.createCell((short) 0).setCellValue("Stream 1"); 
     rowhead.createCell((short) 1).setCellValue("Stream 2"); 
     rowhead.createCell((short) 2).setCellValue("Stream 3"); 
     rowhead.createCell((short) 3).setCellValue("Stream 4"); 


     int i = 1; 
     while (!stream1.isEmpty()) { 
      String urlstream1 = ""; 
      String urlstream2 = ""; 
      String urlstream3 = ""; 
      String urlstream4 = ""; 
      HSSFRow row = sheet1.createRow((short) i); 
      try { 
       if (stream1.size() > 0) { 
        urlstream1 = stream1.dequeue().toString(); 
       } 
      } catch (Exception ex) { 
      } 
      try { 
       if (stream2.size() > 0) { 
        urlstream2 = stream2.dequeue().toString(); 
       } 
      } catch (Exception ex) { 
      } 
      try { 
       if (stream3.size() > 0) { 
        urlstream3 = stream3.dequeue().toString(); 
       } 
      } catch (Exception ex) { 
      } 
      try { 
       if (stream4.size() > 0) { 
        urlstream4 = stream4.dequeue().toString(); 
       } 
      } catch (Exception ex) { 
      } 
      row.createCell((short) 0).setCellValue(urlstream1); 
      row.createCell((short) 1).setCellValue(urlstream2); 
      row.createCell((short) 2).setCellValue(urlstream3); 
      row.createCell((short) 3).setCellValue(urlstream4); 
      i++; 
     } 

     FileOutputStream fileOut = new FileOutputStream(filename); 
     hwb.write(fileOut); 
     fileOut.close(); 

但這不是追加,能夠代碼。它逐行插入數據。

thx in advanced。

回答

1

我想你的代碼應該稍微改變一下。

嘗試更換

INT I = 1;

與下列:

INT I = sheet1.getLastRowNum()+ 1;

您還需要更改您的實現以便讀取和寫入文件。