您的位置:首页 > 编程语言 > Java开发

java把word格式文件转化成html显示

2015-05-16 19:30 357 查看
<pre name="code" class="html">public void wordToHtml(String xlsfile, String htmlfile) throws Exception {
ActiveXComponent app = new ActiveXComponent("Word.Application"); // 启动word应用程序
try {
File dir = new File(htmlfile+".files");
File htm = new File(htmlfile+".htm");
if(dir.exists()){
dir.delete();
}
if(htm.exists()){
htm.delete();
}
//设置word不可见
app.setProperty("Visible", new Variant(false));
//打开word文件
Dispatch doc = app.getProperty("Documents").toDispatch();
Dispatch doc1 = Dispatch.invoke(
doc,
"Open",
Dispatch.Method,
new Object[] { xlsfile, new Variant(false),
new Variant(true) }, new int[1]).toDispatch();
//new Variant(i) i参数控制word文档另存为目标文档的格式:i=7时存为txt格式,
//i=8时存为html格式
Dispatch.invoke(doc1, "SaveAs", Dispatch.Method, new Object[] {
htmlfile, new Variant(WORD_HTML) }, new int[1]);
Variant f = new Variant(false);
Dispatch.call(doc1, "Close", f);
} catch (Exception e) {
e.printStackTrace();
throw new Exception(e);
} finally {
app.invoke("Quit", new Variant[] {});
}

}



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息