word转化为html

本文介绍了一个使用Java实现的简单工具,该工具可以将Microsoft Word文档(.doc)转换成HTML格式,并提供了删除源文件的功能。转换过程利用了COM组件与Word应用程序交互来完成。
package word;

import java.io.File;

import jp.ne.so_net.ga2.no_ji.jcom.*;

public class TestWord {
	/**
	 * 删除文件
	 * @param fileName
	 * @return
	 */
	public static boolean deleteFile(String fileName) {
		File file = new File(fileName);
		if (file.isFile() && file.exists()) {

			file.delete();

			System.out.println("删除单个文件" + fileName + "成功!");
			return true;
		} else {
			System.out.println("删除单个文件" + fileName + "失败!");
			return false;
		}
	}
/**
 * 转化文件word为html
 * @param wordname
 * @param htmname
 */
	public static void changeWordToHtm(String wordname, String htmname) {

		

		ReleaseManager rm = new ReleaseManager();

		String userdir = System.getProperty("user.dir");

		String tempHtmlFile = userdir + "/" + htmname;
		System.out.println(tempHtmlFile);

		try {

			IDispatch wdApp = new IDispatch(rm, "Word.Application");

			wdApp.put("Visible", new Boolean(false));

			IDispatch wdDocuments = (IDispatch) wdApp.get("Documents");

			IDispatch wdDocument = (IDispatch) wdDocuments.method("Open",

			new Object[] { wordname });

			// 转换为HTML文件

			wdDocument.method("SaveAs", new Object[] { tempHtmlFile, 8 });

			wdApp.method("Quit", null);

			System.out.println("文件已经存放");
		

		} catch (Exception e) {

			e.printStackTrace();

		} finally {

			rm.release();
			System.out.println("释放完毕");

		}

	}

	public static void main(String[] args) {

		TestWord.changeWordToHtm("D:\\11.doc", "11.html");
		TestWord.deleteFile("D:/11.doc");

	}

}

 将jcom.dll放到jak的bin下面

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值