package com.esp.dochtml;
import java.io.File;
import com.jacob.com.*;
import com.jacob.activeX.*;
public class Word2Html{
private String inFile = " ";//要转换的word文件
private String otFile = " ";//目标文件
public Word2Html(){
}
public String setinFile(String filepath)
{
inFile = filepath;
return inFile;
}
public String setotFile(String filepath)
{
otFile = filepath;
return otFile;
}
public boolean convert()
{
ActiveXComponent app = new ActiveXComponent( "Word.Application ");//启动word
boolean flag = false;
try {
app.setProperty( "Visible ", new Variant(false));//设置word不可见
Object docs = app.getProperty( "Documents ").toDispatch();
Object doc = Dispatch.invoke(docs, "Open ", Dispatch.Method, new Object[]{inFile,new Variant(false), new Variant(true)}, new int[1]).toDispatch();//打开word文件
Dispatch.invoke(doc, "SaveAs ", Dispatch.Method, new Object[]{otFile,new Variant(8)}, new int[1]);//作为html格式保存到临时文件
Variant f = new Variant(false);
Dispatch.call(doc, "Close ", f);
flag = true;
} catch (Exception e) {
e.printStackTrace();
} finally {
app.invoke( "Quit ", new Variant[] {});
}
return flag;
}
}
我在jsp中这样写的:
<%@ page language= "java " import= "java.sql.* "%>
<%@ page contentType= "text/html; charset=gb2312 " %>
<%@ page import= "java.io.*,java.text.*,java.util.* " %>
<%@ page import= "com.esp.dochtml.* " %>
<%
Word2Html wordTohtml = new Word2Html();
wordTohtml.setinFile( "d://1111111111.doc ");
wordTohtml.setotFile( "d://2.html ");
%>
也是网络上找到的。
注意jacob.dll的路径要加到path里。
jacob实现word转html
最新推荐文章于 2021-11-30 10:21:44 发布