【问题标题】:org.apache.poi.openxml4j.exceptions.OpenXML4JRuntimeException: Fail to saveorg.apache.poi.openxml4j.exceptions.OpenXML4JRuntimeException:保存失败
【发布时间】:2020-08-12 00:08:22
【问题描述】:

我面对的是org.apache.poi.openxml4j.exceptions.OpenXML4JRuntimeException: Fail to save:an error occurs while saving the package : The part /docProps/app.xml fail to be saved in the stream with marshaller <br/> org.apache.poi.openxml4j.opc.internal.marshallers.DefaultMarshaller@7c81475b

在每个测试场景执行完成后尝试将每个测试场景结果(PASS 或 FAIL)写入 Excel 表(.xlsx)时出现异常。为此,我编写了以下两个不同的模块。

请告诉我问题出在哪里以及如何解决。

//Method for writing results into Report
 public void putResultstoReport(String values[])
 {
      int j=NoofTimesExecuted;
      NoofTimesExecuted++;
      XSSFRow row = sheet.createRow(j);
      for(int i=0;i<values.length;i++)
      {
           XSSFCell cell = row.createCell(i);
           cell.setCellValue(values[i]);
      }
      try {
           System.out.println("Times:"+NoofTimesExecuted);
           wb.write(fileOut);
      }
      //fileOut.flush();
      //fileOut.close();
      }
      catch(Exception e) {
           System.out.println("Exception at closing opened Report :"+e);
      }

//Method for Creating the Excelt Report
 public void createReport()
 {
      String FileLocation = getProperty("WorkSpace")+"//SCH_Registration//OutPut//TestResults.xlsx";
      try {
           fileOut = new FileOutputStream(FileLocation);
           String sheetName = "TestResults"; //name of sheet
           wb = new XSSFWorkbook();
           sheet = wb.createSheet(sheetName);
           fileOut.flush();
           fileOut.close();
      }
      catch(Exception e)
      {
           System.out.println("Exception at Create Report file:"+e);
      }
}

【问题讨论】:

标签: file-io excel-2010 export-to-excel


【解决方案1】:

我今天遇到了这个问题,已经解决了。

问题出在putResultstoReport()

你不能在你的周期中wb.write(fileOut);

分辨率:

首先调用putResultstoReport(); 然后wb.write(fileOut);

【讨论】:

    【解决方案2】:

    我也遇到了这个错误。

    我发现我的错误是因为我多次打开同一个文件/工作簿。

    因此,我建议您在尝试关闭之前确保只打开一次。

    【讨论】:

      【解决方案3】:

      如果发生超时,可能会发生这种情况。我有适用于小型数据集的代码,并在大型数据集上引发此错误。

      【讨论】:

        【解决方案4】:

        我有类似的问题。 最后我得到了原因,那就是下面的 jar 文件的版本被覆盖了。

          org.apache.xmlgraphics:batik-dom
        

        因此,我添加了以下依赖项,现在它工作正常。

        <dependency>
            <groupId>org.apache.xmlgraphics</groupId>
            <artifactId>batik-dom</artifactId>
            <version>1.8</version>
        </dependency>
        

        此 jar 包含 xalan 的依赖项。 要生成报告,xalan 是必需的。

        【讨论】:

          【解决方案5】:

          我有同样的问题。 当我缩短输出 excel 文件名时,它停止了。

          【讨论】:

            【解决方案6】:

            在上一个请求完成之前,我在用户刷新页面并再次发送请求时遇到了同样的问题。 创建名称时,在名称中使用毫秒以避免名称冲突与名称创建代码中的这些更新解决了上述问题。

                   String sheetName="projectName"+System.currentTimeMillis() + ".xlsx"
                   FileOutputStream fileOut = new FileOutputStream(sheetName);
                   workbook.write(fileOut);
                   fileOut.close();
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2021-10-24
              • 2020-10-12
              • 2011-11-17
              • 2014-04-21
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多