10

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;
}
Johnny000
  • 2,058
  • 5
  • 30
  • 59
Nikesh Pathak
  • 450
  • 2
  • 7
  • 18

2 Answers2

6

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"));
Sankumarsingh
  • 9,889
  • 11
  • 50
  • 74
  • 1
    Don't load a File via an InputStream, it's slower and uses more memory! [Open it directly via the File instead](http://poi.apache.org/spreadsheet/quick-guide.html#FileInputStream), as described in the docs – Gagravarr Aug 15 '13 at 22:12
  • Gagravarr: You are right. But I have given answer in the same way the question was asked. @niks: The suggestion given by Gagravarr is realy useful. If you can change your code accordingly then please do that. For above problem, you just need to load workbook again with the same and updated file. – Sankumarsingh Aug 16 '13 at 03:26
  • Hi, i'm doing same thing but its not work for me – Nikesh Pathak Aug 16 '13 at 15:34
  • It should work. I have used it many times. Its strange for me why its not working on your end. Please share the code, what you have done. – Sankumarsingh Aug 16 '13 at 19:21
1

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

akokskis
  • 1,486
  • 15
  • 32