[jacob]---------------->java 实现 word 转换为html jacob

最近要利用java来实现word转换为html。
网上google后发现一种可行的方法:

package com.lgf.jacob.word;

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

public class WordTOHTML {

public static void main(String[] args) {
String wordExistPath = "E:\\xxx.doc";//word文件路径
String htmlSaveASPath = "E:\\ooo.htm";//html文件路径
wordToHtml(wordExistPath, htmlSaveASPath);
}

/**
* 工具类。实现word转html
* @param wordExistPath 要转换的word路径
* @param htmlSaveASPath 生成后html保存的路径
* @return
*/
private static Boolean wordToHtml(String wordExistPath, String htmlSaveASPath)
{
String wordPath = wordExistPath;
String htmlPath = htmlSaveASPath;
ComThread.InitSTA();
ActiveXComponent app = new ActiveXComponent("Word.Application");//打开word

// word 是否可见
boolean flag = false;
try
{
// 设置word不可见
app.setProperty("Visible", new Variant(flag));
Dispatch document = app.getProperty("Documents").toDispatch();

Dispatch doc2 = Dispatch.invoke(document,"Open", Dispatch.Method, new Object[]{wordPath,new Variant(false), new Variant(true)}, new int[1]).toDispatch();//打开word文件

/*
* new Variant(10)筛选过的网页
* new Variant(9) 单个文件网页
* new Variant(8) 另存为网页
* new Variant(7) 另存为txt格式
* new Variant(6) 另存为rtf格式
* new Variant(0) 另存为doc格式
*/
Dispatch.invoke(doc2,"SaveAs", Dispatch.Method, new Object[]{htmlPath,new Variant(8)}, new int[1]);

Dispatch.call(doc2,"Close",new Variant(false)); //关闭
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
app.invoke("Quit",new Variant[]{});
ComThread.Release();
}
return true;
}


}


下面带上jar包和 上面的源码,需要的可以下载
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值