使用Jacob实现Word转换Html

2023-11-05 20:10

本文主要是介绍使用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();}}
}
       单例方式将只有一个ActiveXComponet,那么WINWORD.exe进程也只会有一个,不会重复创建关闭进程,而是一直使用这个进程。转换30次,只需14秒。

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的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/352210

相关文章

springboot集成easypoi导出word换行处理过程

《springboot集成easypoi导出word换行处理过程》SpringBoot集成Easypoi导出Word时,换行符n失效显示为空格,解决方法包括生成段落或替换模板中n为回车,同时需确... 目录项目场景问题描述解决方案第一种:生成段落的方式第二种:替换模板的情况,换行符替换成回车总结项目场景s

SpringBoot集成redisson实现延时队列教程

《SpringBoot集成redisson实现延时队列教程》文章介绍了使用Redisson实现延迟队列的完整步骤,包括依赖导入、Redis配置、工具类封装、业务枚举定义、执行器实现、Bean创建、消费... 目录1、先给项目导入Redisson依赖2、配置redis3、创建 RedissonConfig 配

Python的Darts库实现时间序列预测

《Python的Darts库实现时间序列预测》Darts一个集统计、机器学习与深度学习模型于一体的Python时间序列预测库,本文主要介绍了Python的Darts库实现时间序列预测,感兴趣的可以了解... 目录目录一、什么是 Darts?二、安装与基本配置安装 Darts导入基础模块三、时间序列数据结构与

Python使用FastAPI实现大文件分片上传与断点续传功能

《Python使用FastAPI实现大文件分片上传与断点续传功能》大文件直传常遇到超时、网络抖动失败、失败后只能重传的问题,分片上传+断点续传可以把大文件拆成若干小块逐个上传,并在中断后从已完成分片继... 目录一、接口设计二、服务端实现(FastAPI)2.1 运行环境2.2 目录结构建议2.3 serv

C#实现千万数据秒级导入的代码

《C#实现千万数据秒级导入的代码》在实际开发中excel导入很常见,现代社会中很容易遇到大数据处理业务,所以本文我就给大家分享一下千万数据秒级导入怎么实现,文中有详细的代码示例供大家参考,需要的朋友可... 目录前言一、数据存储二、处理逻辑优化前代码处理逻辑优化后的代码总结前言在实际开发中excel导入很

Spring Security简介、使用与最佳实践

《SpringSecurity简介、使用与最佳实践》SpringSecurity是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架,本文给大家介绍SpringSec... 目录一、如何理解 Spring Security?—— 核心思想二、如何在 Java 项目中使用?——

SpringBoot+RustFS 实现文件切片极速上传的实例代码

《SpringBoot+RustFS实现文件切片极速上传的实例代码》本文介绍利用SpringBoot和RustFS构建高性能文件切片上传系统,实现大文件秒传、断点续传和分片上传等功能,具有一定的参考... 目录一、为什么选择 RustFS + SpringBoot?二、环境准备与部署2.1 安装 RustF

Nginx部署HTTP/3的实现步骤

《Nginx部署HTTP/3的实现步骤》本文介绍了在Nginx中部署HTTP/3的详细步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学... 目录前提条件第一步:安装必要的依赖库第二步:获取并构建 BoringSSL第三步:获取 Nginx

springboot中使用okhttp3的小结

《springboot中使用okhttp3的小结》OkHttp3是一个JavaHTTP客户端,可以处理各种请求类型,比如GET、POST、PUT等,并且支持高效的HTTP连接池、请求和响应缓存、以及异... 在 Spring Boot 项目中使用 OkHttp3 进行 HTTP 请求是一个高效且流行的方式。

MyBatis Plus实现时间字段自动填充的完整方案

《MyBatisPlus实现时间字段自动填充的完整方案》在日常开发中,我们经常需要记录数据的创建时间和更新时间,传统的做法是在每次插入或更新操作时手动设置这些时间字段,这种方式不仅繁琐,还容易遗漏,... 目录前言解决目标技术栈实现步骤1. 实体类注解配置2. 创建元数据处理器3. 服务层代码优化填充机制详