首先需要 jacob_18.zip
将其中的 jacob.dll 放置 jdk1.6.0\jre\bin (看其他文章是放 system32/下)
将其中的 jacob.jar 放置 jdk1.6.0\jre\lib\ext
public static void change(String paths, String savepaths)
{
File d = new File(paths);
//取得当前文件夹下所有文件和目录的列表
File lists[] = d.listFiles();
String pathss = new String("");
String tpFile = "";
String docpath = "";
//对当前目录下面所有文件进行检索
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, filename.length());
//判断是否为doc文件 处理Word文件
if(filetype.equals("doc")||filetype.equals("docx"))
{
System.out.println("当前正在转换......");
//打印当前目录路径
System.out.println(paths);
//打印doc文件名
System.out.println(filename.substring(0, filename.indexOf(".")));
ActiveXComponent app = new ActiveXComponent("Word.Application");//启动word
docpath = paths + filename;
String htmlpath = savepaths + filename.substring(0, filename.indexOf("."));
File fpath = new File(htmlpath);
if(!fpath.exists()){
fpath.mkdirs();
}
String inFile = docpath;
//要转换的word文件
tpFile = htmlpath+".html";
//HTML文件
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[]{tpFile,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[] {});
}
System.out.println("转化完毕!");
}//处理Excel文件
if(filetype.equals("xls"))
{
System.out.println("当前正在转换......");
//打印当前目录路径
System.out.println(paths);
//打印doc文件名
System.out.println(filename.substring(0, (filename.length() - 4)));
docpath = paths + filename;
File fpath = new File(savepaths);
if(!fpath.exists()){
fpath.mkdirs();
}
String htmlpath = savepaths + filename.substring(0, filename.indexOf("."));
//要转换的word文件
tpFile = htmlpath+".html";
ActiveXComponent app = new ActiveXComponent("Excel.Application");//启动word
String result = "N";
try
{
//设置excel不可见
app.setProperty("Visible", new Variant(false));
Dispatch excels = app.getProperty("Workbooks").toDispatch();
//打开excel文件
Dispatch excel = Dispatch.invoke(
excels,
"Open",
Dispatch.Method,
new Object[] { docpath, new Variant(false),
new Variant(true) }, new int[1]).toDispatch();
//作为html格式保存到临时文件
Dispatch.invoke(excel, "SaveAs", Dispatch.Method, new Object[] {
tpFile, new Variant(EXCEL_HTML) }, new int[1]);
app.setProperty("DisplayAlerts", new Variant(false));
Dispatch.call(excel, "Close", new Variant(false));
excel = null;
excels = null;
app.invoke("Quit", new Variant[] {});
ComThread.Release();//真正退出Excel進程
app = null;
result = "Y";
}
catch (Exception e)
{
result = "N";
e.printStackTrace();
}
finally
{
if(app!=null)
app.invoke("Quit", new Variant[] {});
}
}
}
else
{
pathss = paths;
//进入下一级目录
pathss = pathss + lists[i].getName() + "/";
//递归遍历所有目录
change(pathss, savepaths);
}
}
}