Converting a file with “.dotx” extension (template) to “docx” (Word File) [closed]

Deadly 提交于 2019-12-03 00:27:14

问题


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();
 }
}


来源:https://stackoverflow.com/questions/54377200/converting-a-file-with-dotx-extension-template-to-docx-word-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!