java使用jacob将word,excel,ppt转成html

本文介绍如何使用Java和COM组件将Office文档(包括Word、Excel和PPT)转换为HTML或PDF格式。提供了完整的代码示例,并解释了所需jar包和DLL文件的配置方法。

用到的jar包及插件:

        jacob-1.18( jacob.jar)

       jacob-1.18( jacob-1.18-x64.dll)(32位)

       jacob-1.18( jacob-1.18-x86.dll)(64位)

1.dll文件根据电脑系统版本和jdk版本(32或者64位)来选择。

2.使用main方法时将dll文件放到C盘Windows\System32目录下,项目调用此方法时将dll文件放到jdk的jre\bin目录下。

3.需要注意一点如果运行还是报错的话,把x64.dll(32位)放到Windows\System32目录下,把x86.dll(32位)放到

Windows\SysWOW64目录下。这样肯定可行。

4.然后把jar包导入到项目工程中,注意对应版本号。

注:此方法来源与网上,然后我给整合了一下。网上的ppt转html我怎么试都不成功,转换失败全是乱码,后来我只能

把ppt转成PDF,然后再将PDF转成html。虽然麻烦但是也能实现最终的目的,谁有成功的方法欢迎分享,谢谢。

下面直接贴java代码:

public class OfficeToXML {

         private final static OfficeToXML oOfficeToXML = new OfficeToXML();  
	
	public static OfficeToXML getInstance() {  
        return oOfficeToXML;  
    } 
	
	public OfficeToXML() {  
    } 

 //Word转html方法
   public boolean WordtoHtml(String s, String s1) {  
        ComThread.InitSTA();  
        ActiveXComponent activexcomponent = new ActiveXComponent(  
                "Word.Application");  
        String s2 = s;  
        String s3 = s1;  
        boolean flag = false;  
        try {  
            activexcomponent.setProperty("Visible", new Variant(false));  
            activexcomponent.setProperty("AutomationSecurity", new Variant(3)); // 禁用宏  
            Dispatch dispatch = activexcomponent.getProperty("Documents").toDispatch();  
            Dispatch dispatch1 = Dispatch.invoke(dispatch, "Open", 1,  
                    new Object[] { s2, new Variant(false), new Variant(true) },  
                    new int[1]).toDispatch();  
            Dispatch.invoke(dispatch1, "SaveAs", 1, new Object[] { s3,  
                    new Variant(8) }, new int[1]);  
            Variant variant = new Variant(false);  
            Dispatch.call(dispatch1, "Close", variant);  
            flag = true;  
        } catch (Exception exception) {  
            exception.printStackTrace();  
        } finally {  
            activexcomponent.invoke("Quit", new Variant[0]);  
            ComThread.Release();  
            ComThread.quitMainSTA();  
        }  
        return flag;  
    }  


    //excel转html方法
    public boolean ExceltoHtml(String s, String s1) {  
         ComThread.InitSTA();  
         ActiveXComponent activexcomponent = new  
         ActiveXComponent("Excel.Application");  
         String s2 = s;  
         String s3 = s1;  
         boolean flag = false;  
         try  
         {  
         activexcomponent.setProperty("Visible", new Variant(false));  
         activexcomponent.setProperty("AutomationSecurity", new Variant(3)); // 禁用宏  
         Dispatch dispatch =  
         activexcomponent.getProperty("Workbooks").toDispatch();  
         Dispatch dispatch1 = Dispatch.invoke(dispatch, "Open", 1, new  
         Object[] {  
         s2, new Variant(false), new Variant(true)  
         }, new int[1]).toDispatch();  
         Dispatch.call(dispatch1, "SaveAs", s3, new Variant(44));  
         Variant variant = new Variant(false);  
         Dispatch.call(dispatch1, "Close", variant);  
         flag = true;  
         }  
         catch(Exception exception)  
         {  
         System.out.println("|||" + exception.toString());  
         }  
         finally  
         {  
         activexcomponent.invoke("Quit", new Variant[0]);  
         ComThread.Release();  
         ComThread.quitMainSTA();  
         }  
         return flag;  
    }
   

   // PPT转PDF ,然后PDF再转html   
   /*转PDF格式值*/   
    private static final int ppFormatPDF = 32;   
    public boolean PPT2pdf(String filename, String pdfFilename) {
    	 ComThread.InitSTA(); 
    	 long start = System.currentTimeMillis();  
         ActiveXComponent app = null;  
         Dispatch ppt = null;  
    	 boolean flag = false;  
    	 try {
    		 app = new ActiveXComponent("PowerPoint.Application");// 创建一个PPT对象  
              app.setProperty("AutomationSecurity", new Variant(3)); // 禁用宏  
             Dispatch ppts = app.getProperty("Presentations").toDispatch();// 获取文挡属性  
			 
             System.out.println("打开文档 >>> " + filename);  
             // 调用Documents对象中Open方法打开文档,并返回打开的文档对象Document  
             ppt = Dispatch.call(ppts, "Open", filename,   
                     true,// ReadOnly  
                     true,// Untitled指定文件是否有标题  
                     false// WithWindow指定文件是否可见  
                     ).toDispatch();  
               
             System.out.println("转换文档 [" + filename + "] >>> [" + pdfFilename + "]");  
             Dispatch.call(ppt, "SaveAs", pdfFilename, ppFormatPDF);  
   
             long end = System.currentTimeMillis();  
   
             System.out.println("用时:" + (end - start) + "ms.");  
             
             flag = true;  
		} catch (Exception e) {
			 e.printStackTrace();  
	         System.out.println("========Error:文档转换失败:" + e.getMessage());  
		}finally {  
            Dispatch.call(ppt, "Close");  
            System.out.println("关闭文档");  
            if (app != null)  
                app.invoke("Quit", new Variant[] {});  
        }  
        ComThread.Release();  
        ComThread.quitMainSTA(); 
    	 
    	 
    	 return flag;  
    }



     public static void main(String args[]) {  
        OfficeToXML otx = OfficeToXML.getInstance();  
    
         boolean flag1 =otx.PPT2pdf("E:\\office\\后台架构.pptx", "E:\\office\\后台架构.pdf");
        if(flag1){  
            System.out.println("PPT文件转换成PDF成功!");  
        }else{  
            System.out.println("PPT文件转换成PDF失败!");  
        }     
        boolean flag2 = otx.WordtoHtml("E:\\office\\产品需求文档.docx", "E:\\office\\产品需求文档.html");  
        if(flag2){  
            System.out.println("WORD文件转换成HTML成功!");  
        }else{  
            System.out.println("WORD文件转换成HTML失败!");  
        }
        boolean flag3 = otx.ExceltoHtml("E:\\office\\bug.xlsx", "E:\\office\\bug.html");  
        if(flag3){  
            System.out.println("EXCEL文件转换成HTML成功!");  
        }else{  
            System.out.println("EXCEL文件转换成HTML失败!");  
        } 

    }

}




      

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值