使用Jacob实现html和word的相互转换

0 篇文章 0 订阅
package zmx.jacob.test;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

public class JacobUtil {
	
	// 8 代表word保存成html
	public static final int WORD_HTML = 8; 
	
	// 1 代表html保存成word
	public static final int HTML_WORD = 1; 

	public static void main(String[] args) {
		String wordFile = "D:\\projects\\testWeb\\src\\zmx\\jacob\\test\\wordFile.doc";
		String htmlFile = "D:\\projects\\testWeb\\src\\zmx\\jacob\\test\\htmlFile.html";
	//	JacobUtil.wordToHtml(wordFile, htmlFile);
		
		
		JacobUtil.htmlToWord(htmlFile, wordFile);
	}
	
    /**  
     * WORD转HTML  
     * @param docfile WORD文件全路径  
     * @param htmlfile 转换后HTML存放路径 
     * notes:需要将jacob.dll拷贝到windows/system32或者项目所在jre\bin目录下面(比如我的Eclipse正在用的Jre路径是D:\Java\jdk1.7.0_17\jre\bin)。
     * @param html html静态页面路径
     * @param wordFile 要生成的word文档路径 
     */  
    public static void wordToHtml(String docfile, String htmlfile)   
    {   
    	// 启动word应用程序(Microsoft Office Word 2003)
        ActiveXComponent app = new ActiveXComponent("Word.Application");
        System.out.println("*****正在转换...*****");
        try  
        {	
        	// 设置word应用程序不可见  
            app.setProperty("Visible", new Variant(false));  
            // documents表示word程序的所有文档窗口,(word是多文档应用程序)
            Dispatch docs = app.getProperty("Documents").toDispatch();  
            // 打开要转换的word文件
            Dispatch doc = Dispatch.invoke(docs, "Open", Dispatch.Method, new Object[] { docfile, new Variant(false), new Variant(true) }, new int[1]).toDispatch();   
            // 作为html格式保存到临时文件
            Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] { htmlfile, new Variant(WORD_HTML) }, new int[1]);   
            // 关闭word文件
            Dispatch.call(doc, "Close", new Variant(false));   
        }   
        catch (Exception e)   
        {   
            e.printStackTrace();   
        }   
        finally  
        {   
        	//关闭word应用程序
            app.invoke("Quit", new Variant[] {});   
        } 
        System.out.println("*****转换完毕********");
    }
    
    
    /**

     * JACOB方式

     * notes:需要将jacob.dll拷贝到windows/system32或者项目所在jre\bin目录下面(比如我的Eclipse正在用的Jre路径是D:\Java\jdk1.7.0_17\jre\bin)。

     * @param html html静态页面路径

     * @param wordFile 要生成的word文档路径

     */

    public static void htmlToWord(String htmlFile, String wordFile) {    
    	
            ActiveXComponent app = new ActiveXComponent("Word.Application"); // 启动word
            System.out.println("*****正在转换...*****");
            try {

                app.setProperty("Visible", new Variant(false));

                Dispatch wordDoc = app.getProperty("Documents").toDispatch();

                wordDoc = Dispatch.invoke(wordDoc, "Add", Dispatch.Method, new Object[0], new int[1]).toDispatch();

                Dispatch.invoke(app.getProperty("Selection").toDispatch(), "InsertFile", Dispatch.Method, new Object[] { htmlFile, "", new Variant(false), new Variant(false), new Variant(false) }, new int[3]);

                Dispatch.invoke(wordDoc, "SaveAs", Dispatch.Method, new Object[] {wordFile, new Variant(HTML_WORD)}, new int[1]);

                Dispatch.call(wordDoc, "Close", new Variant(false));

            } catch (Exception e) {

                e.printStackTrace();

            } finally {

                app.invoke("Quit", new Variant[] {});

            }
            System.out.println("*****转换完毕********");
    }
}

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 11
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值