欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 教育 > 培训 > Libreoffice实现Word、Excel在线预览

Libreoffice实现Word、Excel在线预览

2025/2/24 14:36:30 来源:https://blog.csdn.net/qq_40711092/article/details/145355770  浏览:    关键词:Libreoffice实现Word、Excel在线预览

Libreoffice下载地址

https://zh-cn.libreoffice.org/download/libreoffice/
在这里插入图片描述

依赖

  <!--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>

代码

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 转 pdfFile srcFile1 = new File("D:\\test1.docx");File targetFile1 = new File("D:\\test1.pdf");JodConverter.convert(srcFile1).to(targetFile1).execute();// ppt 转 pdfFile 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

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词