从今天开始,我也要养成记录开发中遇到的问题和解决方法的好习惯!

最近开发一个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转换也很费劲。

1、到官网下载Jacob,目前最新版是1.17,地址链接:http://sourceforge.net/projects/jacob-project/

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 class JacobUtil {

    // 8 代表word保存成html
public static final int WORD_HTML = 8; public static void main(String[] args) {
String docfile = "C:\\Users\\無名\\Desktop\\xxx.doc";
String htmlfile = "C:\\Users\\無名\\Desktop\\xxx.html";
JacobUtil.wordToHtml(docfile, htmlfile);
} /**
* WORD转HTML
* @param docfile WORD文件全路径
* @param htmlfile 转换后HTML存放路径
*/
public static void wordToHtml(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, new Object[] {
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", new Variant[] {});
}
System.out.println("*****转换完毕********");
}
}

EXCEL转HTML代码:

package test;

import java.io.File;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant; public class WordToHtml { int WORD_HTML = 8;
int WORD_TXT = 7;
int EXCEL_HTML = 44; /**
* WORD转HTML
* @param docfile WORD文件全路径
* @param htmlfile 转换后HTML存放路径
*/
public void wordToHtml(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, new Object[] {
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", new Variant[] {});
}
} /**
* EXCEL转HTML
* @param xlsfile EXCEL文件全路径
* @param htmlfile 转换后HTML存放路径
*/
public void excelToHtml(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, new Object[] {
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", new Variant[] {});
}
} /**
* /删除指定文件夹
* @param folderPath 文件夹全路径
* @param htmlfile 转换后HTML存放路径
*/
public void delFolder(String folderPath) {
try {
delAllFile(folderPath); // 删除完里面所有内容
String filePath = folderPath;
filePath = filePath.toString();
java.io.File myFilePath = new java.io.File(filePath);
myFilePath.delete(); // 删除空文件夹
} catch (Exception e) {
e.printStackTrace();
}
} /**
* /删除指定文件夹下所有文件
* @param path 文件全路径
*/
public boolean delAllFile(String path) {
boolean flag = false;
File file = new File(path);
if (!file.exists()) {
return flag;
}
if (!file.isDirectory()) {
return flag;
}
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;
}
}
return flag;
} public static void main(String[] args) {
// TODO Auto-generated method stub
WordToHtml wordtohtml = new WordToHtml();
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#

最新文章

  1. Java不同编码方式,中英文字符所占字节数
  2. &#39;scrapyd-deploy&#39; 不是内部或外部命令,也不是可运行的程序 或批处理文件。
  3. 【python】遍历类的所有成员
  4. __thread关键字[转]
  5. Conway&#39;s Game of Life: An Exercise in WPF, MVVM and C#
  6. SqlServer 中如何查看某一个Sql语句是复用了执行计划,还是重新生成了执行计划
  7. 汇编debug 截图3
  8. jquery中onclick=&quot;fn&quot;中$(this)所代表的对象
  9. openstacks
  10. PHP 魔术常量__FUNCTION__与__METHOD__的区别
  11. xrdp的rdp前端无法连接Windows 2008的问题
  12. PHP语言基础06 MySql By ACReaper
  13. 微信公众平台开发-OAuth2.0网页授权(含源码)
  14. 记录linux tty的一次软锁排查2
  15. Java开源生鲜电商平台-财务系统模块的设计与架构(源码可下载)
  16. gp工具的许可
  17. eclipse 打包
  18. kafka+docker+python
  19. Kafka设计解析(十三)Kafka消费组(consumer group)
  20. c# 读取confgi文件

热门文章

  1. 搭建windows下的odoo开发环境
  2. MVC上传多张图片
  3. WPF popup控件的使用
  4. iOS8 PUSH解决方法
  5. 九度OJ 1139:最大子矩阵 (矩阵运算、缓存)
  6. if UDP is permitted
  7. 【洛谷 2405】 non天平
  8. Shell之历史操作记录与欢迎信息
  9. delphi2010\delphi XE7 开发及调试WebService 实例
  10. Linux下直接读写物理地址内存