Spring Boot 集成 OpenOffice

/ 代码

Spring Boot 集成 OpenOffice

Maven引入jar

<dependency>
	<groupId>org.openoffice</groupId>
	<artifactId>juh</artifactId>
	<version>3.0.1</version>
</dependency>

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

<dependency>
	<groupId>com.artofsolving</groupId>
	<artifactId>jodconverter</artifactId>
	<version>2.2.2</version>
</dependency>

<dependency>
	<groupId>commons-io</groupId>
	<artifactId>commons-io</artifactId>
	<version>2.6</version>
</dependency>

代码集成

application.yml添加openOffice配置

#openOffice配置
open:
  officce:
    ip: 127.0.0.1
    port: 8100

创建OpenOfficeProperties.java

/**
 * @author: lsyzx (zhux@zhengebang.com)
 * 创建时间: 2019-05-24 18:34:47
 * 描述: OpenOffice配置
 * 历史修改:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
@Data
@ConfigurationProperties(prefix = "open.officce")
public class OpenOfficeProperties {

    /**
     * ip地址
     */
    private String ip;

    /**
     * 端口
     */
    private Integer port;


    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }
}

创建OpenOfficeConfiguration.java

/**
 * @author: lsyzx (zhux@zhengebang.com)
 * 创建时间: 2019-05-24 18:34:47
 * 描述:OpenOffice配置
 * 历史修改:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
@Configuration
@EnableConfigurationProperties(OpenOfficeProperties.class)
public class OpenOfficeConfiguration {
    private OpenOfficeProperties properties;

    @Autowired
    public OpenOfficeConfiguration(OpenOfficeProperties properties) {
        this.properties = properties;
    }

    @Bean
    @ConditionalOnMissingBean
    public OpenOfficeConnection openOfficeConnection() {
        OpenOfficeConnection connection = new SocketOpenOfficeConnection(properties.getIp(), properties.getPort());
        return connection;
    }

}

转换测试

@Autowired
    private OpenOfficeConnection openOfficeConnection;

    /**
     * office转换pdf,支持doc,.docx, .xls, .xlsx, .ppt, .pptx
     *
     * @param sourceFile
     * @param destFile
     * @return
     */
    @ApiOperation(value = "office转换pdf,支持doc,.docx, .xls, .xlsx, .ppt, .pptx", notes = "office转换pdf,支持doc,.docx, .xls, .xlsx, .ppt, .pptx")
    @GetMapping(value = "/officeConvertPdf")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sourceFile", value = "源文件地址", dataType = "string", required = true, paramType = "query"),
            @ApiImplicitParam(name = "destFile", value = "生成文件地址", dataType = "string", required = true, paramType = "query"),
    })
    public ApiResult officeConvertPdf(String sourceFile, String destFile) {
        try {
            //原文件
            File inputFile = new File(sourceFile);
            // 如果目标路径不存在, 则新建该路径
            File outputFile = new File(destFile);
            if (!outputFile.getParentFile().exists()) {
                outputFile.getParentFile().mkdirs();
            }
            //连接远程openOffice服务
            openOfficeConnection.connect();
            //获取文档转换器
            DocumentConverter converter = new StreamOpenOfficeDocumentConverter(openOfficeConnection);
            //执行转换
            converter.convert(inputFile, outputFile);
            // close the connection
            openOfficeConnection.disconnect();
            return new ApiResult(CommonEnum.SUCCESS, destFile);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("office转换异常", e);
            return new ApiResult(CommonEnum.FAIL);
        }
    }