在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后效果:

相关推荐
墨雨晨曦8811 分钟前
2026/08/15 spring AI学习总结
java·tomcat
wno70426 分钟前
Spring Boot JdbcTemplate配置Druid多数据源
java·spring boot·后端
聊浮游1 小时前
JAVA2026最新全套学习资料、学习路线
java·开发语言·jvm·mysql·spring·maven·idea
MacroZheng1 小时前
完美替代 Navicat!这款内置 AI 的数据库工具,太香了!
java·后端·mysql
SamDeepThinking2 小时前
从REST到gRPC,一个API选型的思考框架
java·后端·程序员
Zane19942 小时前
接口都能写默认实现了,为什么还需要抽象类
java·后端
未秃头的程序猿2 小时前
读Spring AI源码的方法——不是硬啃,是带着问题去翻
java·后端·spring
用户3126874877202 小时前
别再 Thread.sleep 硬等了!并发工具类到底怎么选?
java
Java内核笔记2 小时前
万字长文剖析 Spring Boot 4 自动配置机制源码:从 @EnableAutoConfiguration 到条件装配
java·后端
evans在进步2 小时前
Spring MVC 核心机制详解:全局异常处理、请求跳转与数据绑定
java·spring·mvc