代码之家  ›  专栏  ›  技术社区  ›  Dhruv Chadha

ApachePOI:运行工作簿创建代码两次会出现错误“Sheet ready exists”,即使是在工作簿之后。关闭()

  •  0
  • Dhruv Chadha  · 技术社区  · 5 年前

    我正在编写一个填充工作簿的代码,然后将其写入excel文件。然而,当我第二次运行代码时,它抛出一个错误,说“sheet ready exists”。

    下面是基本代码-

    File file = getFileObjectForExistingXLSXTemplate(existingTemplateLocation);
    Workbook workbook = null;
    workbook = WorkbookFactory.create(file);
    populateWorkbookThenWriteOutToExcel(workbook, someOtherDestinationDifferentFromTemplateLocation);
    workbook.close();
    file.delete();
    

    请帮助我理解在第一次运行代码后如何从内存中清除工作簿。

    最初,现有模板只有“表1”。代码将“表2”写入其中。第一次,一切都很顺利。但第二次,当我立即运行相同的代码时,它会显示“表2已经存在”。

    0 回复  |  直到 5 年前
        1
  •  0
  •   Dhruv Chadha    5 年前

    答案->使用WorkbookFactory。create(FileInputStream(file))修复了这个问题,但不确定如何修复。