配置方法如下
把jacob.dll放入 Java\jdk1.5.0_06\jre\bin目录下.
把jacob.jar放入 Java\jdk1.5.0_06\jre\lib\ext 目录下.
jacob.jar 就是我们要使用的包是和java 交互的东西 我们可以放进java的类库中然后引入项目
需要在环境变量中指明jacob.jar 的位置。
jacob.dll 是和com 交互的东西 我们需要把它放入windows\system32 中,
而且在path中要指明它的位置(path=C:\WINDOWS\system32\jacob.dll;)
web应用要用到tomcat,当初配置的时候还有个jacob.dll,网上说需要将它放进tomcat的bin目录下
import com.jacob.com.*;
import com.jacob.activeX.*;
import java.io.*;
import com.jacob.com.ComException;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
/**
* 将一个word转化成html
*
*/
/**
*文档转换函数222222222222222222
*
* @param docfile
* word文档的绝对路径加文件名(包含扩展名)
*@param htmlfile
* 转换后的html文件绝对路径和文件名(不含扩展名)
*/
public static final int WORD_HTML = 8;
public static final int WORD_TXT = 7;
public static final int EXCEL_HTML = 44;
/**
* WORD转HTML
*
* @param docfile
* WORD文件全路径
* @param htmlfile
* 转换后HTML存放路径
*/
public static void wordToHtml(String docfile, String htmlfile) {
ActiveXComponent app = new ActiveXComponent("Word.Application"); // 启动word
try {
app.setProperty("Visible", new Variant(false));
Dispatch docs = 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[] {});
}
}
public static void main(String[] strs) {
TestWordHtml.wordToHtml("C:\\Users\\joner\\Desktop\\1\\test.doc", "C:\\Users\\joner\\Desktop\\1\\test222.html");
}
/**
*将1夹下所有doc在2夹中转为html
*
*/
public static void change(String paths,String savepaths){
File d = new File(paths);
// 取得当前文件夹下所有文件和目录的列表
File lists[] = d.listFiles();
String pathss = new String("");
// 对当前目录下面所有文件进行检索
for(int i = 0; i < lists.length; i ++)
{
if(lists[i].isFile())
{
String filename = lists[i].getName();
String filetype = new String("");
// 取得文件类型
filetype = filename.substring(filename.lastIndexOf(".")+1);
System.out.println("文件类型"+filetype);
// 判断是否为doc文件
if(filetype.equals("doc")||filetype.equals("docx"))
{
System.out.println("当前正在转换......");
// 打印当前目录路径
System.out.println(paths);
// 打印doc文件名
System.out.println(filename.substring(0, filename.lastIndexOf(".")));
ActiveXComponent app = new ActiveXComponent("Word.Application");// 启动word
String docpath = paths + filename;
String htmlpath = savepaths + filename.substring(0,filename.lastIndexOf("."));
String inFile = docpath;
// 要转换的word文件
String tpFile = htmlpath;
System.out.println("文件"+tpFile);
// HTML文件
boolean flag = false;
try
{
app.setProperty("Visible", new Variant(false));
// 设置word不可见
Object docs = app.getProperty("Documents").toDispatch();
Object doc = Dispatch.invoke((Dispatch) docs,"Open", Dispatch.Method, new Object[]{inFile,new Variant(false), new Variant(true)}, new int[1]).toDispatch();
// 打开word文件
Dispatch.invoke((Dispatch) doc,"SaveAs", Dispatch.Method, new Object[]{tpFile,new Variant(8)}, new int[1]);
// 作为html格式保存到临时文件
Variant f = new Variant(false);
Dispatch.call((Dispatch) doc, "Close", f);
flag = true;
}
catch (Exception e)
{
e.printStackTrace();
}
finally {
app.invoke("Quit", new Variant[] {});
}
System.out.println("转化完毕!");
}
}
else{
pathss = paths;
// 进入下一级目录
pathss = pathss + lists[i].getName() + "\\";
// 递归遍历所有目录
change(pathss, savepaths);
}
}
}
// ------------------------------------------------------------------------------
// 方法原型: main(String[] args)
// 功能描述: main文件
// 输入参数: 无
// 输出参数: 无
// 返 回 值: 无
// 其它说明: 无
// ------------------------------------------------------------------------------
public static void main(String[] args) {
String paths = new String(
"C:\\Users\\joner\\Desktop\\1\\");
String savepaths = new String(
"C:\\Users\\joner\\Desktop\\2\\");
change(paths, savepaths);
}