2012-06-30 12 views
11

Desidero creare un foglio Excel in grado di accodare. Come se avessi quattro colonne stream1 stream2 stream3 stream4come creare un eccelsheet accesibile usando java

prima volta inserisco solo i dati della prima colonna (stream1) dopo di che voglio riempire completamente altre colonne una alla volta.

questo è il codice che sto usando:

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(); 

ma questo non è accodamento in grado di codice. il suo inserimento dati riga per riga.

thx in avanzato.

risposta

1

Suppongo che un leggero cambiamento nel codice dovrebbe farlo.

Prova a sostituire

int i = 1;

con il seguente:

int i = sheet1.getLastRowNum() + 1;

Si sarebbe anche necessario modificare l'implementazione per la lettura e la scrittura di file un po '.