无涯教程-POI Word - 创建文档

80 阅读1分钟

要创建新的Microsoft Word文件,Apache POI提供了 XWPFDocument 类。此类在Java程序中用于处理word文档。

请参见下面的示例,无涯教程将创建一个单词文件Learnfk.docx。

Apache POI创建MS Word示例

package poiexample;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
public class CreateWord {
	public static void main(String[] args) throws FileNotFoundException, IOException {
		XWPFDocument document = new XWPFDocument();
	    try(OutputStream fileOut = new FileOutputStream("Learnfk.docx")) {
	    	document.write(fileOut);
	    	System.out.println("File created");
	    }catch(Exception e) {
	    	System.out.println(e.getMessage());
	    }
	}
}

输出:

This example creates a .doc extension file having name Learnfk.docx.

参考链接

www.learnfk.com/apache-poi-…