本文介绍了用".dotx"转换文件.扩展名(模板)为"docx" (Word文件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用POI API或Docx4j将".dotx" Word模板转换为普通的".docx"?

How to convert a ".dotx" Word template to a plain ".docx" using a POI APIs or Docx4j?

推荐答案

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

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.

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

这篇关于用".dotx"转换文件.扩展名(模板)为"docx" (Word文件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 19:21