0

I have a source src Excel file to read, and a destination dst file to overwrite for edited changes after reading src. With k=true, the following code fails with POIXMLException. With k=false, both files are written.

Exception in thread "main" org.apache.poi.ooxml.POIXMLException: 
java.io.EOFException: Unexpected end of ZLIB input stream
    at org.apache.poi.ooxml.POIXMLDocument.getProperties(POIXMLDocument.java:146)
    at org.apache.poi.ooxml.POIXMLDocument.write(POIXMLDocument.java:225)
    at Test.main(Test.java:26)

What should I do to keep intact the 'src' file?, hopefully not changing the ´Worksheet´ and ´Sheet´ classes.

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

public class Test {
    static Workbook wb;
    static Sheet sh;
    public static void main(String[] args) throws Exception {
        Files.copy(Paths.get("src\\src.xlsx"),Paths.get("src\\dst.xlsx"),
                StandardCopyOption.REPLACE_EXISTING);
        File fl; boolean k=true;
        if (k)
            fl= new File("src\\src.xlsx"); //Fails
        else
            fl = new File("src\\dst.xlsx"); //Do Not Fails
        wb = WorkbookFactory.create(fl);
        sh = wb.getSheetAt(0);
        Row row = sh.getRow(1);
        row.getCell(1).setCellValue("Test");
        OutputStream fos = new FileOutputStream("src\\src.xlsx");
        wb.write(fos); // [FAILS]
        wb.close();
    }
}
Brethlosze
  • 1,533
  • 1
  • 22
  • 41

0 Answers0