我有一个 docx 模板,我将其保存为 .xml,然后解析内容。然后我正在生成一个新的更新的 Word 文档。生成word文档后,我无法打开它。它说“文件损坏”。我按确定。然后它说“如果你想检索文档,请按 OK”。我按确定。然后我得到更新的文件。每次都会发生这种情况。我创建了与独立 java 应用程序相同的程序。通过独立 Java 应用程序生成的文档打开时没有任何错误。谁能让我对此有所了解?我也为服务器端使用了相同的代码。
这是我用来生成文档的代码。
try {
// Prepare the DOM document for writing
Source source = new DOMSource(doc);
// Prepare the output file
FileOutputStream file = new FileOutputStream(filename);
Result result = new StreamResult(file);
// Write the DOM document to the file
Transformer xformer = TransformerFactory.newInstance()
.newTransformer();
xformer.transform(source, result);
file.close();
} catch (TransformerConfigurationException e) {
System.out.println("Transformation Configuration Excepiton in WriteXMLFile");
} catch (TransformerException e) {
System.out.println("Transformation Excepiton in WriteXMLFile");
} catch (Exception e) {
System.out.println("Transformation Excepiton in WriteXMLFile");
e.printStackTrace();
}