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