java转换docx为html_Java使用Jacob转换Word为HTML
从今天开始,我也要养成记录开发中遇到的问题和解决方法的好习惯!
最近开发一个Android项目,需要用到查看Word和Pdf文档的功能,由于Android没有直接显示Word和PDF文档的组件,只有一个Webview能查看html网页,所以决定将文档于服务器端转换为html,之后不论是在线预览还是下载到移动终端都可以直接查看了。
最近在网上查阅相关资料,找到利用Jacob来转换Word为html,除了占用CPU性能多一些,好像还不错(.doc和.docx都可以转换的!)。废话不多说,切入正题,这篇文章就先介绍转换Word为html的过程,Pdf还在研究当中,如果有结果我也会发出来!
"JACOB一个Java-COM中间件.通过这个组件你可以在Java应用程序中调用COM组件和Win32 libraries。"
Ps:Jacob只能用于windows系统,如果你的系统不是windows,建议使用Openoffice.org,这个是跨平台的,虽然我没用,但是应该不麻烦,就是需要先安装Openoffice这个软件,然后使用8100服务。至于Poi,说实话,我真不爱用,那个需要先解析word,然后自己覆写成html,工作量大不说,还得不偿失,因为很难保证转换的html内容的格式与原来word文档格式一致,并且.docx转换也很费劲。
2、将压缩包解压后,Jacob.jar添加到Libraries中(先复制到项目目录中,右键单击jar包选择Build Path—>Add to Build Path);
3、将Jacob.dll放至当前项目所用到的“jre\bin”下面(比如我的Eclipse正在用的Jre路径是D:\Java\jdk1.7.0_17\jre\bin)。
Ps:我就是按照上面的步骤配置的,一点问题没有,但是有些人可能还会报错,比如:java.lang.UnsatisfiedLinkError: no jacob in java.library.path,这是系统没有加载到jacob.dll,网上解决方法是将Jacob.dll放至“WINDOWS\SYSTEM32”下面(我没试过,因为我的直接没问题)。
Java代码:
public classJacobUtil {//8 代表word保存成html
public static final int WORD_HTML = 8;public static voidmain(String[] args) {
String docfile= "C:\\Users\\無名\\Desktop\\xxx.doc";
String htmlfile= "C:\\Users\\無名\\Desktop\\xxx.html";
JacobUtil.wordToHtml(docfile, htmlfile);
}/*** WORD转HTML
*@paramdocfile WORD文件全路径
*@paramhtmlfile 转换后HTML存放路径*/
public static voidwordToHtml(String docfile, String htmlfile) {//启动word应用程序(Microsoft Office Word 2003)
ActiveXComponent app = new ActiveXComponent("Word.Application");
System.out.println("*****正在转换...*****");try{//设置word应用程序不可见
app.setProperty("Visible", new Variant(false));//documents表示word程序的所有文档窗口,(word是多文档应用程序)
Dispatch docs = app.getProperty("Documents").toDispatch();//打开要转换的word文件
Dispatch doc =Dispatch.invoke(
docs,"Open",
Dispatch.Method,new Object[] { docfile, new Variant(false),new Variant(true) }, new int[1]).toDispatch();//作为html格式保存到临时文件
Dispatch.invoke(doc, "SaveAs", Dispatch.Method, newObject[] {
htmlfile,new Variant(WORD_HTML) }, new int[1]);//关闭word文件
Dispatch.call(doc, "Close", new Variant(false));
}catch(Exception e) {
e.printStackTrace();
}finally{//关闭word应用程序
app.invoke("Quit", newVariant[] {});
}
System.out.println("*****转换完毕********");
}
}
EXCEL转HTML代码:
packagetest;importjava.io.File;importcom.jacob.activeX.ActiveXComponent;importcom.jacob.com.Dispatch;importcom.jacob.com.Variant;public classWordToHtml {int WORD_HTML = 8;int WORD_TXT = 7;int EXCEL_HTML = 44;/*** WORD转HTML
*@paramdocfile WORD文件全路径
*@paramhtmlfile 转换后HTML存放路径*/
public voidwordToHtml(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, newObject[] {
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", newVariant[] {});
}
}/*** EXCEL转HTML
*@paramxlsfile EXCEL文件全路径
*@paramhtmlfile 转换后HTML存放路径*/
public voidexcelToHtml(String xlsfile, String htmlfile) {
ActiveXComponent app= new ActiveXComponent("Excel.Application"); //启动excel
try{
app.setProperty("Visible", new Variant(false));
Dispatch excels= app.getProperty("Workbooks").toDispatch();
Dispatch excel=Dispatch.invoke(
excels,"Open",
Dispatch.Method,new Object[] { xlsfile, new Variant(false),new Variant(true) }, new int[1]).toDispatch();
Dispatch.invoke(excel,"SaveAs", Dispatch.Method, newObject[] {
htmlfile,new Variant(EXCEL_HTML) }, new int[1]);
Variant f= new Variant(false);
Dispatch.call(excel,"Close", f);
System.out.println("wordtohtml转换成功");
}catch(Exception e) {
e.printStackTrace();
}finally{
app.invoke("Quit", newVariant[] {});
}
}/*** /删除指定文件夹
*@paramfolderPath 文件夹全路径
*@paramhtmlfile 转换后HTML存放路径*/
public voiddelFolder(String folderPath) {try{
delAllFile(folderPath);//删除完里面所有内容
String filePath =folderPath;
filePath=filePath.toString();
java.io.File myFilePath= newjava.io.File(filePath);
myFilePath.delete();//删除空文件夹
} catch(Exception e) {
e.printStackTrace();
}
}/*** /删除指定文件夹下所有文件
*@parampath 文件全路径*/
public booleandelAllFile(String path) {boolean flag = false;
File file= newFile(path);if (!file.exists()) {returnflag;
}if (!file.isDirectory()) {returnflag;
}
String[] tempList=file.list();
File temp= null;for (int i = 0; i < tempList.length; i++) {if(path.endsWith(File.separator)) {
temp= new File(path +tempList[i]);
}else{
temp= new File(path + File.separator +tempList[i]);
}if(temp.isFile()) {
temp.delete();
}if(temp.isDirectory()) {
delAllFile(path+ "/" + tempList[i]);//先删除文件夹里面的文件
delFolder(path + "/" + tempList[i]);//再删除空文件夹
flag = true;
}
}returnflag;
}public static voidmain(String[] args) {//TODO Auto-generated method stub
WordToHtml wordtohtml = newWordToHtml();
wordtohtml.wordToHtml("D://test.doc", "D://test.html");
System.out.println("word转html成功");
}
}
本文转自:http://www.cnblogs.com/qingxinblog/articles/3399454.html
参考文章:http://blog.csdn.net/zhuyi412546724/article/details/5825983#
java转换docx为html_Java使用Jacob转换Word为HTML相关推荐
- java 转 docx 生成 html 含图片,word公式,wps公式,自动编号,表格
引言 思路word中的docx是 Office Open XML 标准的文档格式,所以可以通过apache 的poi解析,docx的文档组件在poi中的结构是这样的 word文档 的组件 poi中的对 ...
- python docx与doc 文件互相转换
因文件格式要求,需要将docx 与doc文件相互转换,特寻找python代码,与大家共分享 from win32com import client#转换doc为docx def doc2docx(fn ...
- Java使用Jacob转换Word为HTML
从今天开始,我也要养成记录开发中遇到的问题和解决方法的好习惯! 最近开发一个Android项目,需要用到查看Word和Pdf文档的功能,由于Android没有直接显示Word和PDF文档的组件,只有一 ...
- java转换docx为doc文件_java使用poi转换doc/docx为pdf
为了方便前端预览word文件,上传后进行pdf转换(也可以预览时生成临时文件)*注word中插入的表格的话表格内字体都要为宋体不然转出来为空 引用jar包 org.apache.poi poi-oox ...
- 解决jodconverter 2.2.1 版本不支持docx、xlsx、pptx 转换成PDF格式异常
文章目录 一.基础对比 1.版本对比 2.异常现象 二.分析定位 2.1. 找异常输出处 2.2. 找异常源头 2.3. api源头 三.实现流程 3.1. 思路 3.2. 新建包重写类 3.3. 完 ...
- 解决jodconverter 2.2.1版本不支持docx、xlsx、pptx转换成PDF格式异常
Java使用openoffice将office系列文档转换为PDF 搭建好 OpenOffice + jodconverter 后,转换doc(97-2003)时正常,但是转换 docx 时报了以下错 ...
- 参考file-convert-util工具,实现doc,docx,html,md,pdf,png转换
参考 https://gitee.com/zhengqingya/file-convert-util 项目,打包到本地仓库实现doc,docx,html,md,pdf,png转换 https://gi ...
- jacob操作word excel 将word excel转换成网页形式
jacob操作word excel(来自http://sinye.iteye.com/blog/588050) 项目开发过程中,需求涉及到了各种文档转换为HTML或者网页易显示格式,现在将实现方式整理 ...
- java中各进制之间的转换(十进制转十六进制、十进制转二进制、二进制转十进制、二进制转十六进制)...
在java编辑中有没有遇到经常需要进行java中各进制之间的转换(十进制转十六进制.十进制转二进制.二进制转十进制.二进制转十六进制)的事情呢?下面我们就来分析一下各自是怎么转换的: [java] / ...
最新文章
- 22.Chain of Responsibility(职责链)模式
- MVC ---- DBHelper.ttinclude
- 2.pandas数据清洗
- 编写一程序,有2个文本框,在第一个文本框中输入一个整数,当焦点从第一个文本框离开时,第二个文本框将显示这个数的绝对值(使用FocusListener)。
- linux目录 文件 pdf,linux文件与目录管理.pdf
- Python编程从入门到实践~函数
- mysql抖动可能的原因,12 | 为什么我的MySQL会“抖”一下?
- wordpress category.php,wordpress自定义分类目录模板
- python 多核并行计算_嫌Python太慢?并行运算Process Pools三行代码给你4倍提速!
- Leetcode 27 Remove Element
- 【Steam】成就系统的制作及本地化
- 发邮件+实习+简历+
- python 列表推导式 else_列表推导式与表达式生成器在 Python 中的滥用!
- ElasticSearch 一文读懂
- java正则表达式多行匹配,正则表达式多行匹配 - Wangle_OP的个人空间 - OSCHINA - 中文开源技术交流社区...
- react18.0.0+ts路由配置
- RAKsmart高防服务器防御形式解析
- linux下查看显卡和驱动版本
- python去重计数_用Python做透视表之value_sum和value_countdistinct功能
- mysql dba工具_Github推荐:MySQL DBA不可错过的五大开源管理工具!
热门文章
- 国防科技大学计算机学院天河楼,科技创新耀天河——记国防科技大学计算机学院计算机研究所所长肖立权...
- 第34篇 Android Studio实现点击图片显示信息(一)需求
- nova-rootwrap笔记
- CloudSim Plus 能耗仿真(二)
- 项目Beta冲刺(团队)——05.25(3/7)
- 深入剖析 redis 事务机制
- 哪种蓝牙耳机最好?最具人气的十大蓝牙耳机品牌
- 启动docker中的compose.yml
- 网络攻击之WebShell
- 晓晓用计算机算一个数乘78时,四年级奥数培训教材(共77页)精品资料.pdf