• java word 转html 的两种方法


    1. 依赖aspose(需要收费,有水印)

        <repositories>
            <repository>
                <id>AsposeJavaAPI</id>
                <name>Aspose Java API</name>
                <url>https://repository.aspose.com/repo/</url>
            </repository>
        </repositories>
    
        <dependencies>
    
            <dependency>
                <groupId>com.aspose</groupId>
                <artifactId>aspose-words</artifactId>
                <version>21.10</version>
                <type>pom</type>
            </dependency>
    </dependencies>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
          
            com.aspose.words.Document doc = new com.aspose.words.Document();
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            HtmlSaveOptions saveOptions = new HtmlSaveOptions();
            saveOptions.setSaveFormat(SaveFormat.HTML);
            saveOptions.setEncoding(StandardCharsets.UTF_8);
            saveOptions.setExportImagesAsBase64(true);
            saveOptions.setExportDocumentProperties(false);
            doc.save(byteArrayOutputStream, saveOptions);
            System.out.println(byteArrayOutputStream.toString());
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    2. 依赖fr.opensagres.xdocreport

    免费,但是依赖poi-ooxml 3.10-FINAL 版本较低,会与其他高版本包冲突

    <dependency>
                <groupId>fr.opensagres.xdocreport</groupId>
                <artifactId>fr.opensagres.xdocreport.document</artifactId>
                <version>1.0.5</version>
            </dependency>
            <dependency>
                <groupId>fr.opensagres.xdocreport</groupId>
                <artifactId>org.apache.poi.xwpf.converter.xhtml</artifactId>
                <version>1.0.5</version>
            </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
       InputStream in = new FileInputStream(f);
                    XWPFDocument document = new XWPFDocument(in);
    
                    // 2) 解析 XHTML配置 (这里设置IURIResolver来设置图片存放的目录)
                    XHTMLOptions options = XHTMLOptions.create();
                    options.setIgnoreStylesIfUnused(false);
                    options.setFragment(true);
    
                    // 3) 将 XWPFDocument转换成XHTML
                    OutputStream out = new FileOutputStream(new File(filepath + htmlName));
                    XHTMLConverter.getInstance().convert(document, out, options);
    
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    XHTMLConverter.getInstance().convert(document, baos, options);
                    String content = baos.toString();
                    System.out.println(content);
                    baos.close();
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
  • 相关阅读:
    移民香港三年的优才计划客户,讲述最真实的香港生活!
    嵌入式软件工程师面试题(三)
    巩固系统韧性三个基础策略
    python项目-飞机大战
    django Ajax(七)
    Go学习第一章——开发环境安装以及快速入门(GoLand)
    The Last Naruto,兼容IE11的vue脚手架
    IntelliJ IDEA 2023 最新版如何试用?IntelliJ IDEA 2023最新版试用方法及验证ja-netfilter配置成功提示
    C++中类的友元函数和友元类详解
    信息系统项目管理师Part17-云计算
  • 原文地址:https://blog.csdn.net/ISaiSai/article/details/125447234