如何使用 Java 创建空白 Word 文档



问题描述

如何使用 Java 创建空白 Word 文档。

解决方案

以下为使用 Java 创建空白 Word 文档的程序。

import java.io.File;
import java.io.FileOutputStream;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

public class CreateWordDocument {
   public static void main(String[] args)throws Exception {
      
      //Blank Document
      XWPFDocument document = new XWPFDocument();

      //Write the Document in file system
      FileOutputStream out = new FileOutputStream (
         new File("C:/poiword/createdocument.docx"));
      
      document.write(out);
      out.close();
      
      System.out.println("createdocument.docx written successully");
   }
}

输出

Blank Word
java_apache_poi_word
广告
© . All rights reserved.