org.apache.xmlbeans.impl.values.XmlValueDisconnect

2019-04-05 08:21发布

问题:

This question already has an answer here:

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

i'm create a method to write and read work book from file but when i call this method second time. error occure : org.apache.xmlbeans.impl.values.XmlValueDisconnectedException

public XSSFWorkbook GetUpdatedResult(XSSFWorkbook vmworkbookhelper) throws Exception
{
     this.vmWorkbookHelper2  = vmworkbookhelper;
    String tempName = UUID.randomUUID().toString()+".xlsx";
    File tempFile = new File(tempName);
    fileOut = new FileOutputStream(tempFile);
    this.vmWorkbookHelper2.write(fileOut);
    fileOut.close();
    vmworkbookhelper = new XSSFWorkbook(tempFile);
    if(tempFile.exists())
        tempFile.delete();
    return vmworkbookhelper;
}

回答1:

Agree with Akokskis, writing twice causing problem, but you can try reloading again the workbook after writing, then it will perfectly work. For example

    FileOutputStream fileOut = new FileOutputStream("Workbook.xlsx");
    wb.write(fileOut);
    fileOut.close();
    wb = new XSSFWorkbook(new FileInputStream("Workbook.xlsx"));


回答2:

Writing twice to the same XSSFWorkbook can produce that error - it's a known bug.