jodconverter是基于OpenOffice和LibreOffice的Office转码工具
废话不多说直接上代码:
maven依赖:
<dependency>
<groupId>org.jodconverter</groupId>
<artifactId>jodconverter-local</artifactId>
<version>4.2.2</version>
</dependency>
主方法:
/**
* @author FastKing
* @version 1.0
* @date 2019/1/16 10:14
**/
public class OpenOfficeTest {
public static void main(String[] args) {
try {
//据说jodconverter可以自动寻找转码路径,windows上试了可以,linux上好像没法自动找到路径,会报officehome not set and could not be auto-detected,所以设置转码工具路径
LocalOfficeManager manager = LocalOfficeManager.builder().officeHome("/opt/libreoffice6.1").install().build();
manager.start();
JodConverter.convert(new File("F:\\test.xlsx")).to(new File("F:\\test.pdf")).execute();
} catch (OfficeException e) {
e.printStackTrace();
}
}
}