Apache poi文件被损坏,无法写入现有的工作簿

我正在尝试使用以下代码来读取和写入工作簿:

public static void main(String args[]) { String absoluteFilePath = System.getProperty("user.dir") + File.separator + "abc.xlsx"; System.out.println("Readin file : " + absoluteFilePath); Workbook workbook = null; try { workbook = WorkbookFactory.create(new File(absoluteFilePath)); //reading and writing on sheets of workbook } } catch (Exception e) { e.printStackTrace(); } finally { try { System.out.println("Writing to workbook and Closing the file"); FileOutputStream fileOutputStream = new FileOutputStream( new File(absoluteFilePath)); workbook.write(fileOutputStream); fileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } 

当我第一次运行代码时,我在workbook.write(fileOutputStream);得到这个exceptionworkbook.write(fileOutputStream);

 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:148) at org.apache.poi.POIXMLDocument.write(POIXMLDocument.java:199) at NewNewDriver.main(NewNewDriver.java:129) 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:146) ... 2 more 

之后,工作簿被损坏,我减less到0kb,我得到WorkbookFactory.create()这个exception:

 org.apache.poi.poifs.filesystem.NotOLE2FileException: Invalid header signature; read 0x0000000000000000, expected 0xE11AB1A1E011CFD0 - Your file appears not to be a valid OLE2 document at org.apache.poi.poifs.storage.HeaderBlock.<init>(HeaderBlock.java:167) at org.apache.poi.poifs.storage.HeaderBlock.<init>(HeaderBlock.java:117) at org.apache.poi.poifs.filesystem.NPOIFSFileSystem.<init>(NPOIFSFileSystem.java:225) at org.apache.poi.poifs.filesystem.NPOIFSFileSystem.<init>(NPOIFSFileSystem.java:164) at org.apache.poi.poifs.filesystem.NPOIFSFileSystem.<init>(NPOIFSFileSystem.java:145) at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:105) at NewNewDriver.main(NewNewDriver.java:27) Closing the file Exception in thread "main" java.lang.NullPointerException at NewNewDriver.main(NewNewDriver.java:129) 

我应该在哪里以及如何使用FileOutputStream,workbook.write(),并且即使使用WorkbookFactory也应该使用FileInputStream?

————编辑———————-我得到我的代码工作我用FileInputStream而不是WorkbookFactory来创build工作簿并在closuresFileOutputStream后closures它。 这工作。

我想我已经发现了你的问题:

 workbook = WorkbookFactory.create(new File(absoluteFilePath)); .... FileOutputStream fileOutputStream = new FileOutputStream( new File(absoluteFilePath)); workbook.write(fileOutputStream); 

你正在打开一个文件,然后在它仍然打开并被使用的时候覆盖它,这是不被支持的

你需要做的是写出更新的文件到不同的文件名。 如果要replace它,则需要在closures原始文件后进行第二步。

现在,您已经开始覆盖该文件,然后POI尝试将原始文件的某些部分复制到新文件中,并失败,因为它正在尝试读取的原始文件已被删除!

(NPOIFSFileSystem和OPCPackage,底层包代码,都支持就地写入和就地更新,但是像HSSFWorkbook / XSSFWorkbook / XWPFDocument这样的更高级别的代码只支持写出一个新文件,而且还没有足够的社区关注就地写入来增加支持)

在哪里以及如何使用FileOutputStream,workbook.write()

不是在finally块。 如果你有任何exception,你肯定不想把可能的垃圾写入文件。 workbook甚至可以在finally块中为null

将该代码移动到try块的末尾。