1.首先需要下載并安裝openOffice,下載地址為:?Apache OpenOffice download | SourceForge.net
2.安裝后,可以測試下是否可用;
3.build.gradle中引入依賴:
implementation group: 'com.artofsolving', name: 'jodconverter', version: '2.2.1'
implementation group: 'com.github.livesense', name: 'jodconverter-core', version: '1.0.5'
implementation group: 'org.jodconverter', name: 'jodconverter-local', version: '4.4.2'
4.創(chuàng)建工具類,啟動openOffice服務(wù)的方法
private static OfficeManager officeManager;
private static int port[] = {8100};
/**
* start openOffice service.
*/
public void startService() {
DefaultOfficeManagerConfiguration configuration = new DefaultOfficeManagerConfiguration();
try {
System.out.println("準(zhǔn)備啟動office轉(zhuǎn)換服務(wù)....");
configuration.setOfficeHome("這里的路徑一般為C:\\Program Files (x86)\\OpenOffice 4");// 設(shè)置OpenOffice.org安裝目錄
configuration.setPortNumbers(port); // 設(shè)置轉(zhuǎn)換端口,默認(rèn)為8100
configuration.setTaskExecutionTimeout(1000 * 60 * 30L);// 設(shè)置任務(wù)執(zhí)行超時為30分鐘
configuration.setTaskQueueTimeout(1000 * 60 * 60 * 24L);// 設(shè)置任務(wù)隊列超時為24小時
officeManager = configuration.buildOfficeManager();
officeManager.start(); // 啟動服務(wù)
System.out.println("office轉(zhuǎn)換服務(wù)啟動成功!");
} catch (Exception e) {
System.out.println("office轉(zhuǎn)換服務(wù)啟動失敗!詳細(xì)信息:" + e);
}
}
5.結(jié)束openOffice服務(wù)的方法
/**
* stop openOffice service.
*/
public void stopService() {
System.out.println("準(zhǔn)備關(guān)閉office轉(zhuǎn)換服務(wù)....");
if (officeManager != null) {
officeManager.stop();
}
System.out.println("office轉(zhuǎn)換服務(wù)關(guān)閉成功!");
}
7.在測試方法中進(jìn)行格式轉(zhuǎn)換,如,他可以是任意類型轉(zhuǎn)換,如excel轉(zhuǎn)換為pdf,word轉(zhuǎn)換為pdf,只需要你傳入一個任意類型文件,輸出一個任意類型文件即可。
public void convertToPDF(String inputFile, String outputFile){
startService();
System.out.println("進(jìn)行文檔轉(zhuǎn)換轉(zhuǎn)換:" + inputFile + " --> " + outputFile);
OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);
converter.convert(new File(inputFile), new File(outputFile));
stopService();
}
效果預(yù)覽:
xlsx轉(zhuǎn)換前效果:
?轉(zhuǎn)換為pdf后效果:
文章來源:http://www.zghlxwxcb.cn/news/detail-625414.html
?文章來源地址http://www.zghlxwxcb.cn/news/detail-625414.html
到了這里,關(guān)于在java中如何使用openOffice進(jìn)行格式轉(zhuǎn)換,word,excel,ppt,pdf互相轉(zhuǎn)換的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!