无法更新 excel 数据 - 它会覆盖实际数据

not able to update the excel data - it overwrites the actual data

我有一个使用 apache poi api 写入 excel 文件的代码。问题是它每次都将新数据写入文件而不是追加数据。你能在这里帮助我吗?这是我的代码。

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class ExcelWrite {
    public static void write(AddExcel addExcel){
       try {
           XSSFWorkbook workbook = new XSSFWorkbook("NG.xlsx");
           XSSFSheet worksheet = workbook.createSheet("Scrap Data");



           int lastRow = worksheet.getLastRowNum();
           System.out.println(lastRow);
           Row row = worksheet.createRow(++lastRow);
           row.createCell(2).setCellValue(addExcel.getArtistName());
           row.createCell(3).setCellValue(addExcel.getItemName());
           row.createCell(6).setCellValue(addExcel.getOriginalPrimaryMarket());
           row.createCell(7).setCellValue(addExcel.getAvgResalePrice());
           row.createCell(8).setCellValue(addExcel.getPriceChangedFromPrimaryMarket());
           row.createCell(9).setCellValue(addExcel.getHighestAvgBid());
           row.createCell(10).setCellValue(addExcel.getLastSoldPrice());
           row.createCell(11).setCellValue(addExcel.getSecondayMarketVolume());
           row.createCell(12).setCellValue(addExcel.getSecondarySales());
           row.createCell(13).setCellValue(addExcel.getPrimarySales());
           row.createCell(14).setCellValue(addExcel.getDateCreated());
           row.createCell(16).setCellValue(addExcel.getInstagramURl());
           row.createCell(17).setCellValue(addExcel.getTwitterURL());


           FileOutputStream out = new FileOutputStream(new File("NG.xlsx"));
           workbook.write(out);
           out.close();
           System.out.println("Write Successfully.");

       }
       catch(IOException io){
           System.out.println(io.getMessage());
           System.out.println(io.getStackTrace());
       }

    }
}

根据我的测试(我使用 maven 存储库中的 4.1.2 版本)当您使用 createSheet 时它会产生异常:

java.lang.IllegalArgumentException: The workbook already contains a sheet named 'Scrap Data'

使用 getSheet() 方法,您可以获取现有的 sheet。如果它不存在,您将得到一个 null 然后您可以使用所需的名称创建一个新的 sheet。

XSSFWorkbook workbook;
try {
    File file = new File("NG.xlsx");
    workbook = new XSSFWorkbook();
    if(file.exists()) {
       FileInputStream fs = new FileInputStream(file);
       workbook = new XSSFWorkbook(fs);                
    }
    String sheetName = "Scrap Data";
    XSSFSheet worksheet = workbook.getSheet(sheetName);
    if(worksheet == null) {
       worksheet = workbook.createSheet(sheetName);
    }

... //other code unchanged

} catch(Exception io){
  io.printStackTrace();
}

注意:我稍微更改了文件读取代码,因为我遇到了错误并且文档说明如下:

* Note - if the Document was opened from a {@link File} rather
*  than an {@link InputStream}, you <b>must</b> write out to
*  a different file, overwriting via an OutputStream isn't possible.

问题是每次你 运行 你的代码,你总是创建 一个全新的 XSSFWorkbook 等等 一个品牌新建 excel 文件,删除并覆盖 现有文件。

你打电话没关系worksheet.getLastRowNum();它会总是return-1因为你的XSSFWorkbook总是空的.

如果您想更新现有的 excel 文件(向其添加新行),您必须通过加载现有的 excel 文件来创建 XSSFWorkbook . 你的代码被破坏了因为行

  workbook = new XSSFWorkbook();

您正在创建一个全新的 XSSFWorkbook,它与您要更新的 excel 文件完全无关。您必须改为使用:

  Workbook workbook = WorkbookFactory.create(new FileInputStream(toUpdateExcelFilePath));

您可能想查看此 post 以了解更多详细信息: