0

We are dealing with an exception when trying to create a create an encrypted xlsx file from an unencrypted one.

Caused by: org.apache.poi.openxml4j.exceptions.OpenXML4JException: The part /xl/styles.xml failed to be saved in the stream with marshaller org.apache.poi.openxml4j.opc.internal.marshallers.DefaultMarshaller@852ef8d
at org.apache.poi.openxml4j.opc.ZipPackage.saveImpl(ZipPackage.java:543) ~[poi-ooxml-4.1.2.jar:4.1.2]

I have read around that it is because one of the files involved is open more than once. That is not the case. There are say, 2 parts to the process. There's one that creates an xlsx file, then there's another that kicks in to encrypt it. I have split them apart them so that I could test them one after the other separately. Run first step, I create the xlsx file, unit test finishes, I don't run anything else. Then I run the second step on a separate test session (to make sure that there's nothing that is holding the previous file open) and I get that exception.

eftshift0
  • 26,375
  • 3
  • 36
  • 60
  • 2
    See https://stackoverflow.com/questions/62067539/styles-xml-breaking-password-protected-xssfworkbook-apache-poi-v3-16-save-proc – Axel Richter Jun 18 '20 at 12:38
  • This is interesting, but we are adding all values as strings. – eftshift0 Jun 18 '20 at 12:57
  • Actually, this didn't point to the solution.... but it did give me a big hint of what was going on. i was creating way too many styles unnecessarily. Which begs the question: Why does it work fine creating a normal xlsx file but it fails when encrypting it with the same amount of information in `/xl/styles.xml`? – eftshift0 Jun 18 '20 at 13:36
  • Maybe Excel has stricter validation rules applied when loading password protected content? – Gagravarr Jun 19 '20 at 05:21

1 Answers1

0

Ok.... there are two parts to the problem.

First, the exception happens when encrypting because the /xl/styles.xml is too big. Encryption is performed on a valid xlsx file, but it does have a very big styles.xml file.

Second: I could skip the problem by reusing styles instead of creating new ones for the cells that i create on the first file. Then encryption doesn't fail.... but this begs the question: why does encryption fail if the original file is created fine?

Thanks go to @AxelRichter for pointing in the right direction.

eftshift0
  • 26,375
  • 3
  • 36
  • 60
  • I don't think it is because the `/xl/styles.xml` is too big but because the `/xl/styles.xml` contains thousands of times the same text. A `*.xlsx` is a `ZIP` archive which compresses its content. And `XML` which contains thousands of times the same text gets highly compressed. Seems as if this high compression is in conflict with the encryption process then. But that's only a guess. – Axel Richter Jun 18 '20 at 13:59
  • But of course one should not storing unnecessary styles as `Excel` itself also has limits for count of styles. See [Excel specifications and limits](https://support.microsoft.com/en-us/office/excel-specifications-and-limits-1672b34d-7043-467e-8e27-269d656771c3?ui=en-us&rs=en-us&ad=us). – Axel Richter Jun 18 '20 at 13:59
  • I agree.... it was an exception waiting to propagate. – eftshift0 Jun 18 '20 at 14:00
  • @eftshift0 I've fixed the mmap handling in [#1877144](https://svn.apache.org/viewvc?view=revision&revision=1877144) because of a [similar issue](https://stackoverflow.com/questions/61463301) - is your original code working with the current trunk? – kiwiwings Jun 19 '20 at 16:41
  • That's great! Using 4.1.2. The problem has been fixed for me by reusing styles.... but I'm glad it's fixed, anyway. – eftshift0 Jun 19 '20 at 16:47