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

            Bug ID: 56468
           Summary: Writing a workbook more than once corrupts the file
           Product: POI
           Version: unspecified
          Hardware: PC
            Status: NEW
          Severity: major
          Priority: P2
         Component: XSSF
          Assignee: [email protected]
          Reporter: [email protected]

Created attachment 31567
  --> https://issues.apache.org/bugzilla/attachment.cgi?id=31567&action=edit
The corrupt file.

The second time a file is written the "docProps/core.xml" file is not included
in the output and the result is a corrupt Excel file that cannot be opened.

This bug may be related :
https://issues.apache.org/bugzilla/show_bug.cgi?id=55033

I debugged this in 3.9 and confirmed it is still present in 3.10. Windows 7.
Being new to the code base I could only track the bug as far as the following
code in "saveImpl" of "ZipPackage.java". It executes on the first run but not
the second.

...
            // If the core properties part does not exist in the part list,
            // we save it as well
            if
(this.getPartsByRelationshipType(PackageRelationshipTypes.CORE_PROPERTIES).size()
== 0 &&
               
this.getPartsByRelationshipType(PackageRelationshipTypes.CORE_PROPERTIES_ECMA376).size()
== 0    ) {
                logger.log(POILogger.DEBUG,"Save core properties part");

                // We have to save the core properties part ...
                new ZipPackagePropertiesMarshaller().marshall(
                        this.packageProperties, zos);
                // ... and to add its relationship ...
                this.relationships.addRelationship(this.packageProperties
                        .getPartName().getURI(), TargetMode.INTERNAL,
                        PackageRelationshipTypes.CORE_PROPERTIES, null);
                // ... and the content if it has not been added yet.
                if (!this.contentTypeManager
                       
.isContentTypeRegister(ContentTypes.CORE_PROPERTIES_PART)) {
                    this.contentTypeManager.addContentType(
                            this.packageProperties.getPartName(),
                            ContentTypes.CORE_PROPERTIES_PART);
                }
            }
...

Test code that replicates the error ("test2.xlsx" is corrupt) :

    public static void main(String[] args) throws FileNotFoundException,
IOException
    {
        XSSFWorkbook wb = new XSSFWorkbook();
        XSSFSheet sheet = wb.createSheet();
        XSSFRow row = sheet.createRow(0);
        XSSFCell cell = row.createCell(0);
        cell.setCellValue("Hi");

        File f = new File("C:\\temp\\test.xlsx");
        try(FileOutputStream fout = new FileOutputStream(f))
        {
            wb.write(fout);
        }

        f = new File("C:\\temp\\test2.xlsx");
        try(FileOutputStream fout = new FileOutputStream(f))
        {
            wb.write(fout);
        }
    }

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

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to