Page 1 of 1

Spreadsheet changes after load/save

PostPosted: Tue Jan 29, 2019 5:15 pm
by LarsD
Hi.

Since some weeks I trying to load, manipulate and save a xlsx file with docx4j.

In most cases, after saving to file and open it with MS Excel the Excel App says it is broken and needs to be fixed. After fixing the sheet looks like it should.

After trying lots of thing I can't fix the following issue:
Code: Select all
...

final File file = excelPath.toFile();

SpreadsheetMLPackage loadedFile = SpreadsheetMLPackage.load(file);

final WorksheetPart sheet = loadedFile.getWorkbookPart()
               .getWorksheet(0);

final Worksheet ws = sheet.getContents();

final SheetData data = ws.getSheetData();

loadedFile.save(newPath.toFile());

...


You are rigth, this code does absolutely nothing. It only loads and saves an Excel file.

The result is kind of strange.
The original file has a size of 1.819 KB.
The new file has a size of 1.536 KB.

I think the problem is related to this peace of code in org.docx4j.openpackaging.parts.JaxbXmlPart<E>:
Code: Select all
...
    XMLOutputFactory xof = XMLOutputFactory.newFactory();
    XMLStreamWriter xsw = xof.createXMLStreamWriter(os, "UTF-8");
              
   // get rid of xmlns="" which com.sun.xml.internal.stream.writers.XMLStreamWriterImpl writes
   XMLStreamWriterWrapper xsww = new XMLStreamWriterWrapper(this, xsw);

   marshaller.setListener(new Docx4jMarshallerListener(xsww));
   marshaller.marshal(jaxbElement, xsww);
   xsww.close();
   xsw.close();
...


The log says:
Code: Select all
DEBUG Save - .. saving
DEBUG ZipPartStore - marshalling /xl/worksheets/sheet1.xml
DEBUG NamespacePrefixMapperUtils - attempting to setProperty on marshaller com.sun.xml.internal.bind.v2.runtime.MarshallerImpl
DEBUG NamespacePrefixMapperUtils - attempting to setProperty: com.sun.xml.INTERNAL.bind.namespacePrefixMapper
DEBUG JaxbXmlPart - marshalling org.docx4j.openpackaging.parts.SpreadsheetML.WorksheetPart
DEBUG XMLStreamWriterWrapper - writeStartDocument
DEBUG XMLStreamWriterWrapper - writeStartDocument UTF-8
DEBUG XMLStreamWriterWrapper - Writing ns6=http://schemas.microsoft.com/office/excel/2008/2/main
DEBUG XMLStreamWriterWrapper - Writing ns5=http://schemas.microsoft.com/office/excel/2006/main
DEBUG XMLStreamWriterWrapper - Writing r=http://schemas.openxmlformats.org/officeDocument/2006/relationships
DEBUG XMLStreamWriterWrapper - Writing xdr=http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing
DEBUG XMLStreamWriterWrapper - Writing ns2=http://schemas.openxmlformats.org/spreadsheetml/2006/main
...


These new namespaces are the only difference between these two files and I think they break the excel file.

Is it possible to disable this?


kind regards
Lars

Re: Spreadsheet changes after load/save

PostPosted: Tue Jan 29, 2019 6:06 pm
by jason
Could you please attach an xlsx I can load/save to see the issue?

Re: Spreadsheet changes after load/save

PostPosted: Tue Jan 29, 2019 6:11 pm
by LarsD
Thank you for the quick response. :)


Before:
https://ufile.io/ux5yj


After:
https://ufile.io/35i1x


kind regards
Lars

Re: Spreadsheet changes after load/save

PostPosted: Wed Jan 30, 2019 7:46 am
by jason
What version of docx4j are you using?

Load/save works for me with docx4j 6.1.1 (6.1.0 would be the same), then opening in Excel 2016.

(I can confirm your after.xlsx doesn't open in Excel 2016)

Re: Spreadsheet changes after load/save

PostPosted: Wed Jan 30, 2019 9:01 pm
by LarsD
It was 6.0.1.

I tried with 6.1.1 now. It has the same issue, it only changes a little bit in the output file.

The input is 1.819 KB.
The output is 1.520 KB.

btw: Java 8.

kind regards
Lars

Re: Spreadsheet changes after load/save

PostPosted: Thu Jan 31, 2019 6:46 pm
by LarsD
I check the xml..

Both fils has UTF-8 encoding but the original file ha "PC" standard and no line breaks,.. The generated file has "UNIX" and line breaks.. that may forces the problems.

Is there a way to force the marshalling to keep the original encoding.


Lars

Re: Spreadsheet changes after load/save

PostPosted: Sat Feb 02, 2019 7:41 am
by jason
Hi Lars, Which Java 8, exactly? java -version

Could you please attach your classpath?

Are you testing in an IDE or an app server? Which one :-)

Re: Spreadsheet changes after load/save

PostPosted: Mon Feb 04, 2019 6:08 pm
by LarsD
I sended an eMail to you with this information.

Lars