Java实现在线预览Office文件

一、LibreOffice

LibreOffice是OpenOffice.org办公套件衍生版, 同样自由开源,以Mozilla Public License V2.0分发源代码 [3] ,但相比OpenOffice增加了很多特色功能。LibreOffice拥有强大的数据导入和导出功能,能直接导入PDF文档、微软Word(.doc文件)、LotusWord,支持主要的OpenXML格式。软件本身并不局限于Debian和Ubuntu平台,现已持Windows、Mac和其它Linux发行版等多个系统平台。

官方地址:
https://zh-cn.libreoffice.org/

二、SpringBoot 集成LibreOffice

1、通过LibreOffice官网下载LibreOffice安装包。

2、安装LibreOffice安装包

Java 实现在线预览Office文件

安装

3、复制安装完成后的文件夹,如下图

Java 实现在线预览Office文件

LibreOffice文件夹

Java 实现在线预览Office文件

项目

4、需要引入openoffice的jar

<!--openoffice-->
<dependency>
    <groupId>org.openoffice</groupId>
    <artifactId>juh</artifactId>
</dependency>

<dependency>
    <groupId>org.openoffice</groupId>
    <artifactId>ridl</artifactId>
</dependency>

<dependency>
    <groupId>org.openoffice</groupId>
    <artifactId>unoil</artifactId>
</dependency>

5、核心代码

/**
 * 获取XComponent对象
 * @param context
 * @param inputFile
 * @return
 */
private XComponent loadDocument(OfficeContext context,String inputFile){
    File file = new File(inputFile);
    if(!file.exists()){
        throw new MyException("input document not found");
    }
    XComponentLoader xComponentLoader = JodUtil.cast(XComponentLoader.class, context.getService(SERVICE_DESKTOP));
    Map<String,?> loadProperties = getLoadProperties(inputFile);
    try {
        xComponent = xComponentLoader.loadComponentFromURL(JodUtil.toUrl(inputFile), "_blank", 0, JodUtil.toUnoProperties(loadProperties));
        logger.info("XComponent获取成功:{}", xComponent);
    } catch (ErrorCodeIOException errorCodeIOException) {
        throw new MyException("could not load document: "  + inputFile + "; errorCode: " + errorCodeIOException.ErrCode, errorCodeIOException);
    } catch (IllegalArgumentException | IOException illegalArgumentException) {
        throw new MyException("could not load document: " + inputFile, illegalArgumentException);
    }
    if (xComponent == null) {
        throw new MyException("could not load document: "  + inputFile);
    }
    return xComponent;
}
/**
 * 文件转换
 * @param xComponent
 * @param outputFile
 */
private void storeDocument(XComponent xComponent,String outputFile){
    Map<String,?> storeProperties = getStoreProperties(inputFile);
    if (storeProperties == null) {
        throw new MyException("unsupported conversion");
    }
    try {
        XStorable xStorable = JodUtil.cast(XStorable.class, xComponent);
        xStorable.storeToURL(JodUtil.toUrl(outputFile), JodUtil.toUnoProperties(storeProperties));
        logger.info("{} 文件转成 {}",inputFile,outputFile);

    } catch (ErrorCodeIOException errorCodeIOException) {
        throw new MyException("could not store document: " + outputFile + "; errorCode: " + errorCodeIOException.ErrCode, errorCodeIOException);
    } catch (IOException ioException) {
        throw new MyException("could not store document: " + outputFile, ioException);
    } finally {
        if(xComponent != null){
            xComponent.dispose();
        }
    }
}

内容出处:,

声明:本网站所收集的部分公开资料来源于互联网,转载的目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。如果您发现网站上有侵犯您的知识产权的作品,请与我们取得联系,我们会及时修改或删除。文章链接:http://www.yixao.com/share/28405.html

发表评论

登录后才能评论