首页 > 开发 > Java > 正文

使用Java将Word转为Html或txt!

2024-07-13 09:55:36
字体:
来源:转载
供稿:网友

  使用java将word转为html或txt。前一段时间为这个问题头疼的不得了,后来查阅了资料终于解决了;现将程序放出,以便以后参考。


  //-------------------------------------------------------
  //版权所有 (c) 浪潮集团商用系统有限公司  保留所有权利
  //文件名称: wordtohtml           文件版本: 1.00.00
  //作    者: 郭铸     作者邮箱: [email protected]  完成日期: 2004-10-20
  //文件描述:
  //其它描述:
  //类 列 表:
  //  wordtohtml: 将指定目录下面所有的doc文件转化为html并存储在相同目录下
  //修改历史:
  //  #   版本     修改日期    作者                 修改内容
  //  -------------------------------------------
  //  1   1.00.01  2004-10-14  作者姓名             修改内容描述
  //  ----------------------------------------------------------
  //-------------------------------------------------------
  import com.jacob.com.*;
  import com.jacob.activex.*;
  import java.io.*;

  //取得指定目录下面所有的doc文件名称
  public class wordtohtml
  {
  //-------------------------------------------------
  //方法原型: change(string paths)
  //功能描述: 将指定目录下面所有的doc文件转化为html并存储在相同目录下
  //输入参数: string
  //输出参数: 无
  //返 回 值: 无
  //其它说明: 递归
  //--------------------------------------------
  public static void change(string paths, string savepaths)
  {
  
  file d = new file(paths);
  //取得当前文件夹下所有文件和目录的列表
  file lists[] = d.listfiles();
  string pathss = new string("");

  //对当前目录下面所有文件进行检索
  for(int i = 0; i < lists.length; i ++)
  {
  if(lists[i].isfile())
  {
  string filename = lists[i].getname();
  string filetype = new string("");
  //取得文件类型
  filetype = filename.substring((filename.length() - 3), filename.length());
  
  //判断是否为doc文件
  if(filetype.equals("doc"))
  {
  system.out.println("当前正在转换......");
  //打印当前目录路径
  system.out.println(paths);
  //打印doc文件名
  system.out.println(filename.substring(0, (filename.length() - 4)));
  
  activexcomponent app = new activexcomponent("word.application");//启动word
  
  string docpath = paths + filename;
  string htmlpath = savepaths + filename.substring(0, (filename.length() - 4));
  
  string infile = docpath;
  //要转换的word文件
  string tpfile = htmlpath;
  //html文件

  boolean flag = false;
  
  try
  {
  app.setproperty("visible", new variant(false));
  //设置word不可见

  object docs = app.getproperty("documents").todispatch();
  object doc = dispatch.invoke(docs,"open", dispatch.method, new object[]{infile,new variant(false), new variant(true)}, new int[1]).todispatch();
  //打开word文件
  dispatch.invoke(doc,"saveas", dispatch.method, new object[]{tpfile,new variant(8)}, new int[1]);
  //作为html格式保存到临时文件
  variant f = new variant(false);
  dispatch.call(doc, "close", f);
  flag = true;
  }
  catch (exception e)
  {
  e.printstacktrace();
  }
  finally
  {
  app.invoke("quit", new variant[] {});
  }
  system.out.println("转化完毕!");
  }
  }
  else
  {
  pathss = paths;
  //进入下一级目录
  pathss = pathss + lists[i].getname() + "//";   
  //递归遍历所有目录
  change(pathss, savepaths);
  }
  }
  
  }
  //---------------------------------------------------------
  //方法原型: main(string[] args)
  //功能描述: main文件
  //输入参数: 无
  //输出参数: 无
  //返 回 值: 无
  //其它说明: 无
  //---------------------------------------------------------- 
  public static void main(string[] args)
  {
  
  string paths = new string("d://work//2004.10.8//test system//test01//word//");
  string savepaths = new string ("d://work//2004.10.8//test system//test01//html//");

  change(paths, savepaths);

  }
  }

  其中import的jar包是一个开源的东东,网上搜索即得。
  dispatch.invoke(doc,"saveas", dispatch.method, new object[]{ tpfile,new variant(8)}, new int[1]);
  修改variant(8)},里面得参数即可将word转化为各种类型。

中国最大的web开发资源网站及技术社区,
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表