大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。
Jetbrains全系列IDE稳定放心使用
废话不多,直接上码
一、说明
1、jdk1.8
2、springboot项目
3、测试包下目录,如下图:
4、模板文件如下:
html2wordtemplate.docx –> https://download.csdn.net/download/wdy_2099/19686795
5、html如下:
2021年6月10日,重庆QGhappy和佛山GK展开败者组第二轮对决,最终重庆QG4:3佛山GK拿下胜利。
重庆QGhappy确认晋级2021年KPL春季赛季后赛败者组·第三轮,佛山GK遗憾告别2021年KPL春季赛舞台。
<br/><img src="http://inews.gtimg.com/newsapp_bt/0/13638266215/641" style="max-width:100%;" width="50%"/><br/><b>图2 学校举办2020年全面从严治党工作会议暨中层干部培训会</b><br/>
二连击破,穿三无惧!
山城弟子无惧挑战,一穿五第二步完成!目标银龙还剩三步,败者组第三轮,我们一起期待重庆QG的精彩表现!
<br/><img src="http://inews.gtimg.com/newsapp_bt/0/13638266213/641" style="max-width:100%;" width="50%"/><br/><b>图2 学校举办2020年全面从严治党工作会议暨中层干部培训会</b><br/>
春之GK暂别赛场,夏日定要打破魔咒突破自己!
不止要有巅峰手法,更要有重头再来的勇气!2021年王者荣耀世界冠军杯选拔赛,我们一起期待夏日的佛山GK秀翻全场!
集合!新十代!
2021年KPL王者荣耀职业联赛春季赛季后赛6月3日-6月20日每日19点准时开播,召唤师们可通过王者荣耀游戏内赛事专区、王者营地、王者荣耀官网观看直播;还可以登录官方播出平台哔哩哔哩、斗鱼直播、虎牙直播、快手、企鹅电竞、腾讯体育、腾讯视频、腾讯微视收看全程比赛直播和回放,电视大屏观赛用哒啵电竞、云视听极光!
二、核心pom引入
【注意:】依赖的版本,不能乱改,可能会有冲突。
<properties>
<poi.version>4.1.2</poi.version>
<hutool.version>4.6.10</hutool.version>
<guava.version>20.0</guava.version>
<commons-lang3.version>3.9</commons-lang3.version>
</properties>
<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>${hutool.version}</version>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.12.1</version>
</dependency>
<!--poi 类 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>${poi.version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>${poi.version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>${poi.version}</version>
</dependency>
<!--word工具类-->
<dependency>
<groupId>com.deepoove</groupId>
<artifactId>poi-tl</artifactId>
<version>1.9.1</version>
<exclusions>
<exclusion>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>ooxml-schemas</artifactId>
<version>1.4</version>
</dependency>
<!--html渲染插件-->
<dependency>
<groupId>io.github.draco1023</groupId>
<artifactId>poi-tl-ext</artifactId>
<version>0.3.3</version>
<exclusions>
<exclusion>
<groupId>com.deepoove</groupId>
<artifactId>poi-tl</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.poi</groupId>
<artifactId>ooxml-schemas</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--样式依赖-->
<dependency>
<groupId>org.w3c.css</groupId>
<artifactId>sac</artifactId>
<version>1.3</version>
</dependency>
<dependency>
<groupId>net.sourceforge.cssparser</groupId>
<artifactId>cssparser</artifactId>
<version>0.9.29</version>
</dependency>
<!--合并word,free免费版可以用,否则收费-->
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.doc.free</artifactId>
<version>3.9.0</version>
</dependency>
</dependencies>
三、Java测试类
import cn.hutool.core.io.FileUtil;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.data.PictureRenderData;
import com.deepoove.poi.policy.PictureRenderPolicy;
import com.deepoove.poi.xwpf.NiceXWPFDocument;
import com.spire.doc.Document;
import com.spire.doc.DocumentObject;
import com.spire.doc.Section;
import com.spire.doc.FileFormat;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.ddr.poi.html.HtmlRenderPolicy;
import java.io.*;
import java.util.*;
/** * java生成word * * @author wangdy * @date 2021-06-15 11:16 */
public class Java2Word {
public static void main(String[] args) throws IOException {
// html渲染插件
HtmlRenderPolicy htmlRenderPolicy = new HtmlRenderPolicy();
// 第一个案例
Configure configure = Configure.builder()
// 注册html解析插件
.bind("content", htmlRenderPolicy)
// .bind("content2", htmlRenderPolicy)
.build();
// 映射数据Map
Map<String, Object> data = new HashMap<>();
data.put("title", "我是一个案例名称1");
data.put("keywords", "我是一个案例keywords");
data.put("cty", "我是一个案例cty");
data.put("content", readFile("/demo1.html"));
// 读取模板文件,并渲染数据
XWPFTemplate template = XWPFTemplate.compile(getResourceInputStream("/html2wordtemplate.docx"), configure).render(data);
// 写入文件
template.writeToFile("demo1.docx");
template.close();
// 第二个案例
Configure configure1 = Configure.builder()
.bind("content", htmlRenderPolicy)
.build();
Map<String, Object> data1 = new HashMap<>();
data1.put("title", "我是一个案例名称2");
data1.put("keywords", "我是一个案例keywords2");
data1.put("cty", "我是一个案例分类2");
data1.put("content", readFile("/demo2.html"));
XWPFTemplate template1 = XWPFTemplate.compile(getResourceInputStream("/html2wordtemplate.docx"), configure1).render(data1);
template1.writeToFile("demo2.docx");
template1.close();
// 合并word
//加载需要合并的两个文档
Document doc1 = new Document("demo1.docx");
Document doc2 = new Document("demo2.docx");
//获取文档1的最后一节
Section lastsec = doc1.getLastSection();
//遍历文档2的所有段落内容,添加到文档1
for (Section section : (Iterable<Section>) doc2.getSections()) {
for (DocumentObject obj : (Iterable<DocumentObject>) section.getBody().getChildObjects()) {
lastsec.getBody().getChildObjects().add(obj.deepClone());
}
}
//保存合并后的文档
doc1.saveToFile("ALL-Word.docx", FileFormat.Docx);
}
/** * 读取文件内容 * * @param resourceFile 文件路径 * @return 文件内容 * @throws IOException IO异常 * import org.apache.commons.io.IOUtils; */
public static String readFile(String resourceFile) throws IOException {
try (InputStream inputStream = FileReader.class.getResourceAsStream(resourceFile)) {
return IOUtils.toString(inputStream, StandardCharsets.UTF_8);
}
}
}
好了,运行看结果吧。
【补充1:】
如果html里有table,则原生table加边框样式不生效,需要添加td样式,如将<td>
整体替换为<td style="border:1px solid #ccc;">
才可以生效,如下效果:
【补充2】:
poi-tl-ext 0.3.3的版本,在实际用的过程中,对于如下html解析有误,升级到0.3.18解决问题。
原HTML如下:
<p> </p>
<p class="MsoNormal" style="text-align: left; text-indent: 21.0pt; line-height: 20.0pt; mso-line-height-rule: exactly; mso-pagination: widow-orphan;" align="left"><span style="font-size: 10.5pt; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt;">根据省人民政府xx政发<span lang="EN-US">(1992)18</span>号文附件六《xxx一九九二年基本建设计划》,现将一九九二年xx基本建设计划下达给你们<span lang="EN-US">(</span>见附表<span lang="EN-US">)</span>,请据此执行</span></p>
<p class="MsoNormal" style="text-align: left; text-indent: 21.0pt; line-height: 20.0pt; mso-line-height-rule: exactly; mso-pagination: widow-orphan;" align="left"><span style="font-size: 10.5pt; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt;">附<span lang="EN-US">:</span>xx省一九九二年xx基本建设计划表</span></p>
<p class="MsoNormal" style="text-align: right; line-height: 20.0pt; mso-line-height-rule: exactly; mso-pagination: widow-orphan;" align="right"><span style="font-size: 10.5pt; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt;">单位:万元、平方米</span></p>
<table style="border-collapse: collapse; width: 100%; height: 66px;" border="1">
<tbody>
<tr style="height: 22px;">
<td style="width: 11.0637%; height: 44px;" rowspan="2"><span style="font-size: 9.0pt; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA;">单位名称</span></td>
<td style="width: 11.0637%; height: 44px; text-align: center;" rowspan="2"><span style="font-size: 9.0pt; mso-bidi-font-size: 11.0pt; font-family: 宋体; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA;">项目</span></td>
<td style="width: 11.0637%; height: 44px; text-align: center;" rowspan="2"><span style="font-size: 9.0pt; mso-bidi-font-size: 11.0pt; font-family: 宋体; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA;">建设性质</span></td>
<td style="width: 11.0637%; height: 44px; text-align: center;" rowspan="2"><span style="font-size: 9.0pt; mso-bidi-font-size: 11.0pt; font-family: 宋体; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA;">建筑面积</span></td>
<td style="width: 11.0637%; height: 22px; text-align: center;" colspan="3"><span style="font-size: 9.0pt; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA;">一九九二年计划投资</span></td>
<td style="width: 11.0637%; height: 44px; text-align: center;" rowspan="2">备注</td>
</tr>
<tr style="height: 22px;">
<td style="width: 11.0637%; height: 22px; text-align: center;"><span style="font-size: 9.0pt; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA;">合计</span></td>
<td style="width: 11.0637%; height: 22px; text-align: center;"><span style="font-size: 9.0pt; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA;">统筹投资</span></td>
<td style="width: 11.0637%; height: 22px; text-align: center;"><span style="font-size: 9.0pt; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA;">省财政自筹</span></td>
</tr>
<tr style="height: 22px;">
<td style="width: 11.0637%; height: 22px; text-align: center;"><span style="font-size: 9.0pt; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA;">xx工学院</span></td>
<td style="width: 11.0637%; height: 22px; text-align: center;">
<p class="MsoNormal" style="text-align: left; line-height: 12.0pt; mso-line-height-rule: exactly; mso-pagination: widow-orphan;" align="left"><span style="font-size: 9.0pt; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt;">图书馆及电教中心</span></p>
<p class="MsoNormal" style="text-align: left; line-height: 12.0pt; mso-line-height-rule: exactly; mso-pagination: widow-orphan;" align="left"><span style="font-size: 9.0pt; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt;">农工住房拆迁</span></p>
<span style="font-size: 9.0pt; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA;">待安排</span></td>
<td style="width: 11.0637%; height: 22px; text-align: center;">
<p class="MsoNormal" style="text-align: left; line-height: 12.0pt; mso-line-height-rule: exactly; mso-pagination: widow-orphan;" align="left"><span style="font-size: 9.0pt; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt;">图书馆及电教中心</span></p>
<p class="MsoNormal" style="text-align: left; line-height: 12.0pt; mso-line-height-rule: exactly; mso-pagination: widow-orphan;" align="left"><span style="font-size: 9.0pt; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt;">农工住房拆迁</span></p>
<span style="font-size: 9.0pt; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA;">待安排</span></td>
<td style="width: 11.0637%; height: 22px; text-align: center;"><span lang="EN-US" style="font-size: 9.0pt; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA;">9800</span></td>
<td style="width: 11.0637%; height: 22px; text-align: center;">
<p class="MsoNormal" style="text-align: left; line-height: 12.0pt; mso-line-height-rule: exactly; mso-pagination: widow-orphan;" align="left"><span lang="EN-US" style="font-size: 9.0pt; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt;">520</span></p>
<p class="MsoNormal" style="text-align: left; line-height: 12.0pt; mso-line-height-rule: exactly; mso-pagination: widow-orphan;" align="left"><span lang="EN-US" style="font-size: 9.0pt; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt;">200</span></p>
<p class="MsoNormal" style="text-align: left; line-height: 12.0pt; mso-line-height-rule: exactly; mso-pagination: widow-orphan;" align="left"><span lang="EN-US" style="font-size: 9.0pt; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt;">100</span></p>
<span lang="EN-US" style="font-size: 9.0pt; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA;">220</span></td>
<td style="width: 11.0637%; height: 22px; text-align: center;">
<p class="MsoNormal" style="text-align: left; line-height: 12.0pt; mso-line-height-rule: exactly; mso-pagination: widow-orphan;" align="left"><span lang="EN-US" style="font-size: 9.0pt; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt;">210</span></p>
<p class="MsoNormal" style="text-align: left; line-height: 12.0pt; mso-line-height-rule: exactly; mso-pagination: widow-orphan;" align="left"><span lang="EN-US" style="font-size: 9.0pt; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt;">200</span></p>
<p class="MsoNormal" style="text-align: left; line-height: 12.0pt; mso-line-height-rule: exactly; mso-pagination: widow-orphan;" align="left"><span lang="EN-US" style="font-size: 9.0pt; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt;"> </span></p>
<span lang="EN-US" style="font-size: 9.0pt; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA;">10</span></td>
<td style="width: 11.0637%; height: 22px; text-align: center;">
<p class="MsoNormal" style="text-align: left; line-height: 12.0pt; mso-line-height-rule: exactly; mso-pagination: widow-orphan;" align="left"><span lang="EN-US" style="font-size: 9.0pt; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt;">310</span></p>
<p class="MsoNormal" style="text-align: left; line-height: 12.0pt; mso-line-height-rule: exactly; mso-pagination: widow-orphan;" align="left"><span lang="EN-US" style="font-size: 9.0pt; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt;"> </span></p>
<p class="MsoNormal" style="text-align: left; line-height: 12.0pt; mso-line-height-rule: exactly; mso-pagination: widow-orphan;" align="left"><span lang="EN-US" style="font-size: 9.0pt; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt;">100</span></p>
<span lang="EN-US" style="font-size: 9.0pt; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA;">210</span></td>
<td style="width: 11.0637%; height: 22px; text-align: center;">
<p class="MsoNormal" style="text-align: left; line-height: 12.0pt; mso-line-height-rule: exactly; mso-pagination: widow-orphan;" align="left"><span style="font-size: 9.0pt; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt;">含预拨投资<span lang="EN-US">100</span>万元</span></p>
<p class="MsoNormal" style="text-align: left; line-height: 12.0pt; mso-line-height-rule: exactly; mso-pagination: widow-orphan;" align="left"><span lang="EN-US" style="font-size: 9.0pt; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt;"> </span></p>
<p class="MsoNormal" style="text-align: left; line-height: 12.0pt; mso-line-height-rule: exactly; mso-pagination: widow-orphan;" align="left"><span lang="EN-US" style="font-size: 9.0pt; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt;"> </span></p>
<span style="font-size: 9.0pt; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA;">还工程欠款</span></td>
</tr>
</tbody>
</table>
<p class="MsoNormal"><span lang="EN-US"> </span></p>
<p> </p>
<p> </p>
HTML浏览器解析原样:
升级版本前生成word结果:发现合计等表格没有了,而且备注下面的边框少了一个。
升级版本后生成word结果:
END
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/184088.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...