POI生成WORD文档

2023-07-10,,

<!--
/* BLOCKS
=============================================================================*/

p, blockquote, ul, ol, dl, table, pre {
margin: 15px 0;
}

/* HEADERS
=============================================================================*/

h1, h2, h3, h4, h5, h6 {
margin: 20px 0 10px;
padding: 0;
font-weight: bold;
-webkit-font-smoothing: antialiased;
}

h1 tt, h1 code, h2 tt, h2 code, h3 tt, h3 code, h4 tt, h4 code, h5 tt, h5 code, h6 tt, h6 code {
font-size: inherit;
}

h1 {
font-size: 28px;
color: #000;
}

h2 {
font-size: 24px;
border-bottom: 1px solid #ccc;
color: #000;
}

h3 {
font-size: 18px;
}

h4 {
font-size: 16px;
}

h5 {
font-size: 14px;
}

h6 {
color: #777;
font-size: 14px;
}

body>-->h2:first-child, body>h1:first-child, body>h1:first-child+h2, body>h3:first-child, body>h4:first-child, body>h5:first-child, body>h6:first-child {
margin-top: 0;
padding-top: 0;
}

a:first-child h1, a:first-child h2, a:first-child h3, a:first-child h4, a:first-child h5, a:first-child h6 {
margin-top: 0;
padding-top: 0;
}

h1+p, h2+p, h3+p, h4+p, h5+p, h6+p {
margin-top: 10px;
}

/* LINKS
=============================================================================*/

a {
color: #4183C4;
text-decoration: none;
}

a:hover {
text-decoration: underline;
}

/* LISTS
=============================================================================*/

ul, ol {
padding-left: 30px;
}

ul li > :first-child,
ol li > :first-child,
ul li ul:first-of-type,
ol li ol:first-of-type,
ul li ol:first-of-type,
ol li ul:first-of-type {
margin-top: 0px;
}

ul ul, ul ol, ol ol, ol ul {
margin-bottom: 0;
}

dl {
padding: 0;
}

dl dt {
font-size: 14px;
font-weight: bold;
font-style: italic;
padding: 0;
margin: 15px 0 5px;
}

dl dt:first-child {
padding: 0;
}

dl dt>:first-child {
margin-top: 0px;
}

dl dt>:last-child {
margin-bottom: 0px;
}

dl dd {
margin: 0 0 15px;
padding: 0 15px;
}

dl dd>:first-child {
margin-top: 0px;
}

dl dd>:last-child {
margin-bottom: 0px;
}

/* CODE
=============================================================================*/

pre, code, tt {
font-size: 12px;
font-family: Consolas, "Liberation Mono", Courier, monospace;
}

code, tt {
margin: 0 0px;
padding: 0px 0px;
white-space: nowrap;
border: 1px solid #eaeaea;
background-color: #f8f8f8;
border-radius: 3px;
}

pre>code {
margin: 0;
padding: 0;
white-space: pre;
border: none;
background: transparent;
}

pre {
background-color: #f8f8f8;
border: 1px solid #ccc;
font-size: 13px;
line-height: 19px;
overflow: auto;
padding: 6px 10px;
border-radius: 3px;
}

pre code, pre tt {
background-color: transparent;
border: none;
}

kbd {
-moz-border-bottom-colors: none;
-moz-border-left-colors: none;
-moz-border-right-colors: none;
-moz-border-top-colors: none;
background-color: #DDDDDD;
background-image: linear-gradient(#F1F1F1, #DDDDDD);
background-repeat: repeat-x;
border-color: #DDDDDD #CCCCCC #CCCCCC #DDDDDD;
border-image: none;
border-radius: 2px 2px 2px 2px;
border-style: solid;
border-width: 1px;
font-family: "Helvetica Neue",Helvetica,Arial,sans-serif;
line-height: 10px;
padding: 1px 4px;
}

/* QUOTES
=============================================================================*/

blockquote {
border-left: 4px solid #DDD;
padding: 0 15px;
color: #777;
}

blockquote>:first-child {
margin-top: 0px;
}

blockquote>:last-child {
margin-bottom: 0px;
}

/* HORIZONTAL RULES
=============================================================================*/

hr {
clear: both;
margin: 15px 0;
height: 0px;
overflow: hidden;
border: none;
background: transparent;
border-bottom: 4px solid #ddd;
padding: 0;
}

/* TABLES
=============================================================================*/

/* IMAGES
=============================================================================*/

img {
max-width: 100%
}
-->
<!--
.highlight { background: #ffffff; }
.highlight .c { color: #999988; font-style: italic } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { font-weight: bold } /* Keyword */
.highlight .o { font-weight: bold } /* Operator */
.highlight .cm { color: #999988; font-style: italic } /* Comment.Multiline */
.highlight .cp { color: #999999; font-weight: bold } /* Comment.Preproc */
.highlight .c1 { color: #999988; font-style: italic } /* Comment.Single */
.highlight .cs { color: #999999; font-weight: bold; font-style: italic } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .gd .x { color: #000000; background-color: #ffaaaa } /* Generic.Deleted.Specific */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #999999 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .gi .x { color: #000000; background-color: #aaffaa } /* Generic.Inserted.Specific */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #aaaaaa } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { font-weight: bold } /* Keyword.Constant */
.highlight .kd { font-weight: bold } /* Keyword.Declaration */
.highlight .kp { font-weight: bold } /* Keyword.Pseudo */
.highlight .kr { font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #445588; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #009999 } /* Literal.Number */
.highlight .s { color: #d14 } /* Literal.String */
.highlight .na { color: #008080 } /* Name.Attribute */
.highlight .nb { color: #0086B3 } /* Name.Builtin */
.highlight .nc { color: #445588; font-weight: bold } /* Name.Class */
.highlight .no { color: #008080 } /* Name.Constant */
.highlight .ni { color: #800080 } /* Name.Entity */
.highlight .ne { color: #990000; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #990000; font-weight: bold } /* Name.Function */
.highlight .nn { color: #555555 } /* Name.Namespace */
.highlight .nt { color: #000080 } /* Name.Tag */
.highlight .nv { color: #008080 } /* Name.Variable */
.highlight .ow { font-weight: bold } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mf { color: #009999 } /* Literal.Number.Float */
.highlight .mh { color: #009999 } /* Literal.Number.Hex */
.highlight .mi { color: #009999 } /* Literal.Number.Integer */
.highlight .mo { color: #009999 } /* Literal.Number.Oct */
.highlight .sb { color: #d14 } /* Literal.String.Backtick */
.highlight .sc { color: #d14 } /* Literal.String.Char */
.highlight .sd { color: #d14 } /* Literal.String.Doc */
.highlight .s2 { color: #d14 } /* Literal.String.Double */
.highlight .se { color: #d14 } /* Literal.String.Escape */
.highlight .sh { color: #d14 } /* Literal.String.Heredoc */
.highlight .si { color: #d14 } /* Literal.String.Interpol */
.highlight .sx { color: #d14 } /* Literal.String.Other */
.highlight .sr { color: #009926 } /* Literal.String.Regex */
.highlight .s1 { color: #d14 } /* Literal.String.Single */
.highlight .ss { color: #990073 } /* Literal.String.Symbol */
.highlight .bp { color: #999999 } /* Name.Builtin.Pseudo */
.highlight .vc { color: #008080 } /* Name.Variable.Class */
.highlight .vg { color: #008080 } /* Name.Variable.Global */
.highlight .vi { color: #008080 } /* Name.Variable.Instance */
.highlight .il { color: #009999 } /* Literal.Number.Integer.Long */
.pl-c {
color: #969896;
}

.pl-c1,.pl-mdh,.pl-mm,.pl-mp,.pl-mr,.pl-s1 .pl-v,.pl-s3,.pl-sc,.pl-sv {
color: #0086b3;
}

.pl-e,.pl-en {
color: #795da3;
}

.pl-s1 .pl-s2,.pl-smi,.pl-smp,.pl-stj,.pl-vo,.pl-vpf {
color: #333;
}

.pl-ent {
color: #63a35c;
}

.pl-k,.pl-s,.pl-st {
color: #a71d5d;
}

.pl-pds,.pl-s1,.pl-s1 .pl-pse .pl-s2,.pl-sr,.pl-sr .pl-cce,.pl-sr .pl-sra,.pl-sr .pl-sre,.pl-src,.pl-v {
color: #df5000;
}

.pl-id {
color: #b52a1d;
}

.pl-ii {
background-color: #b52a1d;
color: #f8f8f8;
}

.pl-sr .pl-cce {
color: #63a35c;
font-weight: bold;
}

.pl-ml {
color: #693a17;
}

.pl-mh,.pl-mh .pl-en,.pl-ms {
color: #1d3e81;
font-weight: bold;
}

.pl-mq {
color: #008080;
}

.pl-mi {
color: #333;
font-style: italic;
}

.pl-mb {
color: #333;
font-weight: bold;
}

.pl-md,.pl-mdhf {
background-color: #ffecec;
color: #bd2c00;
}

.pl-mdht,.pl-mi1 {
background-color: #eaffea;
color: #55a532;
}

.pl-mdr {
color: #795da3;
font-weight: bold;
}

.pl-mo {
color: #1d3e81;
}
.task-list {
padding-left:10px;
margin-bottom:0;
}

.task-list li {
margin-left: 20px;
}

.task-list-item {
list-style-type:none;
padding-left:10px;
}

.task-list-item label {
font-weight:400;
}

.task-list-item.enabled label {
cursor:pointer;
}

.task-list-item+.task-list-item {
margin-top:3px;
}

.task-list-item-checkbox {
display:inline-block;
margin-left:-20px;
margin-right:3px;
vertical-align:1px;
}
-->

POI生成WORD文档

POI为Java系处理office文档的比较优秀的开源库,其中对于Excel的处理最为优秀,文档也写的很详细。不过很多网友都认为它在word文档处理方面就逊色很多,不过对于我本次的完成文档的生成我依然选择了POI。添加微信回复POI邀请你加群

需要完成功能

    配置Word模板文件,包括表格
    解析配置的Word文档,返回配置的特殊标记
    构造数据,替换配置的标签,以及生成表格

配置word模版

采用${xx}方式配置标签,如果是表格在对应一行一列配置表格名称

注意在word文档中,如果两个相近的字符样式不同,word默认会保存在不同的RUN元素中,由此很多朋友在配置好以后都需要保存为一个单独的文件,然后不把不在一起的标签合并到一个RUN元素中,如果文件比较大,我相信这绝对是一个比较痛苦的事情,这里将会侧重处理这个问题.我的解决方案是只保留第一RUN的样式其他的删掉

解析word模板

首先需要将文件转换为XWPFDocument对象,可以通过流的当时,也可以通过opcpackage,不过如果使用opcpackage打开的方式,打开的文件和最终生成的文件不能够是同一个文件,我这里采用文件流的方式

public XWPFDocument openDocument() {
XWPFDocument xdoc = null;
InputStream is = null;
try {
is = new FileInputStream(saveFile);
xdoc = new XWPFDocument(is);
} catch (IOException e) {
e.printStackTrace();
}
return xdoc;
}

获取非列表的标签,实现方式XWPFDocument对象有当前所有段落以及表格,这里暂不考虑表格嵌套表格的情况,每个段落的文本信息是可以通过p.getText()获取,获取段落中文档配置信息如下:

   // 获取段落集合中所有文本
public List<TagInfo> getWordTag(XWPFDocument doc, String regex) {
List<TagInfo> tags = new ArrayList<TagInfo>();
// 普通段落
List<XWPFParagraph> pars = doc.getParagraphs();
for (int i = 0; i < pars.size(); i++) {
XWPFParagraph p = pars.get(i);
setTagInfoList(tags, p, regex);
}
// Table中段落
List<XWPFTable> commTables = getDocTables(doc, false, regex);
for (XWPFTable table : commTables) {
List<XWPFParagraph> tparags = getTableParagraph(table);
for (int i = 0; i < tparags.size(); i++) {
XWPFParagraph p = tparags.get(i);
setTagInfoList(tags, p, regex);
}
}
return tags;
}

获取文本后通过正则解析,并依次保存到TagInfo中

// 向 taglist中添加新解析的段落信息
private void setTagInfoList(List<TagInfo> list, XWPFParagraph p,
String regex) {
if (regex == "")
regex = defaultRegex;
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(p.getText());
int startPosition = 0;
while (matcher.find(startPosition)) {
String match = matcher.group();
if (!list.contains(new TagInfo(match, match, ""))) {
list.add(new TagInfo(match, match, ""));
}
startPosition = matcher.end();
}
}

解析表格

    // 获取Table列表中的配置信息
public Map<String, List<List<TagInfo>>> getTableTag(XWPFDocument doc,
String regex) {
Map<String, List<List<TagInfo>>> mapList = new HashMap<String, List<List<TagInfo>>>();
List<XWPFTable> lstTables = getDocTables(doc, true, regex);
for (XWPFTable table : lstTables) {
// 获取每个表格第一个单元格,以及最后一行
String strTableName = getTableListName(table, regex);
List<List<TagInfo>> list = new ArrayList<List<TagInfo>>();
List<TagInfo> lstTag = new ArrayList<TagInfo>();
int rowSize = table.getRows().size();
XWPFTableRow lastRow = table.getRow(rowSize - 1);
for (XWPFTableCell cell : lastRow.getTableCells()) {
for (XWPFParagraph p : cell.getParagraphs()) {
// 去掉空白字符串
if (p.getText() != null && p.getText().length() > 0) {
setTagInfoList(lstTag, p, regex);
}
}
}
list.add(lstTag);
// 添加到数据集
mapList.put(strTableName, list);
}
return mapList;
}

生成WORD文档

难点替换标签
传入数据格式包含三个formtag以及一个tableTag

{"formTags":
[{"TagName":"${xxxx}","TagText":"${xxxx}","TagValue":""},
{"TagName":"${123}","TagText":"${123}","TagValue":""},
{"TagName":"${ddd}","TagText":"${ddd}","TagValue":""}],
"tableTags":{
"${table}":[
[{"TagName":"${COL1}","TagText":"${COL1}","TagValue":""},{"TagName":"${COL2}","TagText":"${COL2}","TagValue":""}]
]}
}

普通文档生成,并且保留配置样式,这里主要使用POI中提供searchText方法,返回Tag所有所在的RUN标签,通过一个字符做比较,如果找的第一个匹配的文本开始计数,所有在当前条件下类型 $${xxx}这样的标签是无法实现替换的
替换普通文本Tag

    public void ReplaceInParagraph(List<TagInfo> tagList, XWPFParagraph para,
String regex) {
if (regex == "")
regex = defaultRegex;
List<XWPFRun> runs = para.getRuns();
for (TagInfo ti : tagList) {
String find = ti.TagText;
String replValue = ti.TagValue;
TextSegement found = para.searchText(find,
new PositionInParagraph());
if (found != null) {
// 判断查找内容是否在同一个Run标签中
if (found.getBeginRun() == found.getEndRun()) {
XWPFRun run = runs.get(found.getBeginRun());
String runText = run.getText(run.getTextPosition());
String replaced = runText.replace(find, replValue);
run.setText(replaced, 0);
} else {
// 存在多个Run标签
StringBuilder sb = new StringBuilder();
for (int runPos = found.getBeginRun(); runPos <= found
.getEndRun(); runPos++) {
XWPFRun run = runs.get(runPos);
sb.append(run.getText((run.getTextPosition())));
}
String connectedRuns = sb.toString();
String replaced = connectedRuns.replace(find, replValue);
XWPFRun firstRun = runs.get(found.getBeginRun());
firstRun.setText(replaced, 0);
// 删除后边的run标签
for (int runPos = found.getBeginRun() + 1; runPos <= found
.getEndRun(); runPos++) {
// 清空其他标签内容
XWPFRun partNext = runs.get(runPos);
partNext.setText("", 0);
}
}
}
}
// 完成第一遍查找,检测段落中的标签是否已经替换完
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(para.getText());
boolean find = matcher.find();
if (find) {
ReplaceInParagraph(tagList, para, regex);
find = false;
}
}

表格主要是通过复制模版行,然后对模版行中的内容做修改
复制文本标签RUN

    private void CopyRun(XWPFRun target, XWPFRun source) {
target.getCTR().setRPr(source.getCTR().getRPr());
// 设置文本
target.setText(source.text());
}

复制段落XWPFParagraph

    private void copyParagraph(XWPFParagraph target, XWPFParagraph source) {
// 设置段落样式
target.getCTP().setPPr(source.getCTP().getPPr());
// 添加Run标签
for (int pos = 0; pos < target.getRuns().size(); pos++) {
target.removeRun(pos);
}
for (XWPFRun s : source.getRuns()) {
XWPFRun targetrun = target.createRun();
CopyRun(targetrun, s);
}
}

复制单元格XWPFTableCell

    private void copyTableCell(XWPFTableCell target, XWPFTableCell source) {
// 列属性
target.getCTTc().setTcPr(source.getCTTc().getTcPr());
// 删除目标 targetCell 所有单元格
for (int pos = 0; pos < target.getParagraphs().size(); pos++) {
target.removeParagraph(pos);
}
// 添加段落
for (XWPFParagraph sp : source.getParagraphs()) {
XWPFParagraph targetP = target.addParagraph();
copyParagraph(targetP, sp);
}
}

复制行XWPFTableRow

    private void CopytTableRow(XWPFTableRow target, XWPFTableRow source) {
// 复制样式
target.getCtRow().setTrPr(source.getCtRow().getTrPr());
// 复制单元格
for (int i = 0; i < target.getTableCells().size(); i++) {
copyTableCell(target.getCell(i), source.getCell(i));
}
}

以上就完成所有功能更,只要你配置规范,可以完全原样输出模版内容。这里特别感谢下肖哥哥大力支持。

其次,java的编码真的让人很无语,get或post时中文各种乱码

POI生成WORD文档的相关教程结束。

《POI生成WORD文档.doc》

下载本文的Word格式文档,以方便收藏与打印。