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

相关推荐
曹牧1 小时前
BeanUtils.copyProperties‌
java
QWQ___qwq2 小时前
Java线程安全深度总结:基本类型与引用类型的本质区别
java·安全·面试
识君啊2 小时前
Java异常处理:中小厂面试通关指南
java·开发语言·面试·异常处理·exception·中小厂
月月玩代码4 小时前
Actuator,Spring Boot应用监控与管理端点!
java·spring boot·后端
阿珍爱上了阿强,在一个有星星的夜晚5 小时前
node后端页面性能监测分析
java·学习方法
Java程序之猿5 小时前
SpringBoot + camel+IBM MQ实现消息队列处理
java·spring boot·mybatis
z_鑫5 小时前
SpringCloud FeignClient 中 Bean 重复注册冲突解决方案解析
java·spring boot·spring cloud
城数派5 小时前
全国各省/直辖市/自治区CLCD1985~2024年30米土地利用数据(分省裁剪)
数据分析·excel
孫治AllenSun6 小时前
【线程池】优化等待队列和拒绝策略
java·spring boot·spring cloud