写在前面:日常开发中,经常会碰到富文本使用场景,有些需求是必须导出到word中,所以我们来实现吧!
一、引入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>{commons-lang3.version}</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>{poi.version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>{poi.version}</version>
</dependency>
<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>
<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>
<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);
}
}
}
三、运行结果
image.png
注意:poi-tl-ext 0.3.3的版本,在实际用的过程中,对于如下html解析有误,比如行距问题,切换到到0.3.18解决问题。
网友评论