我创建了一个用poi编写文件.xls的函数:
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);
这似乎不够。
答案 0 :(得分:0)
您需要从文件/输入流创建工作簿对象,然后您可以读取现有的行/单元格/ ...并修改/添加内容,即
InputStream stream = new FileInputStream(excelFileName);
try {
HSSFWorkbook wb = new HSSFWorkbook(stream);
...
// write to a new file here!
wb.write(outStream);
} finally {
stream.close();
}