需要将内容类型
/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(); }}


