博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java 通过使用poi hpsf 生成2003 word 文档(java生成word文档1)
阅读量:6582 次
发布时间:2019-06-24

本文共 2328 字,大约阅读时间需要 7 分钟。

hot3.png

 

import java.io.*;import org.apache.poi.hpsf.CustomProperties;import org.apache.poi.hpsf.DocumentSummaryInformation;import org.apache.poi.hwpf.HWPFDocument;import org.apache.poi.hwpf.usermodel.*;import org.apache.poi.poifs.filesystem.POIFSFileSystem;public class CreateWordDoc {    public static void main (String[] args) throws Exception {        // POI apparently can't create a document from scratch,        // so we need an existing empty dummy document        POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream("empty.doc"));        HWPFDocument doc = new HWPFDocument(fs);        // centered paragraph with large font size        Range range = doc.getRange();        Paragraph par1 = range.insertAfter(new ParagraphProperties(), 0);        par1.setSpacingAfter(200);        par1.setJustification((byte) 1);        // justification: 0=left, 1=center, 2=right, 3=left and right        CharacterRun run1 = par1.insertAfter("one");        run1.setFontSize(2 * 18);        // font size: twice the point size        // paragraph with bold typeface        Paragraph par2 = run1.insertAfter(new ParagraphProperties(), 0);        par2.setSpacingAfter(200);        CharacterRun run2 = par2.insertAfter("two two two two two two two two two two two two two");        run2.setBold(true);        // paragraph with italic typeface and a line indent in the first line        Paragraph par3 = run2.insertAfter(new ParagraphProperties(), 0);        par3.setFirstLineIndent(200);        par3.setSpacingAfter(200);        CharacterRun run3 = par3.insertAfter("three three three three three three three three three "            + "three three three three three three three three three three three three three three "            + "three three three three three three three three three three three three three three");        run3.setItalic(true);        // add a custom document property (needs POI 3.5; POI 3.2 doesn't save custom properties)        DocumentSummaryInformation dsi = doc.getDocumentSummaryInformation();        CustomProperties cp = dsi.getCustomProperties();        if (cp == null)            cp = new CustomProperties();        cp.put("myProperty", "foo bar baz");        dsi.setCustomProperties(cp);        doc.write(new FileOutputStream("new-hwpf-file.doc"));    }}

转载于:https://my.oschina.net/u/555639/blog/226962

你可能感兴趣的文章
边缘计算赋能数字化新模式,施耐德电气再度领跑行业
查看>>
如何清空sql server sa密码
查看>>
DOM——拷贝.clone()与替换.replaceWith() 和.replaceAll()及包裹.wrap()
查看>>
硬链接与软链接简单说明
查看>>
MySQL查看和修改表的存储引擎
查看>>
解决缺少sql头文件编译错误
查看>>
Docker镜像和容器
查看>>
CentOS系统启动流程
查看>>
分布式数据库之事务隔离性
查看>>
mysql总结3→单表查询1
查看>>
ADO判断ACCESS字段值是否为NULL
查看>>
教你打包自己的专属docker镜像
查看>>
MySQL主从多种架构部署及常见错误问题解析
查看>>
关于负载均衡的几个知识点
查看>>
使用xtrabackup备份innodb引擎的数据库
查看>>
jQuery EasyUI DataGrid动态合并单元格解决方案
查看>>
V 2 heartbeat V2(HA)
查看>>
程序员回家过年生存指南
查看>>
Vim应用
查看>>
esxi安装
查看>>