Java 实现 HTML 转 DOC

Java 实现 HTML 转 doc


前言

最近开发中,遇到需要将前端富文本编辑内容导出word的需求。由于富文本编辑主要是前端生成对应的html文本,服务器端基本没有做什么处理原封不动地保存。所以该需求实际上就是需要处理HTML和DOC之间的转换问题。
查找资料后,最后决定选用docx4j工具进行转换处理。下面开始代码实现。

一、引入依赖

<!--html-->
<dependency>
    <groupId>org.docx4j</groupId>
    <artifactId>docx4j-ImportXHTML</artifactId>
    <version>${docx4j.version}</version>
    <exclusions>
        <exclusion>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
        </exclusion>
    </exclusions>
</dependency>

二、实现转换

import lombok.extern.slf4j.Slf4j;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.WordprocessingML.AltChunkType;
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * <p>
 * HTML 转换工具
 * </p>
 */
@Slf4j
public class HtmlConvertUtils {

    private HtmlConvertUtils() {
    }

    /**
     * HTML 文件转 DOC 文件
     *
     * @param htmlPath html文件路径
     * @param docPath  doc文件路径
     * @throws Docx4JException
     * @throws IOException
     */
    public static void html2Doc(String htmlPath, String docPath) throws Docx4JException, IOException {
        // 构建文件对象
        File htmlFile = new File(htmlPath);
        File docFile = new File(docPath);

        // html 转 doc
        html2Doc(htmlFile, docFile);
    }

    /**
     * HTML 文件转 DOC 文件
     *
     * @param htmlFile html文件
     * @param docFile  doc文件
     */
    public static void html2Doc(File htmlFile, File docFile) throws Docx4JException, IOException {
        // 1 读取 html 文件内容
        String htmlContent = FileUtils.readContent(htmlFile);

        // 2 如果目标路径不存在 则新建该路径
        if (!docFile.getParentFile().exists()) {
            docFile.getParentFile().mkdirs();
        }

        // html 转 doc
        htmlContent2Doc(htmlContent, docFile);
    }

    /**
     * HTML 内容转 DOC 文件
     *
     * @param html    html 内容
     * @param docPath doc文件路径
     * @throws Docx4JException
     */
    public static void htmlContent2Doc(String html, String docPath) throws Docx4JException {
        File docFile = new File(docPath);
        // 2 如果目标路径不存在 则新建该路径
        if (!docFile.getParentFile().exists()) {
            docFile.getParentFile().mkdirs();
        }

        // html 转 doc
        htmlContent2Doc(html, docFile);
    }

    /**
     * HTML 内容转 DOC 文件
     *
     * @param html    html 内容
     * @param docFile doc文件
     * @throws Docx4JException
     */
    public static void htmlContent2Doc(String html, File docFile) throws Docx4JException {
        WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
        MainDocumentPart mdp = wordMLPackage.getMainDocumentPart();
//        wordMLPackage.setFontMapper(IFontHandler.getFontMapper());
        // Add the Html altChunk
        mdp.addAltChunk(AltChunkType.Html, html.getBytes());

        // Round trip
        WordprocessingMLPackage pkgOut = mdp.convertAltChunks();

        pkgOut.save(docFile);
    }

}

总结

以上Java 实现 HTML 转 DOC 的代码实现,本文仅仅简单介绍了docx4j的使用,这是java中转门用来处理doc相关的工具库。

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
使用POI将Word文档换为HTML需要以下步骤: 1. 导入POI库和相关依赖库。 2. 读取Word文档,创建XWPFDocument对象。 3. 创建XHTMLContentHandler对象,用于处理Word文档中的内容。 4. 创建WordToHtmlConverter对象,并将XWPFDocument和XHTMLContentHandler作为参数传入。 5. 调用WordToHtmlConverter的processDocument方法,将Word文档换为HTML。 6. 获取HTML内容,可以将其保存到文件或输出到页面。 以下是一个简单的代码示例: ```java import java.io.*; import org.apache.poi.xwpf.converter.core.*; import org.apache.poi.xwpf.converter.xhtml.*; import org.apache.poi.xwpf.usermodel.*; public class WordToHtml { public static void main(String[] args) throws Exception { // 读取Word文档 XWPFDocument document = new XWPFDocument(new FileInputStream("input.docx")); // 创建XHTMLContentHandler对象 OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream("output.html"), "UTF-8"); XHTMLContentHandler contentHandler = new XHTMLContentHandler(writer, null); // 创建WordToHtmlConverter对象 WordToHtmlConverter converter = new WordToHtmlConverter(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument()); converter.setParagraphCssStyle("text-align: justify;"); // 设置段落样式 // 将XHTMLContentHandler设置为converter的内容处理器 converter.processDocument(document, contentHandler); // 获取HTML内容并输出 String html = writer.toString(); System.out.println(html); // 关闭流 writer.close(); document.close(); } } ``` 注意:该示例代码中的XWPFDocument仅适用于处理.docx格式的Word文档,如果需要处理.doc格式的Word文档,需要使用HWPFDocument类。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值