Libreoffice实现Word、Excel在线预览

Libreoffice下载地址

https://zh-cn.libreoffice.org/download/libreoffice/

依赖

xml 复制代码
  <!--jodconverter 核心包 -->
        <dependency>
            <groupId>org.jodconverter</groupId>
            <artifactId>jodconverter-core</artifactId>
            <version>4.4.6</version>
        </dependency>

        <!--springboot支持包,里面包括了自动配置类 -->
        <dependency>
            <groupId>org.jodconverter</groupId>
            <artifactId>jodconverter-spring-boot-starter</artifactId>
            <version>4.4.6</version>
        </dependency>

        <!--jodconverter 本地支持包 -->
        <dependency>
            <groupId>org.jodconverter</groupId>
            <artifactId>jodconverter-local-lo</artifactId>
            <version>4.4.6</version>
        </dependency>

代码

java 复制代码
package com.dxy.util.excel;

import java.io.File;
import org.jodconverter.core.office.OfficeException;
import org.jodconverter.local.JodConverter;
import org.jodconverter.local.office.LocalOfficeManager;

public class JodConverterDemo {
  public static void main(String[] args) throws OfficeException {
    // 获取LibreOffice服务实例
    LocalOfficeManager instance = getInstance();
    try {
      // 启动服务, 注意:如果是Spring boot项目,只需要在项目启动时 start 一次即可
      instance.start();
      // docx 转 pdf
      File srcFile1 = new File("D:\\test1.docx");
      File targetFile1 = new File("D:\\test1.pdf");
      JodConverter.convert(srcFile1).to(targetFile1).execute();
      // ppt 转 pdf
      File srcFile2 = new File("D:\\test2.ppt");
      File targetFile2 = new File("D:\\test2.pdf");
      JodConverter.convert(srcFile2).to(targetFile2).execute();
      // excel 转 html, excel 转 pdf 效果不好
      File srcFile3 = new File("E:\\tmp\\user2.xlsx");
      File targetFile3 = new File("E:\\tmp\\user2.html");
      JodConverter.convert(srcFile3).to(targetFile3).execute();
    } finally {
      // 停止服务,demo中需要手动停止LibreOffice服务,不然项目无法退出,只能手动杀掉LibreOffice进程
      instance.stop();
    }
  }

  public static LocalOfficeManager getInstance() {
    LocalOfficeManager.Builder builder = LocalOfficeManager.builder().install();
    // 此处是 LibreOffice 的目录
    builder.officeHome("C:\\Program Files\\LibreOffice");
    // 指定 LibreOffice 服务的端口号,若要启动多个服务,则填写多个端口号
    builder.portNumbers(2000);
    // 转换超时时间
    builder.taskExecutionTimeout(60L * 1000);
    // 队列超时时间
    builder.taskQueueTimeout(1000 * 60 * 60L); // 1小时
    return builder.build();
  }
}

参考文章

https://blog.csdn.net/qq_33256826/article/details/136497411
https://blog.csdn.net/wolf_you/article/details/129858438

相关推荐
123的故事1 小时前
工具分享(7)-多Excel文件内容查询工具
c#·excel·实用工具
yivifu1 小时前
怎样将Word文档中脚注引用后面的空格轻松删除
word·vba
一 乐2 小时前
家政服务管理系统|基于springboot + vue家政服务管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·论文·毕设·家政服务管理系统
碳基硅坊3 小时前
Spring AI:把大模型接进 Spring 应用
java·人工智能·spring ai
黄毛火烧雪下3 小时前
Java 核心知识点总结(一)
java·开发语言
技术小结-李爽3 小时前
【工具】Maven的下载、安装、使用
java·maven
极创信息3 小时前
Linux挖矿病毒深度清理实战教程,从进程隐藏、Rootkit驻留到彻底根除
java·大数据·linux·运维·安全·tomcat·健康医疗
努力成为AK大王3 小时前
并发编程的核心挑战、优化方案与核心知识点总结
java·开发语言·数据库
云烟成雨TD3 小时前
Agent Scope Java 2.x 系列【10】技能(Skill)
java·人工智能·agent
摇滚侠4 小时前
SpringMVC 入门到实战 DispatcherServlet 源码解读 92-95
java·后端·spring·maven·intellij-idea