https://issues.apache.org/bugzilla/show_bug.cgi?id=56745

--- Comment #2 from icear...@gmail.com ---
Using POI 3.11 nightly (2014-07-21), I get the same behavior as 3.10-FINAL.

    // open workbook
    String filename = "C:\\path\\to\\New Microsoft Excel Worksheet.xlsx";
    Workbook wb = new XSSFWorkbook(filename);

    // save workbook
    File file = new File(filename);  // output and input filename are the same
    FileOutputStream out = new FileOutputStream(file);
    wb.write(out);
    out.close();
    wb.close();

raises the following while trying to write to the FileOutputStream
Traceback (most recent call last):
  File "<input>", line 1, in <module>
POIXMLException: org.apache.poi.POIXMLException: java.io.IOException: Can't
obtain the input stream from /docProps/app.xml

It appears that if I read the file unbuffered (either via String or File), the
file becomes write protected after calling XSSFWorkbook(String) or
XSSFWorkbook(File). The only way to release the write protection after running
the above code is to kill the application.

Closing the workbook after reading it frees up the write protection (so the
file can be renamed in Windows Explorer or edited in another application),
however it causes a problem when trying to write the workbook.

    // open workbook
    String inFilename = "C:\\path\\to\\New Microsoft Excel Worksheet.xlsx";
    Workbook wb = new XSSFWorkbook(inFilename);
    wb.close();
    int sheetCount = wb.getNumberOfSheets(); //returns 3

    // save workbook
    String outFilename = "C:\\path\\to\\output.xlsx";
    File file = new File(outFilename);
    FileOutputStream out = new FileOutputStream(file);
    wb.write(out);
    out.close();
    wb.close();

raises the following exception while trying to write to the FileOutputStream.
Traceback (most recent call last):
  File "<input>", line 1, in <module>
OpenXML4JRuntimeException:
org.apache.poi.openxml4j.exceptions.OpenXML4JRuntimeException: Rule M2.4
exception : this error should NEVER happen, if so please send a mail to the
developers team, thanks !

It seems like the solution for this is to not read and write to the same file
(or at least while the workbook hasn't been closed). Maybe this was my
misunderstanding of the POI API, but it's possible there's something broken
that I've discovered. The problem with this code is that I must keep the file
locked for access from XSSFWorkbook(inFilename) to wb.close().

    // open workbook
    String inFilename = "C:\\path\\to\\New Microsoft Excel Worksheet.xlsx";
    wb = XSSFWorkbook(inFilename);

    // save workbook
    String outFilename = "C:\\path\\to\\output.xlsx";
    File file = new File(outFilename);
    FileOutputStream out = new FileOutputStream(file);
    wb.write(out);
    out.close();
    wb.close();

-- 
You are receiving this mail because:
You are the assignee for the bug.

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@poi.apache.org
For additional commands, e-mail: dev-h...@poi.apache.org

Reply via email to