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

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

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

1 介绍下OpenOffice

官网:https://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月前
|
人工智能 自然语言处理 JavaScript
Univer:开源全栈 AI 办公工具,支持 Word、Excel、PPT 等文档处理和多人实时协作
Univer 是一款开源的 AI 办公工具,支持 Word、Excel 等文档处理的全栈解决方案。它具有强大的功能、高度的可扩展性和跨平台兼容性,适用于个人和企业用户,能够显著提高工作效率。
177 8
Univer:开源全栈 AI 办公工具,支持 Word、Excel、PPT 等文档处理和多人实时协作
|
6月前
|
XML 前端开发 Java
基于SpringBoot 3.3实现任意文件在线预览功能的技术分享
【8月更文挑战第30天】在当今的数字化办公环境中,文件在线预览已成为提升工作效率、优化用户体验的重要功能之一。无论是文档、图片、PDF还是代码文件,用户都期望能够直接在浏览器中快速查看而无需下载。本文将围绕如何在Spring Boot 3.3框架下实现这一功能,分享一系列技术干货,助力开发者高效构建文件预览服务。
531 3
|
3月前
|
前端开发 Java easyexcel
SpringBoot操作Excel实现单文件上传、多文件上传、下载、读取内容等功能
SpringBoot操作Excel实现单文件上传、多文件上传、下载、读取内容等功能
221 8
|
9月前
|
XML JavaScript 前端开发
springboot配合Freemark模板生成word,前台vue接收并下载【步骤详解并奉上源码】
springboot配合Freemark模板生成word,前台vue接收并下载【步骤详解并奉上源码】
333 2
|
4月前
|
easyexcel Java UED
SpringBoot中大量数据导出方案:使用EasyExcel并行导出多个excel文件并压缩zip后下载
在SpringBoot环境中,为了优化大量数据的Excel导出体验,可采用异步方式处理。具体做法是将数据拆分后利用`CompletableFuture`与`ThreadPoolTaskExecutor`并行导出,并使用EasyExcel生成多个Excel文件,最终将其压缩成ZIP文件供下载。此方案提升了导出效率,改善了用户体验。代码示例展示了如何实现这一过程,包括多线程处理、模板导出及资源清理等关键步骤。
|
4月前
|
JSON 数据格式
LangChain-20 Document Loader 文件加载 加载MD DOCX EXCEL PPT PDF HTML JSON 等多种文件格式 后续可通过FAISS向量化 增强检索
LangChain-20 Document Loader 文件加载 加载MD DOCX EXCEL PPT PDF HTML JSON 等多种文件格式 后续可通过FAISS向量化 增强检索
290 2
|
5月前
|
前端开发 Java easyexcel
SpringBoot操作Excel实现单文件上传、多文件上传、下载、读取内容等功能
SpringBoot操作Excel实现单文件上传、多文件上传、下载、读取内容等功能
86 6
|
7月前
|
Web App开发 前端开发 安全
2024年新一代WebOffice内嵌网页组件,Web网页在线编辑Word/Excel/PPT
WebOffice控件面临兼容性、用户体验和维护难题。随着浏览器更新,依赖插件的技术不再适用,如Chrome不再支持NPAPI和PPAPI。产品普遍不支持多版本Office并存,定制能力弱,升级复杂。猿大师办公助手提供了解决方案,它兼容多种浏览器,包括最新版和国产浏览器,不依赖插件,支持文档对比,具有丰富的功能和接口,兼容多种Office版本,允许源码级定制,提供终身技术支持,并实现静默在线升级。适用于多种行业和操作系统。
434 17
|
6月前
|
JavaScript 前端开发 easyexcel
基于SpringBoot + EasyExcel + Vue + Blob实现导出Excel文件的前后端完整过程
本文展示了基于SpringBoot + EasyExcel + Vue + Blob实现导出Excel文件的完整过程,包括后端使用EasyExcel生成Excel文件流,前端通过Blob对象接收并触发下载的操作步骤和代码示例。
1035 0
基于SpringBoot + EasyExcel + Vue + Blob实现导出Excel文件的前后端完整过程
|
6月前
|
Java BI API
SpringBoot + POI-TL:高效生成Word报表的技术盛宴
【8月更文挑战第22天】在日常的工作与学习中,文档处理特别是Word报表的自动生成,是许多项目中不可或缺的一环。传统的手工编辑Word文档不仅效率低下,还容易出错,特别是在需要批量生成包含动态数据的报告时,更是令人头疼不已。幸运的是,结合Spring Boot的简洁高效与POI-TL的强大功能,我们能够轻松实现Word报表的快速生成,既提升了工作效率,又保证了数据的准确性和报表的美观性。
584 0

热门文章

最新文章