将扩展名为“.dotx”的文件(模板)转换为 "docx"(Word 文件)

Converting a file with ".dotx" extension (template) to "docx" (Word File)

如何使用 POI API 或 Docx4j 将“.dotx”Word 模板转换为纯“.docx”?

需要将 /word/document.xml 的内容类型从 application/vnd.openxmlformats-officedocument.wordprocessingml.template.main+xml 更改为 application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml

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