Word转换HTML(Java实用版)

前言:

在业务中,如果需要在浏览器中预览Word文档,或者需要将Word文档转成HTML文件保存,那么本章内容,可以帮助到你。

实现这一功能,有多种实现方式,如:docx4j、poi、Free Spire.Doc for Java、openoffice、jacob都可以实现转换功能,但都有局限性。在这稍微介绍一下哈,大家可做个对比

docx4j

docx4j主要是针对docx文件进行操作,操作的对象的Microsoft Open XML文件。

java当中用于操作office(docx/xlsx/ppt)等文件的类库

poi

POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能。

结构:

HSSF - 提供读写Microsoft Excel格式档案的功能。

XSSF - 提供读写Microsoft Excel OOXML格式档案的功能。

HWPF - 提供读写Microsoft Word格式档案的功能。

HSLF - 提供读写Microsoft PowerPoint格式档案的功能。

HDGF - 提供读写Microsoft Visio格式档案的功能。

Free Spire.Doc for Java(功能强大,但可以收费)

Free Spire.Doc for Java 是一款免费、专业的 Java Word 组件,开发人员使用它可以轻松地将 Word 文档创建读取、编辑、转换打印等功能集成到自己的 Java 应用程序中。作为一款完全独立的组件,Free Spire.Doc for Java的运行环境无需安装 Microsoft Office。

友情提示 :免费版有篇幅限制。在加载或保存 Word 文档时,要求 Word 文档不超过 500 个段落,25 个表格。同时将 Word 文档转换为 PDF 和 XPS 等格式时,仅支持转换前三页

openoffice

一、利用jodconverter(基于OpenOffice服务)将文件(.doc、.docx、 .xls、.ppt)转化为html格式。
二、利用jodconverter(基于OpenOffice服务)将文件(.doc、.docx、 .xls、.ppt)转化为pdf格式。需要用户安装了Adobe Reader XI

jacob(不能用于Linux)

需要引入jacob.jar jar包,并且jar包还要调用jacob.dll文件,需要事先把jacob.dll文件放到以下3处地方:C:\Windows\System32 目录下,安装的jdk文件夹下的bin目录中,以及jre文件夹下的bin目录(注意一定是你这个项目运行所用到的jdk和jre)

它允许在java中调用com接口自动组件,它使用JNI(本地调用进程)来进行本地调用COM库。它可运行在x86和支持32位和64位Java 虚拟机

本文采用poi来进行转换

1、Poi转换

1.1、引入依赖

<!-- WordToHtml .doc .odcx  poi  -->
<dependency>
    <groupid>org.apache.poi</groupid>
    <artifactid>poi-scratchpad</artifactid>
    <version>4.1.2</version>
</dependency>

<!-- 操作excel的库 注意版本保持一致 poi poi-ooxml  poi-scratchpad -->
<dependency>
    <groupid>org.apache.poi</groupid>
    <artifactid>poi</artifactid>
    <version>4.1.2</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
    <groupid>org.apache.poi</groupid>
    <artifactid>poi-ooxml</artifactid>
    <version>4.1.2</version>
</dependency>

<dependency>
    <groupid>fr.opensagres.xdocreport</groupid>
    <artifactid>fr.opensagres.poi.xwpf.converter.xhtml</artifactid>
    <version>2.0.2</version>
</dependency>

<!-- https://mvnrepository.com/artifact/fr.opensagres.xdocreport/fr.opensagres.xdocreport.converter.docx.xwpf -->
<dependency>
    <groupid>fr.opensagres.xdocreport</groupid>
    <artifactid>fr.opensagres.xdocreport.converter.docx.xwpf</artifactid>
    <version>2.0.1</version>
</dependency>

1.2、工具类

poi转换工具类

/**
 * poi WordToHtml&#x5DE5;&#x5177;&#x7C7B;
 */
@Slf4j
public class WordToHtml {

//    &#x6587;&#x4EF6;&#x4E0A;&#x4F20;&#x4FDD;&#x5B58;&#x8DEF;&#x5F84;
    @Value(value = "${upload.path}")
    private final String uploadPath = "";

    //&#x8F6C;&#x6362;&#x7684;&#x65B9;&#x6CD5;
    public File convert(MultipartFile file) {
        //&#x83B7;&#x5F97;&#x6587;&#x4EF6;&#x7684;&#x540D;&#x5B57;
        String filename = file.getOriginalFilename();
        //&#x83B7;&#x5F97;&#x6587;&#x4EF6;&#x7684;&#x6269;&#x5C55;&#x540D;
        String suffix = filename.substring(filename.lastIndexOf("."));
        String newName = UUID.randomUUID().toString();
        // TODO &#x9700;&#x8981;&#x4FDD;&#x5B58;&#x5728;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x4F4D;&#x7F6E;
        // File =new File &#x8868;&#x793A;&#x76EE;&#x5F55;&#x7684;&#x4E00;&#x4E2A;&#x62BD;&#x8C61;,&#x53EF;&#x4EE5;&#x8FDB;&#x4E00;&#x6B65;&#x7528;exists()&#x548C;isDirectory()&#x65B9;&#x6CD5;&#x5224;&#x65AD;&#x3002;
        File convFile = new File(uploadPath + newName + suffix);
        FileOutputStream fos = null;
        try {
            //&#x521B;&#x5EFA;&#x6587;&#x4EF6;
            convFile.createNewFile();
            //FileOutputStream &#x662F;&#x8F93;&#x51FA;&#x6D41; &#x5C06;&#x6587;&#x4EF6;&#x8F93;&#x51FA;&#x5230;&#x78C1;&#x76D8;&#x6216;&#x8005;&#x6570;&#x636E;&#x5E93;&#x4E2D;
            fos = new FileOutputStream(convFile);
            fos.write(file.getBytes());
        } catch (IOException ex) {
            log.error("&#x4E0A;&#x4F20;&#x6587;&#x4EF6;&#x51FA;&#x9519;&#xFF01;", ex);
            return null;
        } finally {
            IOUtils.closeQuietly(fos);
        }

        // &#x8F93;&#x5165;&#x6587;&#x4EF6;&#x540D;&#x7684;&#x6240;&#x5728;&#x6587;&#x4EF6;&#x5939;
        // &#x52A0;&#x4E0A;&#x53CD;&#x659C;&#x6760;
        String parentDirectory = convFile.getParent();
        if (!parentDirectory.endsWith("\\")) {
            parentDirectory = parentDirectory + "\\";
        }

        if (filename.endsWith(".docx")) {
            return docxConvert(parentDirectory, convFile.getAbsolutePath(), newName);
        } else if (filename.endsWith(".doc")) {
            return docConvert(parentDirectory, convFile.getAbsolutePath(), newName);
        } else {
            log.error("&#x4E0D;&#x652F;&#x6301;&#x7684;&#x6587;&#x4EF6;&#x683C;&#x5F0F;&#xFF01;");
            return null;
        }
    }

    /**
     * html &#x6D41;&#x6587;&#x4EF6; &#x4FEE;&#x6539;&#x5185;&#x5BB9; width:595.3pt;  &#x56E0;&#x4E3A;&#x8F6C;&#x6362;&#x7684;HTML&#x9875;&#x9762;&#x9ED8;&#x8BA4;&#x5185;&#x5BB9;&#x533A;&#x57DF;&#x4E0D;&#x662F;html&#x81EA;&#x9002;&#x5E94;&#x5927;&#x5C0F;&#xFF0C;&#x5185;&#x5BB9;&#x4F4D;&#x7F6E;&#x4E0D;&#x5BF9;
     * @param parentDirectory html&#x6587;&#x4EF6;&#x6240;&#x5728;&#x6587;&#x4EF6;&#x5939;
     * @param filename html&#x65E7;&#x6587;&#x4EF6;&#x5730;&#x5740;
     * @param newName html&#x65B0;&#x6587;&#x4EF6;&#x5730;&#x5740;
     * @return
     */
    private File htmlreplace(String parentDirectory, String filename, String newName) {
        try {
//            &#x8BFB;&#x53D6;&#x751F;&#x6210;&#x7684;Html
            FileInputStream inputStream = new FileInputStream(new File(parentDirectory + filename + ".html"));
            InputStream inputStrem = readInputStrem(inputStream);
//            &#x6E05;&#x7A7A;&#x6587;&#x4EF6;&#x5185;&#x5BB9;
            clearInfoForFile(parentDirectory + filename + ".html");
            // TODO: 2022/4/22 &#x8FDB;&#x884C;&#x6D41;&#x8F93;&#x51FA;Html&#x6587;&#x4EF6; inputStrem
//            1&#x3001;&#x8BFB;&#x53D6;&#x5185;&#x5BB9;
            byte[] buffer = new byte[inputStrem.available()];
            inputStrem.read(buffer);
//            &#x5199;&#x5165;&#x5185;&#x5BB9;
            OutputStream outStream = new FileOutputStream(new File(parentDirectory + newName + ".html"));
            outStream.write(buffer);
            return new File(parentDirectory + newName + ".html");
        } catch (FileNotFoundException e) {
            log.error("Html&#x8F6C;&#x6362;&#x5931;&#x8D25;&#xFF01;",e);
            return null;
        } catch (IOException e) {
            log.error("Html&#x8F6C;&#x6362;&#x5931;&#x8D25;&#xFF01;",e);
            return null;
        }
    }

    /**
     * &#x8BFB;&#x53D6;HTML &#x6D41;&#x6587;&#x4EF6;&#xFF0C;&#x5E76;&#x67E5;&#x8BE2;&#x5F53;&#x4E2D;&#x7684;width:595.3pt;  / white-space:pre-wrap; &#x6216;&#x7C7B;&#x4F3C;&#x7B26;&#x53F7;&#x76F4;&#x63A5;&#x66FF;&#x6362;&#x4E3A;&#x7A7A;&#x683C;
     *
     * @param inputStream
     * @return
     */
    private static InputStream readInputStrem(InputStream inputStream) {
//        &#x5339;&#x914D;&#x5185;&#x5BB9;
        String regEx_special = "width:595.3pt;";

        String regEx_special2 = "white-space:pre-wrap;";

//        &#x66FF;&#x6362;&#x65B0;&#x5185;&#x5BB9;
        String replace = "white-space:pre-wrap;word-break:break-all;";
        try {
            //<1>&#x521B;&#x5EFA;&#x5B57;&#x8282;&#x6570;&#x7EC4;&#x8F93;&#x51FA;&#x6D41;&#xFF0C;&#x7528;&#x6765;&#x8F93;&#x51FA;&#x8BFB;&#x53D6;&#x5230;&#x7684;&#x5185;&#x5BB9;
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            //<2>&#x521B;&#x5EFA;&#x7F13;&#x5B58;&#x5927;&#x5C0F;
            byte[] buffer = new byte[1024]; // 1KB
            //&#x6BCF;&#x6B21;&#x8BFB;&#x53D6;&#x5230;&#x5185;&#x5BB9;&#x7684;&#x957F;&#x5EA6;
            int len = -1;
            //<3>&#x5F00;&#x59CB;&#x8BFB;&#x53D6;&#x8F93;&#x5165;&#x6D41;&#x4E2D;&#x7684;&#x5185;&#x5BB9;
            while ((len = inputStream.read(buffer)) != -1) { //&#x5F53;&#x7B49;&#x4E8E;-1&#x8BF4;&#x660E;&#x6CA1;&#x6709;&#x6570;&#x636E;&#x53EF;&#x4EE5;&#x8BFB;&#x53D6;&#x4E86;
                baos.write(buffer, 0, len);   //&#x628A;&#x8BFB;&#x53D6;&#x5230;&#x7684;&#x5185;&#x5BB9;&#x5199;&#x5230;&#x8F93;&#x51FA;&#x6D41;&#x4E2D;
            }
            //<4> &#x628A;&#x5B57;&#x8282;&#x6570;&#x7EC4;&#x8F6C;&#x6362;&#x4E3A;&#x5B57;&#x7B26;&#x4E32;
            String content = baos.toString();
            //<5>&#x5173;&#x95ED;&#x8F93;&#x5165;&#x6D41;&#x548C;&#x8F93;&#x51FA;&#x6D41;
//            inputStream.close();
            baos.close();
//            log.info("&#x8BFB;&#x53D6;&#x7684;&#x5185;&#x5BB9;&#xFF1A;{}", content);
//            &#x5224;&#x65AD;HTML&#x5185;&#x5BB9;&#x662F;&#x5426;&#x5177;&#x6709;HTML&#x7684; width:595.3pt;
            Pattern compile = Pattern.compile(regEx_special, Pattern.CASE_INSENSITIVE);
            Matcher matcher = compile.matcher(content);
            String replaceAll = matcher.replaceAll("");
//            &#x5224;&#x65AD;&#x662F;&#x5426;&#x5177;&#x6709;white-space:pre-wrap;
            Pattern compile2 = Pattern.compile(regEx_special2, Pattern.CASE_INSENSITIVE);
            Matcher matcher2 = compile2.matcher(replaceAll);
            String replaceAll2 = matcher2.replaceAll(replace);
//            log.info("&#x66FF;&#x6362;&#x540E;&#x7684;&#x5185;&#x5BB9;&#xFF1A;{}", replaceAll2);
//            &#x5C06;&#x5B57;&#x7B26;&#x4E32;&#x8F6C;&#x5316;&#x4E3A;&#x8F93;&#x5165;&#x6D41;&#x8FD4;&#x56DE;
            InputStream stringStream = getStringStream(replaceAll2);
            //<6>&#x8FD4;&#x56DE;&#x7ED3;&#x679C;
            return stringStream;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("&#x9519;&#x8BEF;&#x4FE1;&#x606F;&#xFF1A;{}", e.getMessage());
            return null;
        }
    }

    /**
     * &#x5C06;&#x4E00;&#x4E2A;&#x5B57;&#x7B26;&#x4E32;&#x8F6C;&#x5316;&#x4E3A;&#x8F93;&#x5165;&#x6D41;
     * @param sInputString &#x5B57;&#x7B26;&#x4E32;
     * @return
     */
    public static InputStream getStringStream(String sInputString) {
        if (sInputString != null && !sInputString.trim().equals("")) {
            try {
                ByteArrayInputStream tInputStringStream = new ByteArrayInputStream(sInputString.getBytes());
                return tInputStringStream;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    /**
     * &#x6E05;&#x7A7A;&#x6587;&#x4EF6;&#x5185;&#x5BB9;
     * @param fileName
     */
    public static void clearInfoForFile(String fileName) {
        File file =new File(fileName);
        try {
            if(!file.exists()) {
                file.createNewFile();
            }
            FileWriter fileWriter =new FileWriter(file);
            fileWriter.write("");
            fileWriter.flush();
            fileWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * &#x8F6C;&#x6362;.docx   &#x5F53;word&#x6587;&#x6863;&#x5B57;&#x4F53;&#x5927;&#x4E8E;5&#x53F7;&#x5B57;&#x4F53;&#x65F6;&#xFF0C;&#x4F1A;&#x51FA;&#x73B0;&#x4E0D;&#x89C4;&#x5F8B;&#x6392;&#x5217;&#x6587;&#x5B57;&#x6362;&#x884C;(&#x56E0;&#x4E3A;&#x8F6C;&#x6362;&#x7684;HTML&#x9875;&#x9762;&#x9ED8;&#x8BA4;&#x5185;&#x5BB9;&#x533A;&#x57DF;&#x4E0D;&#x662F;html&#x539F;&#x59CB;&#x533A;&#x57DF;)
     * @param parentDirectory html&#x6587;&#x4EF6;&#x6240;&#x5728;&#x6587;&#x4EF6;&#x5939; &#xFF08;&#x4E3B;&#x8981;&#x7528;&#x4E8E;&#x56FE;&#x50CF;&#x7684;&#x7BA1;&#x7406;&#xFF09;
     * @param filename word&#x6587;&#x4EF6;&#x5730;&#x5740;
     * @param newName html&#x6587;&#x4EF6;&#x5730;&#x5740;
     * @return
     */
    private File docxConvert(String parentDirectory, String filename, String newName) {
        try {
            // 1) &#x52A0;&#x8F7D;word&#x6587;&#x6863;&#x751F;&#x6210; XWPFDocument&#x5BF9;&#x8C61;
            XWPFDocument document = new XWPFDocument(new FileInputStream(filename));

//           &#x8BBE;&#x7F6E;&#x5B58;&#x653E;&#x56FE;&#x7247;&#x5730;&#x5740;
            XHTMLOptions options = XHTMLOptions.create().setImageManager(new ImageManager(new File(parentDirectory), UUID.randomUUID().toString())).indent(4);
            OutputStream out = new FileOutputStream(new File(parentDirectory + newName + ".html"));
//            &#x81EA;&#x5B9A;&#x4E49;&#x7F16;&#x7801;&#x683C;&#x5F0F;
            OutputStreamWriter writer = new OutputStreamWriter(out,"GBK");
//            &#x751F;&#x6210;HTML
            XHTMLConverter xhtmlConverter = (XHTMLConverter)XHTMLConverter.getInstance();
            xhtmlConverter.convert(document, writer, options);
//            &#x5C06;&#x751F;&#x6210;&#x7684;HTML&#x8FDB;&#x884C;&#x5185;&#x5BB9;&#x5339;&#x914D;&#x66FF;&#x6362;
            File htmlreplace = htmlreplace(parentDirectory, newName, newName);
            return htmlreplace;
//            return new File(parentDirectory + newName + ".html");
        } catch (IOException ex) {
            log.error("word&#x8F6C;&#x5316;&#x51FA;&#x9519;&#xFF01;", ex);
            return null;
        }

    }

    /**
     * &#x8F6C;&#x6362;.doc
     * @param parentDirectory html&#x6587;&#x4EF6;&#x6240;&#x5728;&#x6587;&#x4EF6;&#x5939; &#xFF08;&#x4E3B;&#x8981;&#x7528;&#x4E8E;&#x56FE;&#x50CF;&#x7684;&#x7BA1;&#x7406;&#xFF09;
     * @param filename word&#x6587;&#x4EF6;&#x5730;&#x5740;
     * @param newName html&#x6587;&#x4EF6;&#x5730;&#x5740;
     * @return
     */
    private File docConvert(String parentDirectory, String filename, String newName) {
        try {
            HWPFDocument document = new HWPFDocument(new FileInputStream(filename));
            WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(
                    DocumentBuilderFactory.newInstance().newDocumentBuilder()
                            .newDocument());

            // converter&#x9ED8;&#x8BA4;&#x5BF9;&#x56FE;&#x7247;&#x4E0D;&#x4F5C;&#x5904;&#x7406;&#xFF0C;&#x9700;&#x8981;&#x624B;&#x52A8;&#x4E0B;&#x8F7D;&#x56FE;&#x7247;&#x5E76;&#x5D4C;&#x5165;&#x5230;html&#x4E2D;
            wordToHtmlConverter.setPicturesManager(new PicturesManager() {
                @Override
                public String savePicture(byte[] bytes, PictureType pictureType, String s, float v, float v1) {
                    String imageFilename = parentDirectory + "";
                    String identity = UUID.randomUUID().toString();
                    File imageFile = new File(imageFilename, identity + s);
                    imageFile.getParentFile().mkdirs();
                    InputStream in = null;
                    FileOutputStream out = null;

                    try {
                        in = new ByteArrayInputStream(bytes);
                        out = new FileOutputStream(imageFile);
                        IOUtils.copy(in, out);

                    } catch (IOException ex) {
                        log.error("word&#x8F6C;&#x5316;&#x51FA;&#x9519;&#xFF01;", ex);
                    } finally {
                        if (in != null) {
                            IOUtils.closeQuietly(in);
                        }

                        if (out != null) {
                            IOUtils.closeQuietly(out);
                        }

                    }
                    return imageFile.getName();
                }
            });

            wordToHtmlConverter.processDocument(document);
            Document htmlDocument = wordToHtmlConverter.getDocument();
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            DOMSource domSource = new DOMSource(htmlDocument);
            StreamResult streamResult = new StreamResult(out);

//            &#x8BBE;&#x7F6E;&#x8F6C;&#x6362;&#x5C5E;&#x6027;
            TransformerFactory tf = TransformerFactory.newInstance();
            Transformer serializer = tf.newTransformer();
            serializer.setOutputProperty(OutputKeys.ENCODING, "GBK");
            serializer.setOutputProperty(OutputKeys.INDENT, "yes");
            serializer.setOutputProperty(OutputKeys.METHOD, "html");
            serializer.transform(domSource, streamResult);
            out.close();

            String result = new String(out.toByteArray());
            FileWriter writer = new FileWriter(parentDirectory + newName + ".html");
            writer.write(result);
            writer.close();
        } catch (IOException | TransformerException | ParserConfigurationException ex) {
            log.error("word&#x8F6C;&#x5316;&#x51FA;&#x9519;&#xFF01;", ex);
        }
        return new File(parentDirectory + newName + ".html");
    }

    /**
     * &#x5C06;&#x4E0A;&#x4F20;&#x7684;Word&#x6587;&#x6863;&#x8F6C;&#x5316;&#x6210;HTML&#x5B57;&#x7B26;&#x4E32;
     *
     * @param file
     * @return
     */
    public String convertToHtml(MultipartFile file) {
        String wordContent = "";
        // &#x5C06;Word&#x6587;&#x4EF6;&#x8F6C;&#x6362;&#x4E3A;html
        File file2 = convert(file);
        // &#x8BFB;&#x53D6;html&#x6587;&#x4EF6;
        if (file2 != null) {
            return "&#x6587;&#x4EF6;&#x8F6C;&#x6362;&#x6210;&#x529F;";
        }
        return "&#x6587;&#x4EF6;&#x8F6C;&#x6362;&#x5931;&#x8D25;";
    }

    /**
     * wordToHtml
     * @param wordFilePath word&#x6587;&#x4EF6;&#x8DEF;&#x5F84;
     * @param htmlFilePath html&#x6587;&#x4EF6;&#x8DEF;&#x5F84;
     * @throws IOException
     * @throws ParserConfigurationException
     * @throws TransformerException
     */
    public static File wordToHtml(String wordFilePath,String htmlFilePath) {
//        &#x63D0;&#x53D6;&#x51FA;word&#x6587;&#x6863;&#x540D;&#x79F0;&#x548C;&#x540E;&#x7F00;
        String filename = wordFilePath.substring(wordFilePath.lastIndexOf("/")+1);
//        &#x63D0;&#x53D6;&#x51FA;html&#x6587;&#x4EF6;&#x5B58;&#x653E;&#x8DEF;&#x5F84;&#x548C;&#x6587;&#x4EF6;&#x540D;&#x79F0;
        String newName = htmlFilePath.substring(htmlFilePath.lastIndexOf("/")+1,htmlFilePath.lastIndexOf("."));
        File convFile = new File(htmlFilePath);
        // &#x8F93;&#x5165;&#x6587;&#x4EF6;&#x540D;&#x7684;&#x6240;&#x5728;&#x6587;&#x4EF6;&#x5939;
        // &#x52A0;&#x4E0A;&#x53CD;&#x659C;&#x6760;
        String parentDirectory = convFile.getParent();
        if (!parentDirectory.endsWith("\\")) {
            parentDirectory = parentDirectory + "\\";
        }

        if (filename.endsWith(".docx")) {
            return new WordToHtml().docxConvert(parentDirectory, wordFilePath, newName);
        } else if (filename.endsWith(".doc")) {
            return new WordToHtml().docConvert(parentDirectory, wordFilePath, newName);
        } else {
            log.error("&#x4E0D;&#x652F;&#x6301;&#x7684;&#x6587;&#x4EF6;&#x683C;&#x5F0F;&#xFF01;");
            return null;
        }

    }
}</6></5></4></3></2></1>

1.3、测试类

/**
 * @Author&#xFF1A;wk
 * @Create&#xFF1A;2022/4/21/15:10
 * @Description&#xFF1A;WordToHtml&#x6D4B;&#x8BD5;&#x7C7B; poi
 * @Version&#xFF1A;1.0
 */
@Slf4j
public class WordToHtmlTest {

    public static void main(String[] args) {
        long timeMillis = System.currentTimeMillis();
        log.info("&#x5F00;&#x59CB;&#x8F6C;&#x6362;&#xFF01;");
        String wordFilePath = "src/main/resources/word/nc.docx";
        String htmlFilePath = "src/main/resources/html/nc5.html";
        File file = WordToHtml.wordToHtml(wordFilePath, htmlFilePath);
        // &#x8BFB;&#x53D6;html&#x6587;&#x4EF6;
        if (file != null) {
            log.info("&#x6587;&#x4EF6;&#x5B58;&#x653E;&#x8DEF;&#x5F84;&#xFF1A;{}",file.getPath());
            log.info("&#x8F6C;&#x6362;&#x7ED3;&#x675F;&#xFF01;&#x7528;&#x65F6;&#xFF1A;{}ms",System.currentTimeMillis()-timeMillis);
            return;
        }
        log.error("&#x6587;&#x4EF6;&#x8F6C;&#x6362;&#x5931;&#x8D25;&#xFF01;");
    }

}

测试效果(真实效果存在较小差异)由于截图一页显示不全,所以文档和页面都做了响应调整哈

.doc

Word转换HTML(Java实用版)

Word转换HTML(Java实用版)

.docx(文档是一样的, 此处就不截屏了哈)

Original: https://www.cnblogs.com/aerfazhe/p/16182514.html
Author: 阿尔法哲
Title: Word转换HTML(Java实用版)

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/621866/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球