09年时就做过java在Windows平台下面把word转mht文件,一直懒得写实现过程,今天写下实现的过程:
找到jacob插件(版本:jacob-1.14.3)。部署过程如下:
1 把jacob.jar加入到java 的class path下面(把jacob.jar,jacob-1.14.3-x86.dll放到工程的lib下面)
2 把jacob-1.14.3-x86.dll和msvcr80.dll放到C:/WINDOWS/system32下面(假设Windows系统装在c盘下面),
3 把vcredist_x86.exe运行一下
4 最重要的一点,电脑上要装office2003,不然还是无法正常运行。
测试使用的jdk是1.5的
在代码中示例如下:
import java.io.File;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
/**
* 将word文档转换为其他类型的文档
*/
public class WordConvertTool {
public static final int WDFORMAT_DOCUMENT = 0;
public static final int WDFORMAT_TEMPLATE = 1;
public static final int WDFORMAT_TEXT = 2;
public static final int WDFORMAT_TEXTLINEBREAKS = 3;
public static final int WDFORMAT_DOSTEXT = 4;
public static final int WDFORMAT_DOSTEXTLINEBREAKS = 5;
public static final int WDFORMAT_RTF = 6;
public static final int WDFORMAT_UNICODETEXT = 7;
public static final int WDFORMAT_HTML = 8;
public static final int WDFORMAT_WEBARCHIVE = 9;
public static final int WDFORMAT_FILTEREDHTML = 10;
public static final int WDFORMAT_XML = 11;
/*
* 转换单个文件,默认转换mht
*srcFilePath 表示要转换的word文件
*destFilePath表示转换后的mht文件
*/
public static boolean convertFile(String srcFilePath, String destFilePath) {
return convertFile(srcFilePath, destFilePath, WDFORMAT_HTML);
}
/*
* 转换整个文件夹,默认转换mht
*/
public static boolean convertDir(String srcDirPath, String destDirPath) {
return convertDir(srcDirPath, destDirPath, WDFORMAT_HTML);
}
public static boolean convertDir(String srcDirPath, String destDirPath,
int type) {
boolean success = false;
File srcDir = new File(srcDirPath);
// 取得当前文件夹下所有文件和目录的列表
File lists[] = srcDir.listFiles();
// 对当前目录下面所有文件进行检索
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.length() - 3), filename
.length());
// 判断是否为doc文件
if (filetype.equals("doc")) {
ActiveXComponent app = new ActiveXComponent(
"Word.Application");
// 启动word
String docpath = srcDirPath + filename;
String destpath = srcDirPath
+ filename.substring(0, (filename.length() - 4));
String srcFile = docpath;
// 要转换的word文件
String destFile = destpath;
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[] { srcFile },
new int[1]).toDispatch();
// 打开word文件
Dispatch.invoke((Dispatch) doc, "SaveAs",
Dispatch.Method,
new Object[] { destFile, type }, new int[1]);
// 保存到临时文件
// Dispatch.invoke((Dispatch) doc, "Close",
// Dispatch.Method, new Object[] { new Variant(
// false) }, new int[1]);
success = true;
} catch (Exception e) {
e.printStackTrace();
} finally {
app.invoke("Quit", new Variant[] {});
}
}
} else {
convertDir(srcDirPath + lists[i].getName() + "//", destDirPath,
type);
}
}
return success;
}
public static boolean convertFile(String srcFilePath, String destFilePath,
int type) {
boolean success = false;
File srcFile = new File(srcFilePath);
File destFile = new File(destFilePath);
if (srcFile.isFile() && destFile.getParentFile() != null) {
String filetype = srcFile.getName().substring(
(srcFile.getName().length() - 3));
if (filetype.equals("doc")) {
ActiveXComponent app = new ActiveXComponent("Word.Application");
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[] { srcFilePath },
new int[1]).toDispatch();
// 打开word文件
Dispatch.invoke((Dispatch) doc, "SaveAs", Dispatch.Method,
new Object[] { destFilePath, type }, new int[1]);
// 保存到临时文件
// Dispatch.invoke((Dispatch) doc, "Close", Dispatch.Method,
// new Object[] { new Variant(false) }, new int[1]);
success = true;
} catch (Exception e) {
e.printStackTrace();
} finally {
app.invoke("Quit", new Variant[] {});
}
}
}
return success;
}
public static void main(String[] args) {
String srcDirPath = new String("d://test//");
String destDirPath = new String("d://test//");
System.out.println(convertDir(srcDirPath, destDirPath));
}
}
想把excel,word等转换为mht,html更详细的可参考:
http://www.iteye.com/topic/333676
内容包括:一、使用Jacob转换Word,Excel为HTML
二、使用XPDF将PDF转换为HTML