Apache POI:如何在 Java 中创建 Word 文件
Apache POI API 提供了使用 Java 创建新 Word 文档和打开现有文件的功能。本教程以最简单的方式说明如何下载和使用此 API 创建 Word.
本教程是一个介绍性教程,仅演示如何下载和创建一个空的 Word 文件。我建议您阅读本教程:在 java其中解释了如何添加 text.
XWPFDocument
org.apache.poi.xwpf.usermodel 包中的此类用于创建具有 .docx 扩展名的新 MS-Word 文档。下载 Apache poi API。将以下文件导入到项目的类路径中:- poi.jar
- poi-ooxml.jar
- poi-ooxml-schemas.jar
- ooxml-lib\dom4j.jar
- ooxml-lib\xmlbeans.jar
import java.io.File;Run:
import java.io.FileOutputStream;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
public class CreateDocument
{
public static void main(String[] args)throws Exception
{
//Word 文档 blank
XWPFDocument document= new XWPFDocument();
//通过给文档起个名字,在特定路径中创建文档
File nv_fichier = new File(nouveauFichier.docx”);
FileOutputStream out = new FileOutputStream(nv_fichier);
document.write(out);
out.close();
System.out.println(文档 ”+ nv_fichier + 已成功创建”);
}
}
createdocument.docx文档创建成功运行时间表示Word文件创建成功。Java 编译并运行此程序以生成一个名为nouveauFichier.docx跨度> 在您指定的路径中。在我们的例子中,这是我们项目的当前目录。
本教程是一个介绍性教程,仅演示如何下载和创建一个空的 Word 文件。我建议您阅读本教程:在 java其中解释了如何添加 text.