本文主要是介绍使用Jacob实现Word转换Html,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
前言
源于一个项目的需求,用户上传Word文件后要能及时在网页上查看文件内容,类似于QQ邮箱的附件查看,QQ邮箱使用的是永中的产品工具。自己做当然是首选不要钱自己写代码就能搞定的。网上搜索后找到了Jacob,下面记录一下使用过程和自己使用中的一些心得。
环境
在项目中引入jacob.jar。复制jacob-1.16.1-x86.dll到jdk\bin目录,放置dll的位置网上有两种说法,一种是jdk\bin另一种是system32。其实只要放在path目录下即可,java中查看path目录的方式:System.getProperty("java.library.path")。另外切勿修改dll的文件名称,因为jacob.jar中已经写好了dll的名称,改了dll的名称后调用时就找不到了。下载的Jacob里面一般会包含x86和x64两个dll,使用哪一个是与jdk的版本相关的,与操作系统版本无关。
代码
常规方式
//word文件路径及名称
String docPath = "D:\\download\\word\\1.docx";
//html文件路径及名称
String fileName = "D:\\download\\html\\1.html";
//创建Word对象,启动WINWORD.exe进程
ActiveXComponent app = new ActiveXComponent("Word.Application");
//设置用后台隐藏方式打开
app.setProperty("Visible", new Variant(false));
//获取操作word的document调用
Dispatch documents = app.getProperty("Documents").toDispatch();
//调用打开命令,同时传入word路径
Dispatch doc = Dispatch.call(documents, "Open", docPath).toDispatch();
//调用另外为命令,同时传入html的路径
Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] { fileName, new Variant(8) }, new int[1]);
//关闭document对象
Dispatch.call(doc, "Close", new Variant(0));
//关闭WINWORD.exe进程
Dispatch.call(app, "Quit");
//清空对象
doc = null;
app = null;
如果word文件包含图片,那么会生成与html同名的1.files文件夹。这与在IE浏览器中使用右键另存为网页一样。
以上方式在每次调用时系统会创建WINWORD.exe进程,然后再关闭。要知道系统创建进程是非常耗资源的,时间和CPU。我做了一个简单的测试,将1.docx文件复制了30份,循环调用30次,用时76秒。
long start = System.currentTimeMillis();
JacobUtil jacobUtil = new JacobUtil();
for (int i = 1; i <= 30; i++) {
<span style="white-space:pre"> </span>jacobUtil.wordConvert("D:\\download\\word\\"+i+".docx", "D:\\download\\html\\"+i+".html");
}
long end = System.currentTimeMillis();
System.out.println("用时:"+(end-start)/1000L+"秒");
单例方式
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;public class JacobUtil {private static ActiveXComponent app;/*** 单例模式*/public static ActiveXComponent getWordInstance(){if (app == null) {app = new ActiveXComponent("Word.Application");app.setProperty("Visible", new Variant(false));}return app;}/*** 转换word*/public static void wordConvertSingleton(String docPath, String fileName) {try {app = getWordInstance();Dispatch documents = app.getProperty("Documents").toDispatch();Dispatch doc = Dispatch.call(documents, "Open", docPath).toDispatch();Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] { fileName, new Variant(8) }, new int[1]);Dispatch.call(doc, "Close", new Variant(0));//没有调用Quit命令doc = null;} catch (Exception e) {e.printStackTrace();}}
}
long start = System.currentTimeMillis();
for (int i = 1; i <= 30; i++) {
<span style="white-space:pre"> </span>JacobUtil.wordConvertSingleton("D:\\download\\word\\"+i+".docx", "D:\\download\\html\\"+i+".html");
}
long end = System.currentTimeMillis();
System.out.println("单例用时:"+(end-start)/1000L+"秒");
多线程方式
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;public class ThreadConvert implements Runnable{private ActiveXComponent app;private String docPath;private String fileName;public ThreadConvert(ActiveXComponent app,String docPath, String fileName) {this.app = app;this.docPath = docPath;this.fileName = fileName;}public void run() {System.out.println("开始:"+System.currentTimeMillis());try {app.setProperty("Visible", new Variant(false));Dispatch documents = app.getProperty("Documents").toDispatch();Dispatch doc = Dispatch.call(documents, "Open", docPath).toDispatch();Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] { fileName, new Variant(8) }, new int[1]);Dispatch.call(doc, "Close", new Variant(0));doc = null;} catch (Exception e) {e.printStackTrace();}System.out.println("结束:"+System.currentTimeMillis());}
}
ActiveXComponent app1 = new ActiveXComponent("Word.Application");
for (int i = 1; i <= 30; i++) {Thread thread = new Thread(new ThreadConvert(app1,"D:\\download\\word\\"+i+".docx", "D:\\download\\html\\"+i+".html"));thread.start();
}
因为线程执行不好计时,使用的是找出最早的开始时间与最晚的结束时间相减是15秒,与单例方式相近。
多进程方式
ActiveXComponent app1 = new ActiveXComponent("Word.Application");
ActiveXComponent app2 = new ActiveXComponent("Word.Application");
ActiveXComponent app3 = new ActiveXComponent("Word.Application");
for (int i = 1; i <= 30; i++) {
<span style="white-space:pre"> </span>if (i<=10) {Thread thread = new Thread(new ThreadConvert(app1,"D:\\download\\word\\"+i+".docx", "D:\\download\\html\\"+i+".html"));thread.start();}else if(i <= 20){Thread thread = new Thread(new ThreadConvert(app2,"D:\\download\\word\\"+i+".docx", "D:\\download\\html\\"+i+".html"));thread.start();}else {Thread thread = new Thread(new ThreadConvert(app3,"D:\\download\\word\\"+i+".docx", "D:\\download\\html\\"+i+".html"));thread.start();}
}
使用三个ActiveXComponent,也就是三个WINWORD.exe进程,每个进程分配10个转换任务,同样用最早的开始时间与最晚的结束时间相减是9秒。
这篇关于使用Jacob实现Word转换Html的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!