jacob word 转换为 html

1.确保本机已安装office word 并可以正常运行

2.首先下载Jacob包:将压缩包解压后,Jacob.jar添加到Libraries中;Jacob.dll放至“WINDOWS\SYSTEM32”下面。

public class ChangMain {
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		JacobUtil ju = new JacobUtil();
		String docfile = "D:\\题库2.doc";
		String htmlfile = "D:\\题库2.html";
		ju.wordToHtml(docfile, htmlfile);
		System.out.println("转换成功!");
	}
}
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;


public class JacobUtil {
	public static final int WORD_HTML = 8;
	public static final int WORD_TXT = 7;
	public static final int EXCEL_HTML = 44;
	
	//word 转 html
	public void wordToHtml(String docfile,String htmlfile){
		ComThread.InitSTA();
		ActiveXComponent app = new ActiveXComponent("Word.Application");
		try{
			//设置word不可见
			app.setProperty("Visible", new Variant(false));
			//获得documents对象
			Dispatch docs = (Dispatch)app.getProperty("Documents").toDispatch();
			//打开文件
			Dispatch doc = Dispatch.invoke(docs, "Open", Dispatch.Method, new Object[] { docfile, new Variant(false),new Variant(true) }, new int[1]).toDispatch();
			//保存新的文件
			Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] {htmlfile, new Variant(WORD_HTML) }, new int[1]);
			Variant f = new Variant(false);
			Dispatch.call(doc, "Close", f);


		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			app.invoke("Quit", new Variant[] {});
			ComThread.Release();
		}
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值