国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
openOffice 轉換文件格式

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

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

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

     2、需要下載jodconverter包

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

      JODConverter是一個開源文檔轉換工具,既可以應用于Linux平臺,也可其應用于Windows平臺。其基于OpenOffice.org或者LibreOffice。因此,文檔轉換服務器上必須安裝有OpenOffice或者LibreOffice。

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

   一切準備就緒那就直接開始了...

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

  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. //轉換文檔為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文檔到服務器  
  29.        
  30.     //2、服務器調(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.         // 開啟服務器  
  41.         startService();  
  42.         // 進行轉換  
  43.         System.out.println("進行文檔轉換轉換:" + inputFile + " --> " + outputFile);  
  44.         OfficeDocumentConverter converter = new OfficeDocumentConverter(  
  45.                 officeManager);  
  46.         converter.convert(new File(inputFile), wantFile);  
  47.         // 關閉服務器  
  48.         stopService();  
  49.         System.out.println();  
  50.         return wantFile;  
  51.   
  52.     }  
  53.   
  54.     // 打開服務器  
  55.     public static Boolean startService() {  
  56.         DefaultOfficeManagerConfiguration configuration = new DefaultOfficeManagerConfiguration();  
  57.         try {  
  58.             System.out.println("準備啟動服務....");  
  59.             configuration.setOfficeHome(OFFICE_HOME);// 設置OpenOffice.org安裝目錄  
  60.             configuration.setPortNumbers(port); // 設置轉換端口,默認為8100  
  61.             configuration.setTaskExecutionTimeout(1000 * 60 * 5L);// 設置任務執(zhí)行超時為5分鐘  
  62.             configuration.setTaskQueueTimeout(1000 * 60 * 60 * 24L);// 設置任務隊列超時為24小時  
  63.   
  64.             officeManager = configuration.buildOfficeManager();  
  65.             officeManager.start(); // 啟動服務  
  66.             System.out.println("office轉換服務啟動成功!");  
  67.             return true;  
  68.         } catch (Exception ce) {  
  69.             System.out.println("office轉換服務啟動失敗!詳細信息:" + ce);  
  70.             return false;  
  71.         }  
  72.     }  
  73.   
  74.     // 關閉服務器  
  75.     public static void stopService() {  
  76.         System.out.println("關閉office轉換服務....");  
  77.         if (officeManager != null) {  
  78.             officeManager.stop();  
  79.         }  
  80.         System.out.println("關閉office轉換成功!");  
  81.     }  
  82.   
  83.     /* 
  84.      * 進行測試轉換是否成功 
  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標簽的字符串可以調(diào)用這個方法進行簡化 
  97.    System.out.println(toHtmlString(inputFile, outputFile));} catch (FileNotFoundException e) { 
  98.             e.printStackTrace(); 
  99.         }*/  
  100.         System.out.println("恭喜您,轉換成功...");  
  101.     }  
  102.   
  103.     /** 
  104.      * 將word轉換成html文件,并且獲取html文件代碼。 
  105.      *  
  106.      * @param docFile 
  107.      *            需要轉換的文檔 
  108.      * @param filepath 
  109.      *            文檔中圖片的保存位置 
  110.      * @return 轉換成功的html代碼 
  111.      * @throws FileNotFoundException 
  112.      */  
  113.     public static String toHtmlString(String docFile, String filepath)  
  114.             throws FileNotFoundException {  
  115.         System.out.println("文檔中圖片的保存位置 ==>" + filepath);  
  116.         // 轉換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.             // 刪除臨時文件  
  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標記 
  143.      *  
  144.      * @param htmlStr  帶有復雜html標記的html語句 
  145.      *          
  146.      * @return 去除了不需要html標記的語句 
  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)容,并轉化BODY標簽為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>轉換成</div></div>保留樣式  
  162.         // content = content.replaceAll("(<P)([^>]*>.*?)(<\\/P>)",  
  163.         // "<div$2</div>");  
  164.         // 把<P></P>轉換成</div></div>并刪除樣式  
  165.         htmlStr = htmlStr.replaceAll("(<P)([^>]*)(>.*?)(<\\/P>)", "<p$3</p>");  
  166.         // 刪除不需要的標簽  
  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是一個接口,主要定義了三個方法:
 1.public void start( )啟動OpenOffice服務
 2.public void stop( )停止OpenOffice服務
 3.public void execute(OfficeTask task)執(zhí)行轉換任務
 
 DefaultOfficeManagerConfiguration是一個實現(xiàn)了OfficeManager接口的實體類,其提供了相關方法配置OpenOffice.org,比如:
 
 public DefaultOfficeManagerConfiguration setOfficeHome(String officeHome)設置OpenOffice.org或者LibreOffice安裝目錄,
  windows下默認值為” C:\Program Files\OpenOffice.org 3”(LibreOffice進行相應更改),因此如果OpenOffice.org安裝在別的目錄,必須設置此項。
 
 public DefaultOfficeManagerConfiguration setConnectionProtocol(OfficeConnectionProtocol conn)設置連接協(xié)議,確定使用管道通信,還是socekt通信。
 
 pubcli DefaultOfficeManagerConfiguration setTemplateProfileDir(File templateProfileDir)設定臨時目錄。
 
 除以上幾個方法之外,DefaultOfficeManagerConfiguration還提供了別的配置OpenOffice.org的方法,具體方法可以查詢JODConverter API手冊。
 配置完之后,必須要執(zhí)行方法buildOfficeManager(),實現(xiàn)真正的配置。
 
 OfficeDocumentConverter中主要包含convert方法,該方法實際上調(diào)用的是實現(xiàn)OfficeManager接口的類中的execute方法。

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

本站僅提供存儲服務,所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權內(nèi)容,請點擊舉報
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
仿百度文庫解決方案(四)——利用JODConverter調(diào)用OpenOffice.org服務轉換文檔為PDF
Java+FlexPaper+swfTools仿百度文庫文檔在線預覽系統(tǒng)設計與實現(xiàn)
網(wǎng)頁文檔在線瀏覽(仿百度文庫設計)
實現(xiàn)一個具有百度文庫文檔轉換功能的工具類
轉換office>>>>pdf>>>>>swf展示(百度文檔)
使用OpenOffice.org將各類文檔轉為PDF
更多類似文章 >>
生活服務
分享 收藏 導長圖 關注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服