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

I’ve had the same problem today. I’ve noticed many people asking the same question on many different forums, but I haven’t seen an answer anywhere. So, here is what I came up with. It is far from ideal (I can think of at least two scenarios where this might be a bad idea), and may not suite every need, but it works!

After every save operation inside the class which the workbook object is a property of, I reload the workbook from the file I just saved it to.

Using your code example above, I would modify the method like so:

void method(int i) throws InvalidFormatException, IOException {
    ...

    // Write the output to a file
    FileOutputStream fileOut = new FileOutputStream("workbook.xlsx");
    workbook.write(fileOut);
    fileOut.close();

    // Reload the workbook, workaround for bug 49940
    // https://issues.apache.org/bugzilla/show_bug.cgi?id=49940
    workbook = new XSSFWorkbook(new FileInputStream("workbook.xlsx"));
}

I tested this in my code, and it resolved the issue nicely. Just make sure that you read it back in from the same file you saved it to, and not an earlier or different version.

Leave a Comment