Springboot實(shí)現(xiàn)富文本HTML轉(zhuǎn)Word(近乎百分之百還原)

寫在前面:日常開發(fā)中,經(jīng)常會(huì)碰到富文本使用場(chǎng)景,有些需求是必須導(dǎo)出到word中,所以我們來(lái)實(shí)現(xiàn)吧!

一、引入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測(cè)試類

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();
    // 第一個(gè)案例
    Configure configure = Configure.builder()
    // 注冊(cè)html解析插件
    .bind("content", htmlRenderPolicy)
    // .bind("content2", htmlRenderPolicy)
    .build();
    // 映射數(shù)據(jù)Map
    Map<String, Object> data = new HashMap<>();
    data.put("title", "我是一個(gè)案例名稱1");
    data.put("keywords", "我是一個(gè)案例keywords");
    data.put("cty", "我是一個(gè)案例cty");
    data.put("content", readFile("/demo1.html"));
    // 讀取模板文件,并渲染數(shù)據(jù)
    XWPFTemplate template = XWPFTemplate.compile(getResourceInputStream("/html2wordtemplate.docx"), configure).render(data);
    // 寫入文件
    template.writeToFile("demo1.docx");
    template.close();

     // 第二個(gè)案例
     Configure configure1 = Configure.builder()
             .bind("content", htmlRenderPolicy)
             .build();
     Map<String, Object> data1 = new HashMap<>();
     data1.put("title", "我是一個(gè)案例名稱2");
     data1.put("keywords", "我是一個(gè)案例keywords2");
     data1.put("cty", "我是一個(gè)案例分類2");
     data1.put("content", readFile("/demo2.html"));
     XWPFTemplate template1 = XWPFTemplate.compile(getResourceInputStream("/html2wordtemplate.docx"), configure1).render(data1);
     template1.writeToFile("demo2.docx");
     template1.close();
    
     // 合并word
     //加載需要合并的兩個(gè)文檔
     Document doc1 = new Document("demo1.docx");
     Document doc2 = new Document("demo2.docx");
     //獲取文檔1的最后一節(jié)
     Section lastsec = doc1.getLastSection();
     //遍歷文檔2的所有段落內(nèi)容,添加到文檔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);
    

    }

/**
* 讀取文件內(nèi)容
*
* @param resourceFile 文件路徑
* @return 文件內(nèi)容
* @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);
}
}

}

三、運(yùn)行結(jié)果


image.png

注意:poi-tl-ext 0.3.3的版本,在實(shí)際用的過程中,對(duì)于如下html解析有誤,比如行距問題,切換到到0.3.18解決問題。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容