org.apache.poi.openxml4j.exceptions.OpenXML4JRuntimeException:无法保存

时间:2013-11-06 10:56:01

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

我正面临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);
      }
}

5 个答案:

答案 0 :(得分:5)

我也有这个错误。

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

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

答案 1 :(得分:4)

我今天遇到了这个问题并且已经修好了。

问题出在putResultstoReport()

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

分辨率:

首先致电putResultstoReport();,然后致电wb.write(fileOut);

答案 2 :(得分:0)

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

  org.apache.xmlgraphics:batik-dom

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

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

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

答案 3 :(得分:0)

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

答案 4 :(得分:0)

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

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