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

相关推荐
ysyxg几秒前
设计模式-策略模式
java·开发语言
LilySesy6 分钟前
ABAP+在select的时候,可以A=B A=C B=C这样子JOIN吗?
数据库·sql·ai·excel·sap·abap
Felix_XXXXL11 分钟前
Spring Security安全框架原理与实战
java·后端
一抓掉一大把32 分钟前
秒杀-StackExchangeRedisHelper连接单例
java·开发语言·jvm
升鲜宝供应链及收银系统源代码服务32 分钟前
升鲜宝生鲜配送供应链管理系统--- 《多语言商品查询优化方案(Redis + 翻译表 + 模糊匹配)》
java·数据库·redis·bootstrap·供应链系统·生鲜配送·生鲜配送源代码
青山的青衫42 分钟前
【JavaWeb】Tlias后台管理系统
java·web
蒟蒻的工具人1 小时前
SSE实时推送订单状态
java·eventsource·sse协议
小蒜学长1 小时前
springboot基于Java的校园导航微信小程序的设计与实现(代码+数据库+LW)
java·spring boot·后端·微信小程序
王元_SmallA1 小时前
IDEA + Spring Boot 的三种热加载方案
java·后端
小苏兮1 小时前
【把Linux“聊”明白】编译器gcc/g++与调试器gdb/cgdb:从编译原理到高效调试
java·linux·运维·学习·1024程序员节