Springboot实现富文本HTML转Word(近乎百分之百还

2022-11-10  本文已影响0人  老街老街

写在前面:日常开发中,经常会碰到富文本使用场景,有些需求是必须导出到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>{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> <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>

<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.
;

/**

/**
* 读取文件内容
*
* @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解决问题。

上一篇 下一篇

猜你喜欢

热点阅读