How to convert a ".dotx" Word template to a plain ".docx" using a POI APIs or Docx4j?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
The need is changing the content type of /word/document.xml
from application/vnd.openxmlformats-officedocument.wordprocessingml.template.main+xml
to application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml
.
Example using apache poi 4.0.1
:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.poi.xwpf.usermodel.*;
public class WordReadDOTXSaveDOCX {
public static void main(String[] args) throws Exception {
XWPFDocument document = new XWPFDocument(new FileInputStream("StudentReport.dotx"));
document.getPackage().replaceContentType(
"application/vnd.openxmlformats-officedocument.wordprocessingml.template.main+xml",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml");
FileOutputStream out = new FileOutputStream("TheDocumentFromDOTXTemplate.docx");
document.write(out);
out.close();
document.close();
}
}