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

相关推荐
我今晚不熬夜11 分钟前
Java语言实现Modbus协议通过用串口读取数据(以RTU为例)
java·网络协议
小锋java12341 小时前
Maven 4 要来了:15 年后,Java 构建工具迎来"彻底重构"
java·maven
卓怡学长1 小时前
w269基于spring boot + vue 候鸟监测数据管理系统
java·数据库·spring boot·spring·intellij-idea
孫治AllenSun1 小时前
【MDC】使用 MDC 封装一个 LogMdcUtil 工具类
java·spring boot·后端
济*沧*海2 小时前
BaseMapper的介绍
java·windows·tomcat
plainGeekDev2 小时前
依赖管理 → Version Catalog
android·java·kotlin
淘源码d3 小时前
医学影像云PACS系统——PACS诊断工作站
java·源码·影像处理·医院管理系统·影像工作站·云pacs系统·dicom 标准通信
KobeSacre4 小时前
Semaphore 源码
java·开发语言
NiceCloud喜云4 小时前
Anthropic 一周三连发:Cowork 多端、Fable 5 按需付费、J-space 论文的技术解读
java·服务器·网络·人工智能·ai
SamDeepThinking4 小时前
组合还是继承?我在项目里主要看这两个判断
java·后端·面试