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

发布于 2021-01-30 16:41:24

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

关注者
0
被浏览
84
1 个回答
  • 面试哥
    面试哥 2021-01-30
    为面试而生,有面试问题,就找面试哥。

    需要将内容类型/word/document.xml从更改application/vnd.openxmlformats- officedocument.wordprocessingml.template.main+xmlapplication/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();
     }
    }
    


知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看