Exception when writing to the xlsx document several times using apache poi 3.7

后端 未结 7 1738
夕颜
夕颜 2020-11-29 08:44

I am getting the following exception while trying to write an .xlsx file using Apache POI: org.apache.xmlbeans.impl.values.XmlValueDisconnectedException

7条回答
  •  一个人的身影
    2020-11-29 09:00

    The solution I've found for this, and I've been looking for a while, is to make sure you don't open your Workbook with the File which you use to open the FileOutputStream to save the Workbook. Instead, use a FileInputStream to open the Workbook.

    Something like this will work flawlessly

            File inputFile = new File("Your-Path");
            this.inputStream = new FileInputStream(inputFile);
            this.opc = OPCPackage.open(this.inputStream);
            this.workbook = WorkbookFactory.create(opc);
    
    ...
    
            this.outputStream = new FileOutputStream(inputFile);
            this.workbook.write(this,outputStream);
    

    Don't forget to close every opened stream and the OPCPackage.

提交回复
热议问题