在java中如何使用openOffice进行格式转换,word,excel,ppt,pdf互相转换

1.首先需要下载并安装openOffice,下载地址为:Apache OpenOffice download | SourceForge.net

2.安装后,可以测试下是否可用;

3.build.gradle中引入依赖:

java 复制代码
implementation group: 'com.artofsolving', name: 'jodconverter', version: '2.2.1'
implementation group: 'com.github.livesense', name: 'jodconverter-core', version: '1.0.5'
implementation group: 'org.jodconverter', name: 'jodconverter-local', version: '4.4.2'

4.创建工具类,启动openOffice服务的方法

java 复制代码
    private static OfficeManager officeManager;
    private static int port[] = {8100};
/**
     * start openOffice service.
     */
    public void startService() {
        DefaultOfficeManagerConfiguration configuration = new DefaultOfficeManagerConfiguration();
        try {
            System.out.println("准备启动office转换服务....");
            configuration.setOfficeHome("这里的路径一般为C:\\Program Files (x86)\\OpenOffice 4");// 设置OpenOffice.org安装目录
            configuration.setPortNumbers(port); // 设置转换端口,默认为8100
            configuration.setTaskExecutionTimeout(1000 * 60 * 30L);// 设置任务执行超时为30分钟
            configuration.setTaskQueueTimeout(1000 * 60 * 60 * 24L);// 设置任务队列超时为24小时
            officeManager = configuration.buildOfficeManager();
            officeManager.start(); // 启动服务
            System.out.println("office转换服务启动成功!");
        } catch (Exception e) {
            System.out.println("office转换服务启动失败!详细信息:" + e);
        }
    }

5.结束openOffice服务的方法

java 复制代码
 /**
     * stop openOffice service.
     */
    public void stopService() {
        System.out.println("准备关闭office转换服务....");
        if (officeManager != null) {
            officeManager.stop();
        }
        System.out.println("office转换服务关闭成功!");
    }

7.在测试方法中进行格式转换,如,他可以是任意类型转换,如excel转换为pdf,word转换为pdf,只需要你传入一个任意类型文件,输出一个任意类型文件即可。

java 复制代码
    public void convertToPDF(String inputFile, String outputFile){
        startService();
        System.out.println("进行文档转换转换:" + inputFile + " --> " + outputFile);
        OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);
        converter.convert(new File(inputFile), new File(outputFile));
        stopService();
    }

效果预览:

xlsx转换前效果:

转换为pdf后效果:

相关推荐
魏 无羡3 小时前
webclient
java·webclient
神仙别闹4 小时前
基于 C++ 实现两个有序链表序列的交集
java·c++·链表
swordbob5 小时前
ReentrantLock 与 AQS 完整学习手册
java·开发语言
m0_587383006 小时前
全民健身解决方案软件开发实战:从架构设计到落地指南
java·spring boot·spring·架构·需求分析
白山编程大哥6 小时前
Java OutputStreamWriter 详解:从字符到字节的桥梁
java·开发语言·python
一技安身7 小时前
【信创】Docker‑Compose V2 两种离线部署(独立模式、插件模式)简易教程
java·docker·eureka
七夜zippoe7 小时前
为什么 2026 年每个 Java 团队都该懂 AI Agent
java·开发语言·人工智能
时凌云.7 小时前
【2026最新】JDK 下载安装与环境配置全教程(Windows/Mac/Linux 三平台,零基础友好)
java·linux·macos
Despacito10067 小时前
Java后端性能探查工具速查表
java·开发语言
蓝桉柒78 小时前
下载idea,用idea输入一个程序
java·ide·intellij-idea