小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

openOffice 轉(zhuǎn)換文件格式

 WindySky 2016-09-20

       引言:突然接到任務(wù),要將word或者ppt轉(zhuǎn)換成HTML的格式在頁面上顯示,類似于百度文庫的效果。以前也聽說過,覺得用java實(shí)現(xiàn)起來還是很簡單的。于是我就帶著我的任務(wù)以及我的好奇心出發(fā)了,在網(wǎng)上找了些資料,最終決定用OpenOffice。

  首先簡單的介紹下轉(zhuǎn)換需要的環(huán)境:

     1、轉(zhuǎn)換組要安裝openoffice軟件(下載地址:http://download./index.html

     2、需要下載jodconverter包

  在此我提供了jodconverter包(包括jodconverter2、jodconverter3,注:jodconverter2需要手動(dòng)啟動(dòng)openoffice服務(wù),如有不清楚的地方可以在我文章下面留言),下載請點(diǎn)擊...

      JODConverter是一個(gè)開源文檔轉(zhuǎn)換工具,既可以應(yīng)用于Linux平臺(tái),也可其應(yīng)用于Windows平臺(tái)。其基于OpenOffice.org或者LibreOffice。因此,文檔轉(zhuǎn)換服務(wù)器上必須安裝有OpenOffice或者LibreOffice。

  目前最新版本的JODConverter為JODConverter3.0,它要求JDK1.5以上的Java環(huán)境,同時(shí)還需要OpenOffice.org 3.x版本。本文基于最新版本3.0設(shè)計(jì)實(shí)現(xiàn),如果是版本為2,則有不同的實(shí)現(xiàn)。(版本2需要手動(dòng)啟動(dòng)OpenOffice.org服務(wù),或者創(chuàng)建Windows服務(wù)設(shè)置為開機(jī)啟動(dòng),而版本3提供了開啟服務(wù)的接口,在此我使用的是版本3)

   一切準(zhǔn)備就緒那就直接開始了...

 下面是一個(gè)比較完整的例子,可以實(shí)現(xiàn) WORD==>HTML 、PPT==>HTML、WORD==>PDF、PPT==>PDF的轉(zhuǎn)換。

  1. package core;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.File;  
  5. import java.io.FileInputStream;  
  6. import java.io.FileNotFoundException;  
  7. import java.io.IOException;  
  8. import java.io.InputStreamReader;  
  9. import java.util.Date;  
  10. import java.util.regex.Matcher;  
  11. import java.util.regex.Pattern;  
  12.   
  13. import org.artofsolving.jodconverter.OfficeDocumentConverter;  
  14. import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration;  
  15. import org.artofsolving.jodconverter.office.OfficeManager;  
  16.   
  17. //轉(zhuǎn)換文檔為pdf  
  18. public class OpenOfficePdfConvert {  
  19.   
  20.     /** 
  21.      * @param args 
  22.      */  
  23.     private static OfficeManager officeManager;  
  24.     private static String OFFICE_HOME = "d:\\Program Files\\OpenOffice.org 3";// 安裝OPenOffice  
  25.     // 的路徑  
  26.     private static int port[] = { 8100 };  
  27.   
  28.     //1、客戶上傳Word文檔到服務(wù)器  
  29.        
  30.     //2、服務(wù)器調(diào)用OpenOffice程序打開上傳的Word文檔  
  31.        
  32.     //3、OpenOffice將Word文檔另存為Html格式  
  33.   
  34.       
  35.     public File convertToHtml(String inputFile, String outputFile)  
  36.             throws FileNotFoundException {  
  37.          // 創(chuàng)建保存html的文件  
  38.         File wantFile = new File(outputFile + File.separator + new Date().getTime()  
  39.             + ".html");  
  40.         // 開啟服務(wù)器  
  41.         startService();  
  42.         // 進(jìn)行轉(zhuǎn)換  
  43.         System.out.println("進(jìn)行文檔轉(zhuǎn)換轉(zhuǎn)換:" + inputFile + " --> " + outputFile);  
  44.         OfficeDocumentConverter converter = new OfficeDocumentConverter(  
  45.                 officeManager);  
  46.         converter.convert(new File(inputFile), wantFile);  
  47.         // 關(guān)閉服務(wù)器  
  48.         stopService();  
  49.         System.out.println();  
  50.         return wantFile;  
  51.   
  52.     }  
  53.   
  54.     // 打開服務(wù)器  
  55.     public static Boolean startService() {  
  56.         DefaultOfficeManagerConfiguration configuration = new DefaultOfficeManagerConfiguration();  
  57.         try {  
  58.             System.out.println("準(zhǔn)備啟動(dòng)服務(wù)....");  
  59.             configuration.setOfficeHome(OFFICE_HOME);// 設(shè)置OpenOffice.org安裝目錄  
  60.             configuration.setPortNumbers(port); // 設(shè)置轉(zhuǎn)換端口,默認(rèn)為8100  
  61.             configuration.setTaskExecutionTimeout(1000 * 60 * 5L);// 設(shè)置任務(wù)執(zhí)行超時(shí)為5分鐘  
  62.             configuration.setTaskQueueTimeout(1000 * 60 * 60 * 24L);// 設(shè)置任務(wù)隊(duì)列超時(shí)為24小時(shí)  
  63.   
  64.             officeManager = configuration.buildOfficeManager();  
  65.             officeManager.start(); // 啟動(dòng)服務(wù)  
  66.             System.out.println("office轉(zhuǎn)換服務(wù)啟動(dòng)成功!");  
  67.             return true;  
  68.         } catch (Exception ce) {  
  69.             System.out.println("office轉(zhuǎn)換服務(wù)啟動(dòng)失敗!詳細(xì)信息:" + ce);  
  70.             return false;  
  71.         }  
  72.     }  
  73.   
  74.     // 關(guān)閉服務(wù)器  
  75.     public static void stopService() {  
  76.         System.out.println("關(guān)閉office轉(zhuǎn)換服務(wù)....");  
  77.         if (officeManager != null) {  
  78.             officeManager.stop();  
  79.         }  
  80.         System.out.println("關(guān)閉office轉(zhuǎn)換成功!");  
  81.     }  
  82.   
  83.     /* 
  84.      * 進(jìn)行測試轉(zhuǎn)換是否成功 
  85.      */  
  86.     public static void main(String[] args) {  
  87.         String inputFile = "c:\\test\\test.docx";  
  88.         String outputFile = "c:\\test";  
  89.         OpenOfficePdfConvert opc = new OpenOfficePdfConvert();  
  90.         try {  
  91.             opc.convertToHtml(inputFile,outputFile);  
  92.         } catch (FileNotFoundException e1) {  
  93.             e1.printStackTrace();  
  94.         }  
  95.         /*try { 
  96.             * 如果想看到不帶HTML標(biāo)簽的字符串可以調(diào)用這個(gè)方法進(jìn)行簡化 
  97.    System.out.println(toHtmlString(inputFile, outputFile));} catch (FileNotFoundException e) { 
  98.             e.printStackTrace(); 
  99.         }*/  
  100.         System.out.println("恭喜您,轉(zhuǎn)換成功...");  
  101.     }  
  102.   
  103.     /** 
  104.      * 將word轉(zhuǎn)換成html文件,并且獲取html文件代碼。 
  105.      *  
  106.      * @param docFile 
  107.      *            需要轉(zhuǎn)換的文檔 
  108.      * @param filepath 
  109.      *            文檔中圖片的保存位置 
  110.      * @return 轉(zhuǎn)換成功的html代碼 
  111.      * @throws FileNotFoundException 
  112.      */  
  113.     public static String toHtmlString(String docFile, String filepath)  
  114.             throws FileNotFoundException {  
  115.         System.out.println("文檔中圖片的保存位置 ==>" + filepath);  
  116.         // 轉(zhuǎn)換word文檔  
  117.         OpenOfficePdfConvert opc = new OpenOfficePdfConvert();  
  118.         File htmlFile = opc.convertToHtml(docFile, filepath);  
  119.         // 獲取html文件流  
  120.         StringBuffer htmlSb = new StringBuffer();  
  121.         try {  
  122.             BufferedReader br = new BufferedReader(new InputStreamReader(  
  123.                     new FileInputStream(htmlFile)));  
  124.             while (br.ready()) {  
  125.                 htmlSb.append(br.readLine());  
  126.             }  
  127.             br.close();  
  128.             // 刪除臨時(shí)文件  
  129.             // htmlFile.delete();  
  130.         } catch (FileNotFoundException e) {  
  131.             e.printStackTrace();  
  132.         } catch (IOException e) {  
  133.             e.printStackTrace();  
  134.         }  
  135.         // HTML文件字符串  
  136.         String htmlStr = htmlSb.toString();  
  137.         // 返回經(jīng)過清潔的html文本  
  138.         return clearFormat(htmlStr, filepath);  
  139.     }  
  140.   
  141.     /** 
  142.      * 清除一些不需要的html標(biāo)記 
  143.      *  
  144.      * @param htmlStr  帶有復(fù)雜html標(biāo)記的html語句 
  145.      *          
  146.      * @return 去除了不需要html標(biāo)記的語句 
  147.      */  
  148.     protected static String clearFormat(String htmlStr, String docImgPath) {  
  149.         // 獲取body內(nèi)容的正則  
  150.         String bodyReg = "<BODY .*</BODY>";  
  151.         Pattern bodyPattern = Pattern.compile(bodyReg);  
  152.         Matcher bodyMatcher = bodyPattern.matcher(htmlStr);  
  153.         if (bodyMatcher.find()) {  
  154.             // 獲取BODY內(nèi)容,并轉(zhuǎn)化BODY標(biāo)簽為DIV  
  155.             htmlStr = bodyMatcher.group().replaceFirst("<BODY", "<DIV")  
  156.                     .replaceAll("</BODY>", "</DIV>");  
  157.         }  
  158.         // 調(diào)整圖片地址  
  159.         htmlStr = htmlStr.replaceAll("<IMG SRC=\"", "<IMG SRC=\"" + docImgPath  
  160.                 + "/");  
  161.         // 把<P></P>轉(zhuǎn)換成</div></div>保留樣式  
  162.         // content = content.replaceAll("(<P)([^>]*>.*?)(<\\/P>)",  
  163.         // "<div$2</div>");  
  164.         // 把<P></P>轉(zhuǎn)換成</div></div>并刪除樣式  
  165.         htmlStr = htmlStr.replaceAll("(<P)([^>]*)(>.*?)(<\\/P>)", "<p$3</p>");  
  166.         // 刪除不需要的標(biāo)簽  
  167.         htmlStr = htmlStr  
  168.                 .replaceAll(  
  169.                         "<[/]?(font|FONT|span|SPAN|xml|XML|del|DEL|ins|INS|meta|META|[ovwxpOVWXP]:\\w+)[^>]*?>",  
  170.                         "");  
  171.         // 刪除不需要的屬性  
  172.         htmlStr = htmlStr  
  173.                 .replaceAll(  
  174.                         "<([^>]*)(?:lang|LANG|class|CLASS|style|STYLE|size|SIZE|face|FACE|[ovwxpOVWXP]:\\w+)=(?:'[^']*'|\"\"[^\"\"]*\"\"|[^>]+)([^>]*)>",  
  175.                         "<$1$2>");  
  176.         return htmlStr;  
  177.     }  
  178. }  


主要類說明:
OfficeManager是一個(gè)接口,主要定義了三個(gè)方法:
 1.public void start( )啟動(dòng)OpenOffice服務(wù)
 2.public void stop( )停止OpenOffice服務(wù)
 3.public void execute(OfficeTask task)執(zhí)行轉(zhuǎn)換任務(wù)
 
 DefaultOfficeManagerConfiguration是一個(gè)實(shí)現(xiàn)了OfficeManager接口的實(shí)體類,其提供了相關(guān)方法配置OpenOffice.org,比如:
 
 public DefaultOfficeManagerConfiguration setOfficeHome(String officeHome)設(shè)置OpenOffice.org或者LibreOffice安裝目錄,
  windows下默認(rèn)值為” C:\Program Files\OpenOffice.org 3”(LibreOffice進(jìn)行相應(yīng)更改),因此如果OpenOffice.org安裝在別的目錄,必須設(shè)置此項(xiàng)。
 
 public DefaultOfficeManagerConfiguration setConnectionProtocol(OfficeConnectionProtocol conn)設(shè)置連接協(xié)議,確定使用管道通信,還是socekt通信。
 
 pubcli DefaultOfficeManagerConfiguration setTemplateProfileDir(File templateProfileDir)設(shè)定臨時(shí)目錄。
 
 除以上幾個(gè)方法之外,DefaultOfficeManagerConfiguration還提供了別的配置OpenOffice.org的方法,具體方法可以查詢JODConverter API手冊。
 配置完之后,必須要執(zhí)行方法buildOfficeManager(),實(shí)現(xiàn)真正的配置。
 
 OfficeDocumentConverter中主要包含convert方法,該方法實(shí)際上調(diào)用的是實(shí)現(xiàn)OfficeManager接口的類中的execute方法。

   整體看起來還是比較簡單的,但對自己也是一種提高,在此特別感謝肖恩也有夢想http://www.cnblogs.com/luckyxiaoxuan/archive/2012/06/14/2549012.html

    本站是提供個(gè)人知識管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多