Apache-POI:无法写入现有工作簿

Mat*_*ter 10 java apache-poi excel-2010

我正在开发一个项目,该项目需要读取Excel工作簿,调用必要的Web服务,然后从Web服务获取响应并将该信息输入到已读取的同一Excel工作簿中.

这是我在尝试写入Excel工作簿时看到的错误:

Exception in thread "main" org.apache.poi.POIXMLException: java.io.IOException: Can't obtain the input stream from /docProps/app.xml
at org.apache.poi.POIXMLDocument.getProperties(POIXMLDocument.java:141)
at org.apache.poi.POIXMLDocument.write(POIXMLDocument.java:177)
at ext.ExcelProcessor.main(ExcelProcessor.java:197)
Caused by: java.io.IOException: Can't obtain the input stream from /docProps/app.xml
    at org.apache.poi.openxml4j.opc.PackagePart.getInputStream(PackagePart.java:500)
    at org.apache.poi.POIXMLProperties.<init>(POIXMLProperties.java:75)
    at org.apache.poi.POIXMLDocument.getProperties(POIXMLDocument.java:139)
    ... 2 more
Run Code Online (Sandbox Code Playgroud)

这是我打开文件/阅读的代码:

pkg = OPCPackage.open(xslFile);
    theWorkbook = new XSSFWorkbook(pkg);
Run Code Online (Sandbox Code Playgroud)

在此之后,我读取每一行并提取每个单元格值.

完成此操作后,我将在Success和Result Message的标题下创建单元格,然后执行以下操作:

String sessionData = sessionKey[1];
                String[] cellValCurrRow = rowCellVals.get(r-1);
                String attachmentData[] = WQSServices.uploadAttachment(sessionData, cellValCurrRow);

                XSSFCell cell = xslRows[r].getCell(7);

                if(cell == null)
                {
                    cell = xslRows[r].createCell(7);
                }

                System.out.println("The Cell: "+cell.getStringCellValue());

                XSSFCell cell2 = xslRows[r].getCell(8);

                if(cell2 == null)
                {
                    cell2 = xslRows[r].createCell(8);
                }

                System.out.println("The Cell: "+cell2.getStringCellValue());

                cell.setCellType(Cell.CELL_TYPE_STRING);
                cell2.setCellType(Cell.CELL_TYPE_STRING);
                cell.setCellValue(attachmentData[0]);
                cell2.setCellValue(attachmentData[1]);

                System.out.println("New Cell Data: 1-"+cell.getStringCellValue()+" 2-"+cell2.getStringCellValue());

                FileOutputStream fos = new FileOutputStream(xslFile);
                theWorkbook.write(fos);
                fos.close();
Run Code Online (Sandbox Code Playgroud)

有没有人遇到过类似的问题?

小智 13

我得到了相同的错误消息,但使用了不同的类.我正在使用的当前poi版本是poi-ooxml 3.9,但它仍然存在问题.现在我解决了我的问题,我认为当你首先获得Workbook实例时会出现这个问题.

当我将数据写入文件时,我喜欢这样(使用异常和关闭的练习规则):

    FileOutputStream fos = new FileOutputStream(filePath);
    wb.write(fos);
    fos.close();
Run Code Online (Sandbox Code Playgroud)

我得到"无法从/docProps/app.xml获取输入流"错误消息,当我获得这样的Workbook实例时:

    Workbook wb = WorkbookFactory.create(new File(filePath));
Run Code Online (Sandbox Code Playgroud)

当我修复问题时,修改后的代码就是

    Workbook wb = WorkbookFactory.create(new FileInputStream(filePath));
Run Code Online (Sandbox Code Playgroud)

在我的情况下,无论是打开,读取和写入同一文件,还是从一个文件读取然后写入另一个文件都无关紧要.如果您阅读poi源代码,您可以看到我使用的工厂方法可能在OPCPackage类中调用open()方法.尝试使用获取InputStream作为其参数的方法.


Mat*_*ter 1

当前列出的问题是自 2010 年以来一直存在的错误,可以在 @ https://issues.apache.org/bugzilla/show_bug.cgi?id=49940找到

在下面的 stackoverflow 列表中发现了一种解决方法,如果您在再次写入文件之前关闭并重新打开该书,它将可以正常工作。这无论如何都不是有效的,但它确实可以解决问题,直到 Apache-POI 开发团队找出问题为止。

/sf/answers/685468451/