tanvi
tanvi

Reputation: 997

Apache poi file getting corrupted and unable to write to existing workbook

I am trying to write to read and write to a workbook using the following code:

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();
        }
    }
}

When I run the code for the first time, I get this exception at workbook.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

And after this, the workbook gets corrupted and I reduced to 0kb and I get this exception on WorkbookFactory.create():

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)

Where and how should I use the FileOutputStream, workbook.write() and should I also be using FileInputStream even though I am using WorkbookFactory?

------------EDIT----------------------I got my code working I used FileInputStream instead of WorkbookFactory to create the workbook and closed it after closing the FileOutputStream. That worked.

Upvotes: 2

Views: 10937

Answers (2)

Gagravarr
Gagravarr

Reputation: 48326

I think I've spotted your problem:

workbook = WorkbookFactory.create(new File(absoluteFilePath));

....

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

You're opening a file, then trying to overwrite it later while it's still open and in use, which isn't supported

What you'll need to do is write the updated file out to a different filename. If you want to replace it, that needs to be a second step after you close the original file.

As it stands, you've started to overwrite the file, then POI tries to copy over some parts of the original to the new file, and fails because the original file it's trying to read from has been zapped!

(NPOIFSFileSystem and OPCPackage, the underlying package code, both support in-place writes and in-place updates, but the higher level code like HSSFWorkbook / XSSFWorkbook / XWPFDocument only support writing out to a new file, and there's not been enough community interest in in-place write to work on adding the support)

Upvotes: 4

user207421
user207421

Reputation: 310840

Where and how should I use the FileOutputStream, workbook.write()

Not in the finally block. If you got any kind of an exception you surely don't want to write possible rubbish into the file. workbook could even be null in the finally block.

Move that code to the end of the try block.

Upvotes: 0

Related Questions