poi生成word2007及以上文件
一、简介
对于poi来说,poi可以完成对word、excel、ppt的处理。word目前有两种文件格式,一种是doc后缀、另一种是docx后缀的。2007之前的版本都是doc后缀的,这种格式poi使用HWPF进行处理。HWPF也能有限多地对旧的word6和word95格式的文件处理提供支持。2007(包括)之后都是docx后缀的,poi使用XWPF进行处理。HWPF 和 XWPF的特性有些相似,但是目前两者没有共用接口。
HWPF和XWPF可以被描述为“适度功能”。对于一些例子来说,管理文本的提取,提供强有力的支持。对于其他莱斯,支持是有限或不完整的,需要深入研究低级别的代码。错误检测已经被移除,所有可能会创建格式错误的文件。
HWPF 包含在poi-scratchpad-XXX.jar包中,而XWPF 包含在 poi-ooxml-XXX.jar包中。我们可以根据我们的需要来将这些包添加都classpath里面。HWPF与XWPF网址为:http://poi.apache.org/document/index.html 。此网站为官网,我一般下载jar包所用网站为http://mvnrepository.com/
二、实例
1、依赖如下:
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.16</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-scratchpad -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>3.16</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.16</version>
</dependency>

2、示例代码如下:

1 package com.test.word;
2
3 import java.io.FileOutputStream;
4 import java.io.IOException;
5 import java.math.BigInteger;
6 import java.util.List;
7
8 import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
9 import org.apache.poi.xwpf.usermodel.TextAlignment;
10 import org.apache.poi.xwpf.usermodel.XWPFDocument;
11 import org.apache.poi.xwpf.usermodel.XWPFParagraph;
12 import org.apache.poi.xwpf.usermodel.XWPFRun;
13 import org.apache.poi.xwpf.usermodel.XWPFTable;
14 import org.apache.poi.xwpf.usermodel.XWPFTableCell;
15 import org.junit.Test;
16 import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr;
17 import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth;
18
19 /**
20 * 创建word文档
21 */
22 public class WordCreate {
23 /**
24 * 2007word文档创建
25 */
26 @Test
27 public void createWord2007() {
28 XWPFDocument doc = new XWPFDocument();
29 XWPFParagraph p1 = doc.createParagraph();
30
31 XWPFTable table = doc.createTable(11, 4);
32 // CTTblBorders borders=table.getCTTbl().getTblPr().addNewTblBorders();
33 CTTblPr tblPr = table.getCTTbl().getTblPr();
34 tblPr.getTblW().setType(STTblWidth.DXA);
35 tblPr.getTblW().setW(new BigInteger("7000"));
36
37 // 设置上下左右四个方向的距离,可以将表格撑大
38 table.setCellMargins(20, 20, 20, 20);
39
40 // 表格
41 List<XWPFTableCell> tableCells = table.getRow(0).getTableCells();
42
43 XWPFTableCell cell = tableCells.get(0);
44 XWPFParagraph newPara = new XWPFParagraph(cell.getCTTc().addNewP(), cell);
45 XWPFRun run = newPara.createRun();
46 /** 内容居中显示 **/
47 newPara.setAlignment(ParagraphAlignment.CENTER);
48 // run.getCTR().addNewRPr().addNewColor().setVal("FF0000");/**FF0000红色*/
49 // run.setUnderline(UnderlinePatterns.THICK);
50 run.setText("第一 数据");
51
52 tableCells.get(1).setText("第一 数据");
53 tableCells.get(2).setText("第一 据")
54 tableCells.get(3).setText("第 据");
55
56 tableCells = table.getRow(1).getTableCells();
57 tableCells.get(0).setText("第数据");
58 tableCells.get(1).setText("第一 数据");
59 tableCells.get(2).setText("第一 据");
60 tableCells.get(3).setText("第 据");
61
62 // 设置字体对齐方式
63 p1.setAlignment(ParagraphAlignment.CENTER);
64 p1.setVerticalAlignment(TextAlignment.TOP);
65
66 // 第一页要使用p1所定义的属性
67 XWPFRun r1 = p1.createRun();
68
69 // 设置字体是否加粗
70 r1.setBold(true);
71 r1.setFontSize(20);
72
73 // 设置使用何种字体
74 r1.setFontFamily("Courier");
75
76 // 设置上下两行之间的间距
77 r1.setTextPosition(20);
78 r1.setText("标题");
79
80 FileOutputStream out;
81 try {
82 out = new FileOutputStream("c:/test/word2007.docx");
83 // 以下代码可进行文件下载
84 // response.reset();
85 // response.setContentType("application/x-msdownloadoctet-stream;charset=utf-8");
86 // response.setHeader("Content-Disposition",
87 // "attachment;filename=\"" + URLEncoder.encode(fileName, "UTF-8"));
88 // OutputStream out = response.getOutputStream();
89 // this.doc.write(out);
90 // out.flush();
91
92 doc.write(out);
93 out.close();
94 } catch (IOException e) {
95 e.printStackTrace();
96 }
97 System.out.println("success");
98 }
99
100 }

3、生成word如下所示

poi生成word2007及以上文件的更多相关文章
- POI生成Web版Word文件
POI生成Web版Word文件 1 通过URL的输入流实现 2 直接把Html文本写入到Word文件 所谓的使用POI生成Web版Word文件是指利用POI将Html代码插入到 ...
- 【Java】使用Apache POI生成和解析Excel文件
概述 Excel是我们平时工作中比较常用的用于存储二维表数据的,JAVA也可以直接对Excel进行操作,分别有jxl和poi,2种方式. HSSF is the POI Project's pure ...
- POI生成EXCEL文件
POI生成EXCEL文件 一.背景 根据指定格式的JSON文件生成对应的excel文件,需求如下 支持多sheet 支持单元格合并 支持插入图片 支持单元格样式可定制 需要 标题(title),表头( ...
- 使用poi读取word2007(.docx)中的复杂表格
使用poi读取word2007(.docx)中的复杂表格 最近工作需要做一个读取word(.docx)中的表格,并以html形式输出.经过上网查询,使用了poi. 对于2007及之后的word文档,需 ...
- POI生成WORD文档
h2:first-child, body>h1:first-child, body>h1:first-child+h2, body>h3:first-child, body>h ...
- 黄聪:利用OpenXml生成Word2007文档(转)
原文:http://blog.csdn.net/francislaw/article/details/7568317 版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[-] 一Op ...
- 利用OpenXml生成Word2007文档
一.OpenXml简介 利用C#生成Word文档并非一定要利用OpenXml技术,至少可以使用微软提供的Office相关组件来编程,不过对于Office2007(确切的说是Word.Excel和Pow ...
- 使用POI生成Excel报表
先把报表模板截图贴上来 下面是POI编写的报表生成类ExcelReport.java package com.jadyer.report; import java.io.FileNotFoundExc ...
- 使用Java类库POI生成简易的Excel报表
使用Java类库POI生成简易的Excel报表 1.需求 1.数据库生成报表需要转义其中字段的信息.比如 1,有效 2.无效等 2.日期格式的自数据需要转义其格式. 3.标题的格式和数据的格式需要分别 ...
随机推荐
- crm高速开发之Entity
我们在后台代码里面操作Entity的时候,基本上是这样写的: /* 创建者:菜刀居士的博客 * 创建日期:2014年07月5号 */ namespace Net.CRM.Entity { ...
- 【翻译自mos文章】 在错误的从os级别remove掉 trace file 之后,怎么找到该trace file的内容?
在错误的从os级别remove掉 trace file 之后,怎么找到该trace file的内容? 參考原文: How to Find the Content of Trace File Gener ...
- 堆排序(Swift版本)
一:什么是堆? 堆可视为 "以数组方式存储的一棵完全二叉树" 堆又分为最大堆和最小堆, 最大堆就是对于整个二叉树中的每一个节点都满足:节点的键值比其左右子节点的键值都要大,对应的 ...
- IOC与DI区别
(1)IOC:控制反转,把对象创建交给spring进行配置. (2)DI:依赖注入,向类里面的属性中设置值. (3)关系:依赖注入不能单独存在,需要在IOC的基础之上完成操作.
- ubuntu16.04+caffe训练mnist数据集
1. caffe-master文件夹权限修改 下载的caffe源码编译的caffe-master文件夹貌似没有写入权限,输入以下命令修改: sudo chmod -R 777 ~/caffe-ma ...
- WebRTC开源项目一览之二
.Kurento视频直播系统4.1 应用实例搞视频会议就会涉及一对多.多对多.广播.转码.混音.合屏.录制,这就需要用到流媒体服务器,而kurento就具有这些功能.他主要用来作为webrtc的流媒 ...
- SQL Server 查询所有包含某文本的存储过程、视图、函数
• 方法一:查询所有包含某文本的存储过程.视图.函数 SELECT * from sysobjects o, syscomments s where o.id = s.id AND text LIK ...
- 构建工具系列二--Grunt
本文地址: http://www.cnblogs.com/blackmanba/p/frontend-scaffold-grunt.html或者http://forkme.info/frontend- ...
- iOS 内网内测应用发布
之前测试时,iOS 开发会把测试版本上传到蒲公英上,可以很方便的获取.后来认为不安全,万一测试版泄露了会有风险,就又回到了解放前,测试跑到开发那里编包.想过把手机越狱安装开发的编的 ipa 包,这样测 ...
- Oracle自制事务
数据库事务是一种单元操作,要么全部操作成功,要么全部失败.在Oracle中,一个事务是从执行第一个数据操作语言(DML)语句开始的,直到执行一个COMMIT语句,提交保存事务,或执行一个ROLLBAC ...