Word/Excel/PDF文件转换成HTML整理

来源:互联网 发布:天庭淘宝城 编辑:程序博客网 时间:2024/05/18 16:18

项目开发过程中,需求涉及到了各种文档转换为HTML或者网页易显示格式,现在将实现方式整理如下: 
一、使用Jacob转换Word,Excel为HTML 

“JACOB一个Java-COM中间件.通过这个组件你可以在Java应用程序中调用COM组件和Win32 libraries。” 

首先下载Jacob包,JDK1.5以上需要使用Jacob1.9版本(JDK1.6尚未测试),与先前的Jacob1.7差别不大 

1、将压缩包解压后,Jacob.jar添加到Libraries中; 

2、将Jacob.dll放至“WINDOWS/SYSTEM32”下面。 

需要注意的是: 
【使用IDE启动Web服务器时,系统读取不到Jacob.dll,例如用MyEclipse启动Tomcat,就需要将dll文件copy到MyEclipse安装目录的“jre/bin”下面。 
一般系统没有加载到Jacob.dll文件时,报错信息为:“java.lang.UnsatisfiedLinkError: no jacob in java.library.path”】 

新建类: 

Java代码 

Java代码
  1. public class JacobUtil     
  2. {     
  3.     public static final int WORD_HTML = 8;     
  4.     
  5.     public static final int WORD_TXT = 7;     
  6.     
  7.     public static final int EXCEL_HTML = 44;     
  8.     
  9.     /**   
  10.      * WORD转HTML   
  11.      * @param docfile WORD文件全路径   
  12.      * @param htmlfile 转换后HTML存放路径   
  13.      */    
  14.     public static void wordToHtml(String docfile, String htmlfile)     
  15.     {     
  16.         ActiveXComponent app = new ActiveXComponent("Word.Application"); // 启动word     
  17.         try    
  18.         {     
  19.             app.setProperty("Visible"new Variant(false));     
  20.             Dispatch docs = app.getProperty("Documents").toDispatch();     
  21.             Dispatch doc = Dispatch.invoke(     
  22.                     docs,     
  23.                     "Open",     
  24.                     Dispatch.Method,     
  25.                     new Object[] { docfile, new Variant(false),     
  26.                             new Variant(true) }, new int[1]).toDispatch();     
  27.             Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] {     
  28.                     htmlfile, new Variant(WORD_HTML) }, new int[1]);     
  29.             Variant f = new Variant(false);     
  30.             Dispatch.call(doc, "Close", f);     
  31.         }     
  32.         catch (Exception e)     
  33.         {     
  34.             e.printStackTrace();     
  35.         }     
  36.         finally    
  37.         {     
  38.             app.invoke("Quit"new Variant[] {});     
  39.         }     
  40.     }     
  41.     
  42.     /**   
  43.      * EXCEL转HTML   
  44.      * @param xlsfile EXCEL文件全路径   
  45.      * @param htmlfile 转换后HTML存放路径   
  46.      */    
  47.     public static void excelToHtml(String xlsfile, String htmlfile)     
  48.     {     
  49.         ActiveXComponent app = new ActiveXComponent("Excel.Application"); // 启动word     
  50.         try    
  51.         {     
  52.             app.setProperty("Visible"new Variant(false));     
  53.             Dispatch excels = app.getProperty("Workbooks").toDispatch();     
  54.             Dispatch excel = Dispatch.invoke(     
  55.                     excels,     
  56.                     "Open",     
  57.                     Dispatch.Method,     
  58.                     new Object[] { xlsfile, new Variant(false),     
  59.                             new Variant(true) }, new int[1]).toDispatch();     
  60.             Dispatch.invoke(excel, "SaveAs", Dispatch.Method, new Object[] {     
  61.                     htmlfile, new Variant(EXCEL_HTML) }, new int[1]);     
  62.             Variant f = new Variant(false);     
  63.             Dispatch.call(excel, "Close", f);     
  64.         }     
  65.         catch (Exception e)     
  66.         {     
  67.             e.printStackTrace();     
  68.         }     
  69.         finally    
  70.         {     
  71.             app.invoke("Quit"new Variant[] {});     
  72.         }     
  73.     }     
  74.     
  75. }    
  76.   
  77. public class JacobUtil  
  78. {  
  79.     public static final int WORD_HTML = 8;  
  80.   
  81.     public static final int WORD_TXT = 7;  
  82.   
  83.     public static final int EXCEL_HTML = 44;  
  84.   
  85.     /** 
  86.      * WORD转HTML 
  87.      * @param docfile WORD文件全路径 
  88.      * @param htmlfile 转换后HTML存放路径 
  89.      */  
  90.     public static void wordToHtml(String docfile, String htmlfile)  
  91.     {  
  92.         ActiveXComponent app = new ActiveXComponent("Word.Application"); // 启动word  
  93.         try  
  94.         {  
  95.             app.setProperty("Visible"new Variant(false));  
  96.             Dispatch docs = app.getProperty("Documents").toDispatch();  
  97.             Dispatch doc = Dispatch.invoke(  
  98.                     docs,  
  99.                     "Open",  
  100.                     Dispatch.Method,  
  101.                     new Object[] { docfile, new Variant(false),  
  102.                             new Variant(true) }, new int[1]).toDispatch();  
  103.             Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] {  
  104.                     htmlfile, new Variant(WORD_HTML) }, new int[1]);  
  105.             Variant f = new Variant(false);  
  106.             Dispatch.call(doc, "Close", f);  
  107.         }  
  108.         catch (Exception e)  
  109.         {  
  110.             e.printStackTrace();  
  111.         }  
  112.         finally  
  113.         {  
  114.             app.invoke("Quit"new Variant[] {});  
  115.         }  
  116.     }  
  117.   
  118.     /** 
  119.      * EXCEL转HTML 
  120.      * @param xlsfile EXCEL文件全路径 
  121.      * @param htmlfile 转换后HTML存放路径 
  122.      */  
  123.     public static void excelToHtml(String xlsfile, String htmlfile)  
  124.     {  
  125.         ActiveXComponent app = new ActiveXComponent("Excel.Application"); // 启动word  
  126.         try  
  127.         {  
  128.             app.setProperty("Visible"new Variant(false));  
  129.             Dispatch excels = app.getProperty("Workbooks").toDispatch();  
  130.             Dispatch excel = Dispatch.invoke(  
  131.                     excels,  
  132.                     "Open",  
  133.                     Dispatch.Method,  
  134.                     new Object[] { xlsfile, new Variant(false),  
  135.                             new Variant(true) }, new int[1]).toDispatch();  
  136.             Dispatch.invoke(excel, "SaveAs", Dispatch.Method, new Object[] {  
  137.                     htmlfile, new Variant(EXCEL_HTML) }, new int[1]);  
  138.             Variant f = new Variant(false);  
  139.             Dispatch.call(excel, "Close", f);  
  140.         }  
  141.         catch (Exception e)  
  142.         {  
  143.             e.printStackTrace();  
  144.         }  
  145.         finally  
  146.         {  
  147.             app.invoke("Quit"new Variant[] {});  
  148.         }  
  149.     }  
  150.   
  151. }  

当时我在找转换控件时,发现网易也转载了一偏关于Jacob使用帮助,但其中出现了比较严重的错误:String htmlfile = "C://AA"; 

只指定到了文件夹一级,正确写法是String htmlfile = "C://AA//xxx.html"; 



到此WORD/EXCEL转换HTML就已经差不多了,相信大家应该很清楚了:) 



二、使用XPDF将PDF转换为HTML 



1、下载xpdf最新版本,地址:http://www.foolabs.com/xpdf/download.html 

我下载的是xpdf-3.02pl2-win32.zip 



2、下载中文支持包 

我下载的是xpdf-chinese-simplified.tar.gz 



3、下载pdftohtml支持包 

地址:http://sourceforge.net/projects/pdftohtml/ 

我下载的是:pdftohtml-0.39-win32.tar.gz 



4、解压调试 

1) 先将xpdf-3.02pl2-win32.zip解压,解压后的内容可根据需要进行删减,如果只需要转换为txt格式,其他的exe文件可以删除,只保留pdftotext.exe,以此类推; 

2) 然后将xpdf-chinese-simplified.tar.gz解压到刚才xpdf-3.02pl2-win32.zip的解压目录; 

3) 将pdftohtml-0.39-win32.tar.gz解压,pdftohtml.exe解压到xpdf-3.02pl2-win32.zip的解压目录; 

4) 目录结构: 

+---[X:/xpdf] 

           |-------各种转换用到的exe文件 

           | 

           |-------xpdfrc 

           | 

           +------[X:/xpdf/xpdf-chinese-simplified] 

                                      | 

                                      | 

                                      +-------很多转换时需要用到的字符文件 



xpdfrc:此文件是用来声明转换字符集对应路径的文件 



5) 修改xpdfrc文件(文件原名为sample-xpdfrc) 

修改文件内容为: 



Txt代码 
Java代码
  1. #----- begin Chinese Simplified support package     
  2. cidToUnicode    Adobe-GB1       xpdf-chinese-simplified/Adobe-GB1.cidToUnicode     
  3. unicodeMap      ISO-2022-CN     xpdf-chinese-simplified/ISO-2022-CN.unicodeMap     
  4. unicodeMap      EUC-CN          xpdf-chinese-simplified/EUC-CN.unicodeMap     
  5. unicodeMap  GBK    xpdf-chinese-simplified/GBK.unicodeMap     
  6. cMapDir         Adobe-GB1       xpdf-chinese-simplified/CMap     
  7. toUnicodeDir                    xpdf-chinese-simplified/CMap     
  8. fontDir  C:/WINDOWS/Fonts     
  9. displayCIDFontTT Adobe-GB1 C:/WINDOWS/Fonts/simhei.ttf     
  10. #----- end Chinese Simplified support package    
  11.   
  12. #----- begin Chinese Simplified support package  
  13. cidToUnicode    Adobe-GB1       xpdf-chinese-simplified/Adobe-GB1.cidToUnicode  
  14. unicodeMap      ISO-2022-CN     xpdf-chinese-simplified/ISO-2022-CN.unicodeMap  
  15. unicodeMap      EUC-CN          xpdf-chinese-simplified/EUC-CN.unicodeMap  
  16. unicodeMap  GBK    xpdf-chinese-simplified/GBK.unicodeMap  
  17. cMapDir         Adobe-GB1       xpdf-chinese-simplified/CMap  
  18. toUnicodeDir                    xpdf-chinese-simplified/CMap  
  19. fontDir  C:/WINDOWS/Fonts  
  20. displayCIDFontTT Adobe-GB1 C:/WINDOWS/Fonts/simhei.ttf  
  21. #----- end Chinese Simplified support package   

6) 创建bat文件pdftohtml.bat(放置的路径不能包含空格) 

内容为: 



Txt代码 
Java代码
  1. @echo off     
  2. set folderPath=%1    
  3. set filePath=%2    
  4. cd /d %folderPath%     
  5. pdftohtml -enc GBK %filePath%     
  6. exit    
  7.   
  8. @echo off  
  9. set folderPath=%1  
  10. set filePath=%2  
  11. cd /d %folderPath%  
  12. pdftohtml -enc GBK %filePath%  
  13. exit   

7) 创建类 



Java代码 
Java代码
  1. public class ConvertPdf     
  2. {     
  3.     private static String INPUT_PATH;     
  4.     private static String PROJECT_PATH;     
  5.          
  6.     public static void convertToHtml(String file, String project)     
  7.     {     
  8.         INPUT_PATH = file;     
  9.         PROJECT_PATH = project;     
  10.         if(checkContentType()==0)     
  11.         {     
  12.             toHtml();     
  13.         }     
  14.     }     
  15.          
  16.     private static int checkContentType()     
  17.     {     
  18.         String type = INPUT_PATH.substring(INPUT_PATH.lastIndexOf(".") + 1, INPUT_PATH.length())     
  19.                 .toLowerCase();     
  20.         if (type.equals("pdf"))     
  21.             return 0;     
  22.         else    
  23.             return 9;     
  24.     }     
  25.          
  26.     private static void toHtml()     
  27.     {     
  28.         if(new File(INPUT_PATH).isFile())     
  29.         {     
  30.             try    
  31.             {     
  32.                 String cmd = "cmd /c start X://pdftohtml.bat /"" + PROJECT_PATH + "/" /"" + INPUT_PATH + "/"";     
  33.                 Runtime.getRuntime().exec(cmd);     
  34.             }     
  35.             catch (IOException e)     
  36.             {     
  37.                 e.printStackTrace();     
  38.             }     
  39.         }     
  40.     }     
  41.          
  42. }    
  43.   
  44. public class ConvertPdf  
  45. {  
  46.     private static String INPUT_PATH;  
  47.     private static String PROJECT_PATH;  
  48.       
  49.     public static void convertToHtml(String file, String project)  
  50.     {  
  51.         INPUT_PATH = file;  
  52.         PROJECT_PATH = project;  
  53.         if(checkContentType()==0)  
  54.         {  
  55.             toHtml();  
  56.         }  
  57.     }  
  58.       
  59.     private static int checkContentType()  
  60.     {  
  61.         String type = INPUT_PATH.substring(INPUT_PATH.lastIndexOf(".") + 1, INPUT_PATH.length())  
  62.                 .toLowerCase();  
  63.         if (type.equals("pdf"))  
  64.             return 0;  
  65.         else  
  66.             return 9;  
  67.     }  
  68.       
  69.     private static void toHtml()  
  70.     {  
  71.         if(new File(INPUT_PATH).isFile())  
  72.         {  
  73.             try  
  74.             {  
  75.                 String cmd = "cmd /c start X://pdftohtml.bat /"" + PROJECT_PATH + "/" /"" + INPUT_PATH + "/"";  
  76.                 Runtime.getRuntime().exec(cmd);  
  77.             }  
  78.             catch (IOException e)  
  79.             {  
  80.                 e.printStackTrace();  
  81.             }  
  82.         }  
  83.     }  
  84.       
  85. }String cmd = "....";  
此处代码是调用创建的bat文件进行转换 



8) 测试转换 





Java代码 
Java代码
  1. public static void main(String[] args)     
  2. {     
  3.     ConvertPdf.convertToHtml("C://test.pdf""X://xpdf");     
  4. }    
  5.   
  6. public static void main(String[] args)  
  7. {  
  8.     ConvertPdf.convertToHtml("C://test.pdf""X://xpdf");  
  9. }   


以上就是整理的内容,后续还会添加视频转换为FLV格式,如果需要的话:) 

说的不是很详细,碰到问题的朋友可以自己努力解决一下:) 

原创粉丝点击