要创建新的Microsoft Word文件,Apache POI提供了 XWPFDocument 类。此类在Java程序中用于处理word文档。
请参见下面的示例,无涯教程将创建一个单词文件Learnfk.docx。
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.
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)