codecamp

Apache POI Word - 文件

这里术语“document"是指MS-Word文件。 阅读完本章后,您将能够使用Java程序创建新文档并打开现有文档。

创建空白文档

以下简单程序用于创建空白MS-Word文档:

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

public class CreateDocument 
{
   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("createdocument.docx"));
   document.write(out);
   out.close();
   System.out.println(
   "createdocument.docx written successully");
   }
}

将上述Java代码保存为CreateDocument.java,然后从命令提示符处编译并执行它,如下所示:

$javac  CreateDocument.java
$java   CreateDocument

如果您的系统环境配置有POI库,它将编译并执行,以在当前目录中生成名为createdocument.docx的空Excel文件,并在命令提示符中显示以下输出:

createdocument.docx written successfully

Apache POI Word - 核心类
Apache POI Word - 段落
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }