Spring Boot整合OpenOffice实现Word、Excel、PPT在线预览

简介: Spring Boot整合OpenOffice实现Word、Excel、PPT在线预览

Spring Boot整合OpenOffice实现Word、Excel、PPT在线预览

1 介绍下OpenOffice

官网:www.openoffice.org/download/

Apache OpenOffice是一款先进的开源办公软件套件,它包含文本文档电子表格演示文稿绘图数据库等。 它能够支持许多语言并且在所有普通计算机上工作。它将你所有的数据以国际开放标准格式存储下来,并能够读写从其它常用办公软件包来的文件。它可以被完全免费下载并使用于任何用途

2 安装OpenOffice

网络异常,图片无法展示
|


然后直接下一步安装就可以了,步骤过于简单这里省略,如有问题可以留言哈

3 Spring Boot整合

新建Spring Boot项目

3.1 依赖

<!--jodconverter 核心包 -->
<dependency>
    <groupId>org.jodconverter</groupId>
    <artifactId>jodconverter-core</artifactId>
    <version>4.2.2</version>
</dependency>
<!--springboot支持包,里面包括了自动配置类 -->
<dependency>
    <groupId>org.jodconverter</groupId>
    <artifactId>jodconverter-spring-boot-starter</artifactId>
    <version>4.2.2</version>
</dependency>
<!--jodconverter 本地支持包 -->
<dependency>
    <groupId>org.jodconverter</groupId>
    <artifactId>jodconverter-local</artifactId>
    <version>4.2.2</version>
</dependency>
<!-- commons-io -->
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.6</version>
</dependency>
复制代码

3.2 配置文件

server.port=9999
#使能
jodconverter.local.enabled=true
#OpenOffice安装地址
jodconverter.local.office-home=C:/Program Files (x86)/OpenOffice 4
#同时执行任务的个数
jodconverter.local.max-tasks-per-process=10
#设置端口号(任意设置)
jodconverter.local.port-numbers=8110
#controller工具参数
online.buffer.path=C:/online_pdf/
online.buffer.file.name=hello.pdf
复制代码

3.3 项目结构

网络异常,图片无法展示
|


3.4 编码

OnlineDocDao.java

/**
 * @desc: 模拟Dao层
 * @author: YanMingXin
 * @create: 2021/10/4-20:28
 **/
@Repository
public class OnlineDocDao {
    /**
     * 获取文件
     *
     * @param fileIndex
     * @return
     */
    @SuppressWarnings("all")
    public String getFiles(int fileIndex) {
        switch (fileIndex) {
            case 1:
                return "file1.docx";
            case 2:
                return "file2.docx";
            case 3:
                return "file3.docx";
            case 4:
                return "1.xlsx";
            default:
                return "file1.docx";
        }
    }
}
复制代码

OnlineDocService.java

/**
 * @desc: Service接口
 * @author: YanMingXin
 * @create: 2021/10/4-20:27
 **/
public interface OnlineDocService {
    /**
     * 根据索引获取文件名称
     *
     * @param fileIndex
     * @return
     */
    String readDoc(int fileIndex);
}
复制代码

OnlineDocServiceImpl.java

/**
 * @desc: Service实现类
 * @author: YanMingXin
 * @create: 2021/10/4-20:27
 **/
@Service
public class OnlineDocServiceImpl implements OnlineDocService {
    @Autowired
    private OnlineDocDao onlineDocDao;
    @Override
    public String readDoc(int fileIndex) {
        return onlineDocDao.getFiles(fileIndex);
    }
}
复制代码

OnlineDocController.java

/**
 * @desc: 控制类
 * @author: YanMingXin
 * @create: 2021/10/5-9:50
 **/
@Controller
@SuppressWarnings("all")
public class OnlineDocController {
    /**
     * 注入DocumentConverter(jodconverter内置)
     */
    @Autowired
    private DocumentConverter converter;
    /**
     * 注入Service
     */
    @Autowired
    private OnlineDocService onlineDocService;
    @Value("${online.buffer.path}")
    private String bufferPath;
    @Value("${online.buffer.file.name}")
    private String bufferFileName;
    /**
     * 主要业务逻辑,处理文件请求
     *
     * @param index
     * @param response
     */
    @RequestMapping("/toPdfFile/{index}")
    public void toPdfFile(@PathVariable("index") Integer index, HttpServletResponse response) {
        String fileName = onlineDocService.readDoc(index);
        File file = new File("src/main/resources/static/" + fileName);
        ServletOutputStream outputStream = null;
        InputStream in = null;
        //转换之后文件生成的地址
        File newFile = new File(bufferPath);
        if (!newFile.exists()) newFile.mkdirs();
        try {
            //文件转化
            converter.convert(file).to(new File(bufferPath + bufferFileName)).execute();
            outputStream = response.getOutputStream();
            in = new FileInputStream(new File(bufferPath + bufferFileName));
            IOUtils.copy(in, outputStream);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (in != null) in.close();
                if (outputStream != null) outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
复制代码

3.5 启动测试

网络异常,图片无法展示
|


4 结语

4.1 为什么需要缓冲层?

网络异常,图片无法展示
|


4.2 OpenOffice端口号问题

因为看网上相同的文章的时候,端口号都写的8100,还以为OpenOffice的默认端口号是8100,但是改过了之后才发现,应该是Java连接OpenOffice时需要用到的进程端口号,可以随意设置,所以这个就不用管啦


相关文章
|
2月前
|
Java 应用服务中间件 Maven
SpringBoot 项目瘦身指南
SpringBoot 项目瘦身指南
54 0
|
2天前
aspose实现word,excel等文件预览
aspose实现word,excel等文件预览
|
25天前
|
安全 Java 应用服务中间件
江帅帅:Spring Boot 底层级探索系列 03 - 简单配置
江帅帅:Spring Boot 底层级探索系列 03 - 简单配置
29 0
江帅帅:Spring Boot 底层级探索系列 03 - 简单配置
|
27天前
|
Web App开发 前端开发 安全
如何用JAVA如何实现Word、Excel、PPT在线前端预览编辑?
随着信息化的发展,在线办公也日益成为了企业办公和个人学习不可或缺的一部分,作为微软Office的三大组成部分:Word、Excel和PPT也广泛应用于各种在线办公场景,但是由于浏览器限制及微软Office的不开源等特性,导致Word、Excel和PPT在在线办公很难整合到自己公司的OA或者文档系统。
365 2
|
27天前
|
XML Java C++
【Spring系列】Sping VS Sping Boot区别与联系
【4月更文挑战第2天】Spring系列第一课:Spring Boot 能力介绍及简单实践
【Spring系列】Sping VS Sping Boot区别与联系
|
2月前
|
Web App开发 JavaScript 前端开发
2024年纯前端VUE在线编辑微软Office/金山WPS的Word/Excel文档
现在,随着数字化进程渗透到到各行各业,数据安全已经成为了数字化革命中的重要组成部分,而在线Office成在OA、ERP、文档系统中得到了广泛的应用,为我国的信息化事业也做出了巨大贡献。随着操作系统、浏览器及Office软件的不断升级和更新换代,加上国家对信息化、数字化系统要求的不断提升,一些厂家的WebOffice控件产品不断被淘汰出局,而现存的几个产品也存在以下几个问题:
447 1
2024年纯前端VUE在线编辑微软Office/金山WPS的Word/Excel文档
|
3月前
|
Java Linux 数据安全/隐私保护
Java【代码 16】将word、excel文件转换为pdf格式和将pdf文档转换为image格式工具类分享(Gitee源码)aspose转换中文乱码问题处理
【2月更文挑战第3天】Java 将word、excel文件转换为pdf格式和将pdf文档转换为image格式工具类分享(Gitee源码)aspose转换中文乱码问题处理
123 0
|
1月前
|
easyexcel
【EasyExcel】第二篇:导出excel文件,导出多个sheet工作空间
【EasyExcel】第二篇:导出excel文件,导出多个sheet工作空间
|
1月前
|
SQL 数据库连接 数据库
【SQL Server】2. 将数据导入导出到Excel表格当中
【SQL Server】2. 将数据导入导出到Excel表格当中
47 0
|
1月前
|
JavaScript 前端开发
【导出Excel】Vue实现导出下载Excel文件(blob文件流)--亲测可用
【导出Excel】Vue实现导出下载Excel文件(blob文件流)--亲测可用
【导出Excel】Vue实现导出下载Excel文件(blob文件流)--亲测可用