2017-04-06 34 views
0

我做了與POI寫入文件的.xls功能:每次如何改變xls文件與Java POI

public void write() throws IOException{ 
    String excelFileName = "C:\\Users/Default/Desktop/MyFirstExcel.xls";//name of excel file 

    String sheetName = "Sheet1";//name of sheet 

    HSSFWorkbook wb = new HSSFWorkbook(); 
    HSSFSheet sheet = wb.createSheet(sheetName) ; 

    //iterating r number of rows 
    for (int r=0;r <10; r++) 
    { 
     HSSFRow row = sheet.createRow(r); 

     //iterating c number of columns 
     for (int c=0;c < 5; c++) 
     { 
      HSSFCell cell = row.createCell(c); 

      cell.setCellValue((String)"Cell "+r+" "+c); //+r+" "+c 
     } 
    } 

    FileOutputStream fileOut = new FileOutputStream(excelFileName); 

    //write this workbook to an Outputstream. 
    wb.write(fileOut); 
    fileOut.flush(); 
    fileOut.close(); 
      Alert alert = new Alert(AlertType.INFORMATION); 
      alert.setTitle("Information Dialog"); 
      alert.setHeaderText(null); 
      alert.setContentText("Done"); 
      alert.showAndWait();     
} 

此方法創建一個新的文件,我不知道如何追加信息到文件。 我試圖拼這一行:

FileOutputStream fileOut = new FileOutputStream(excelFileName,true); 

這似乎還不夠。

+1

如果您想更改Excel,您需要打開並修改工作簿 – maraca

回答