我正在处理一个项目,它需要读取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以下是我打开文件/读取的代码:
pkg = OPCPackage.open(xslFile);
theWorkbook = new XSSFWorkbook(pkg);在此之后,我读取每一行并提取每个单元格值。
完成后,我将在Success和Result消息的标头下创建单元格,然后执行以下操作:
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();有没有人遇到过类似的问题?
发布于 2013-01-04 04:12:25
当前列出的问题是自2010年以来一直存在的错误,可在@ https://issues.apache.org/bugzilla/show_bug.cgi?id=49940中找到
在下面的stackoverflow列表中,发现了一个解决方法,如果您在对文件进行另一次写出之前再次关闭并重新打开图书,它将不会出现问题。这无论如何都不是很有效,但它确实解决了这个问题,直到Apache-POI开发团队解决了这个问题。
发布于 2014-02-07 00:22:26
我得到了相同的错误消息,但使用了不同的类。我当前使用的poi版本是poi-ooxml 3.9,但它仍然存在这个问题。现在我修复了我的问题,我认为这个问题是在你首先获取Workbook实例时出现的。
当我将数据写入文件时,我是这样做的(使用exceptions和close的实践规则):
FileOutputStream fos = new FileOutputStream(filePath);
wb.write(fos);
fos.close();当我像这样获取Workbook实例时,我得到了"Can't obtain the input stream from /docProps/app.xml“的错误信息:
Workbook wb = WorkbookFactory.create(new File(filePath));当我修复这个问题时,修改后的代码是
Workbook wb = WorkbookFactory.create(new FileInputStream(filePath));在我的例子中,不管你是打开并读写同一个文件,还是从一个文件读取然后再写到另一个文件。如果您阅读poi源代码,您可以看到我使用的工厂方法可能调用OPCPackage类中的open()方法。尝试使用获取InputStream作为其参数的方法。
发布于 2013-01-02 14:15:45
我认为这里的问题是您使用相同的filePath xslFile来打开和保存文件。
打开文件,
pkg = OPCPackage.open(xslFile);
theWorkbook = new XSSFWorkbook(pkg);保存文件,
FileOutputStream fos = new FileOutputStream(xslFile);
theWorkbook.write(fos);
fos.close();你需要一个InputStream来读取和操作你的文件,但是当你在相同的路径和文件名下创建一个OutputStream时,这个流变得不可访问。
https://stackoverflow.com/questions/14117617
复制相似问题