我无法将数据附加到现有的Excel工作表中 这是我的代码:
fis = new FileInputStream(file);
fs = new POIFSFileSystem(fis);
HSSFWorkbook wb1 = new HSSFWorkbook(fs);
sheet1 = wb1.getSheet("sheet1");
System.out.println("Last Row No.: " + sheet1.getLastRowNum());
row = sheet1.createRow(sheet1.getLastRowNum()+ 1);
System.out.println("Last Row No.: " + sheet1.getLastRowNum());
row.createCell(0).setCellValue(2);
row.createCell(1).setCellValue(2.2);
row.createCell(2).setCellValue(true);
fos = new FileOutputStream(file,true);
wb1.write(fos);
fos.flush();
fos.close();
答案 0 :(得分:3)
excel文件格式不是可附加格式。要写出更改,您必须更新整个文件。
只需将FileOutputStream更改为替换而不是附加,然后您的代码应该按预期运行。
答案 1 :(得分:1)
尝试使用此代码附加
fis = new FileInputStream(file);
Workbook wb=WorkbookFactory.create(fis);
sheet1 = wb1.getSheet("sheet1");
System.out.println("Last Row No.: " + sheet1.getLastRowNum());
row = sheet1.createRow(sheet1.getLastRowNum()+ 1);
System.out.println("Last Row No.: " + sheet1.getLastRowNum());
row.createCell(0).setCellValue(2);
row.createCell(1).setCellValue(2.2);
row.createCell(2).setCellValue(true);
fos = new FileOutputStream(file,true);
wb1.write(fos);
fos.flush();
fos.close();