gpt4 book ai didi

java - 无法将数据追加到现有 Excel 中

转载 作者:行者123 更新时间:2023-12-01 18:59:41 26 4
gpt4 key购买 nike

我无法将数据追加到现有的 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();

最佳答案

Excel 文件格式不是可附加格式。要写出更改,您必须更新整个文件。

只需将 FileOutputStream 更改为替换而不是附加,然后您的代码就应该按照您的预期运行。

关于java - 无法将数据追加到现有 Excel 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12673004/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com