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

相关推荐
一 乐4 小时前
电影院|基于springboot + vue电影院购票管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·论文·毕设·电影院购票管理管理系统
恼书:-(空寄4 小时前
JVM GC 日志分析 + 常见 GC 场景 + 实战参数调优
java·jvm
消失的旧时光-19434 小时前
Spring Boot 实战(五):接口工程化升级(统一返回 + 异常处理 + 错误码体系 + 异常流转机制)
java·spring boot·后端·解耦
杨凯凡6 小时前
【012】图与最短路径:了解即可
java·数据结构
比特森林探险记6 小时前
【无标题】
java·前端
椰猫子6 小时前
Javaweb(Filter、Listener、AJAX、JSON)
java·开发语言
朝新_7 小时前
【Spring AI 】核心知识体系梳理:从入门到实战
java·人工智能·spring
一 乐7 小时前
旅游|基于springboot + vue旅游信息推荐系统(源码+数据库+文档)
java·vue.js·spring boot·论文·旅游·毕设·旅游信息推荐系统
我命由我123457 小时前
Android 开发中,关于 Gradle 的 distributionUrl 的一些问题
android·java·java-ee·android studio·android jetpack·android-studio·android runtime
橙露7 小时前
SpringBoot 全局异常处理:优雅封装统一返回格式
java·spring boot·后端