jacob转html( 转)

 

客户要求上传的doc和xls附件能直接打开预览,不要下载后才能打开的。想到将如果将doc转成HTML不就OK 了吗?网上还真找到了转换之法:jacob。
此例演示在jdk6下将word、excel和ppt文件转成html文件。
1.首先需要jacob包。可以从http://danadler.com/jacob/上取的。
2.解压得到两个文件jacob.jar(直接放到classpath下去就行了),还有一个jacob.dll放到 system32或者 jdk安装目录的jdk/jre/bin目录中就OK了。
3.传说如果是win2003还需要安装一个东西。http://www.microsoft.com/downloads/details.aspx?familyid=200B2FD9-AE1A-4A14-984D-389C36F85647&displaylang=en&displaylang=en
不过我的是xp,不需要。

4.貌似需要安装offices,因为jacob是调用微软offices去转化文件的。(不知道有什么替代的办法没有)

 

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

public class Jacob2Html {
	private static final Log log = LogFactory.getLog(Jacob2Html.class);

	/**
	 * PowerPoint转成HTML
	 * 
	 * @param pptPath
	 *            PowerPoint文件全路径
	 * @param htmlfile
	 *            转换后HTML存放路径
	 */
	public static Boolean pptToHtml(String pptPath, String htmlPath) {
		Boolean b = false;
		ActiveXComponent offCom = new ActiveXComponent("PowerPoint.Application");
		try {
			offCom.setProperty("Visible", new Variant(true));
			Dispatch excels = offCom.getProperty("Presentations").toDispatch();
			Dispatch excel = Dispatch.invoke(
					excels,
					"Open",
					Dispatch.Method,
					new Object[] { pptPath, new Variant(false),
							new Variant(false) }, new int[1]).toDispatch();
			Dispatch.invoke(excel, "SaveAs", Dispatch.Method, new Object[] {
					htmlPath, new Variant(12) }, new int[1]);
			Variant f = new Variant(false);
			Dispatch.call(excel, "Close");
			b=true;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			offCom.invoke("Quit", new Variant[] {});
			ComThread.Release();
		}
		return b;
	}

	/**
	 * WORD转成HTML
	 * 
	 * @param wordPath
	 *            WORD文件全路径
	 * @param htmlPath
	 *            生成的HTML存放路径
	 */
	public static Boolean wordToHtml(String wordPath, String htmlPath) {
		Boolean b = false;
		ActiveXComponent offCom = new ActiveXComponent("Word.Application");
		try {
			offCom.setProperty("Visible", new Variant(false));
			Dispatch wordDis = offCom.getProperty("Documents").toDispatch();
			Dispatch doc = Dispatch.invoke(
					wordDis,
					"Open",
					Dispatch.Method,
					new Object[] { wordPath, new Variant(true),
							new Variant(true) }, new int[1]).toDispatch();
			Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] {
					htmlPath, new Variant(8) }, new int[1]);
			Variant f = new Variant(false);
			Dispatch.call(doc, "Close", f);
			b=true;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			offCom.invoke("Quit", new Variant[] {});
		}
		return b;
	}

	/**
	 * EXCEL转成HTML
	 * 
	 * @param xlsfile
	 *            EXCEL文件全路径
	 * @param htmlfile
	 *            转换后HTML存放路径
	 */
	public static Boolean excelToHtml(String excelPath, String htmlPath) {
		Boolean b = false;
		ActiveXComponent offCom = new ActiveXComponent("Excel.Application");
		try {
			offCom.setProperty("Visible", new Variant(false));
			Dispatch excels = offCom.getProperty("Workbooks").toDispatch();
			Dispatch excel = Dispatch.invoke(
					excels,
					"Open",
					Dispatch.Method,
					new Object[] { excelPath, new Variant(false),
							new Variant(true) }, new int[1]).toDispatch();
			Dispatch.invoke(excel, "SaveAs", Dispatch.Method, new Object[] {
					htmlPath, new Variant(44) }, new int[1]);
			Variant f = new Variant(false);
			Dispatch.call(excel, "Close", f);
			b=true;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			offCom.invoke("Quit", new Variant[] {});
			ComThread.Release();
		}
		return b;
	}

	public static void main(String[] args) {
		 wordToHtml("c:/a.doc","c:/a.html");
//		pptToHtml("D:/test/1ppt.ppt", "D:/test/1ppt.html");
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值