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

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

1 介绍下OpenOffice

官网:https://www.openoffice.org/download/

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

2 安装OpenOffice

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-cMq7QeDB-1633403027895)(Spring Boot整合OpenOffice实现Word、Excel、PPT在线预览.assets/image-20211005104228963.png)]

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

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 项目结构

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-yE2496zq-1633403027900)(Spring Boot整合OpenOffice实现Word、Excel、PPT在线预览.assets/image-20211005104527784.png)]

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 启动测试

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-vHA0Tz9h-1633403027904)(Spring Boot整合OpenOffice实现Word、Excel、PPT在线预览.assets/image-20211005104735793.png)]

4 结语

4.1 为什么需要缓冲层?

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ZVkfzFQt-1633403027913)(Spring Boot整合OpenOffice实现Word、Excel、PPT在线预览.assets/image-20211005105754437.png)]

4.2 OpenOffice端口号问题

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

猜你喜欢

转载自blog.csdn.net/Mr_YanMingXin/article/details/120611941