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转换也很费劲。

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

2、将压缩包解压后,Jacob.jar添加到Libraries中(先复制到项目目录中,右键单击jar包选择Build Path—>Add to Build Path);

3、将Jacob.dll放至当前项目所用到的“jrein”下面(比如我的Eclipse正在用的Jre路径是D:Javajdk1.7.0_17jrein)。
Ps:我就是按照上面的步骤配置的,一点问题没有,但是有些人可能还会报错,比如:java.lang.UnsatisfiedLinkError: no jacob in java.library.path,这是系统没有加载到jacob.dll,网上解决方法是将Jacob.dll放至“WINDOWSSYSTEM32”下面(我没试过,因为我的直接没问题)。

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#

原文地址:https://www.cnblogs.com/dreammyle/p/4486791.html