Word格式转化为Html

package com.wiseweb.util;

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

public class WordtoHtml {

	//------------------------------------------------------------------------------
	//方法原型: change(String paths)
	//功能描述: 将指定目录下面所有的doc文件转化为HTML并存储在相同目录下
	//输入参数: String
	//输出参数: 无
	//返 回 值: 无
	//其它说明: 递归
	//------------------------------------------------------------------------------ 
	  public static boolean change (String FolderPath,String FileName){

	        String FileFormat = "";
	        System.out.println(FolderPath);
	        FileFormat = FileName.substring(FileName.length()-4,FileName.length());
	        System.out.println(FileFormat);

	        if(FileFormat.equalsIgnoreCase(".doc"))
	        {
	            String DocFile = FolderPath +"//"+ FileName;

	            System.out.println("word文件路径:"+DocFile);
	            //word文件的完整路径

	            String HtmlFile = DocFile.substring(0, (DocFile.length() - 4)) + ".html";

	            System.out.println("htm文件路径:"+HtmlFile);
	            //html文件的完整路径

	            ActiveXComponent app = new ActiveXComponent("Word.Application");
	            //启动word

	            try
	            {
	                app.setProperty("Visible", new Variant(false));
	                //设置word程序非可视化运行

	                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(); 
	                //打开word文件

	                Dispatch.invoke(doc,"SaveAs",Dispatch.Method, new Object[]{HtmlFile,new Variant(8)}, new int[1]);
	                //作为htm格式保存文件

	                Dispatch.call(doc, "Close",new Variant(false));
	                //关闭文件

	            }
	            catch (Exception e)
	            {
	                e.printStackTrace();
	            }
	            finally
	            {
	                app.invoke("Quit", new Variant[] {});
	                //退出word程序
	            }
	            //转化完毕
	            return true;
	        }
	        return false;
	    }


	//------------------------------------------------------------------------------
	//方法原型: main(String[] args)
	//功能描述: main文件
	//输入参数: 无
	//输出参数: 无
	//返 回 值: 无
	//其它说明: 无
	//------------------------------------------------------------------------------  
	 public static void main(String[] args)
	 {
	  
//	  String paths = new String("E://wordToHtml");
	  String paths = new String("E:");
//	  String filename = "a.doc";
	  String filename = "servlet和jsp学习指南(Budi kurniawan).doc";

	  change(paths, filename);

	 }



}
其实原理就是利用word把文档打开,然后另存为html格式就ok了。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值