posts - 0, comments - 77, trackbacks - 0, articles - 356
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

使用Java将Word转为Html或txt

Posted on 2007-09-07 15:29 semovy 阅读(336) 评论(0)  编辑  收藏 所属分类: JAVA应用

package org.client;

// 使用Java将Word转为Html或txt!
import com.jacob.com.*;
import com.jacob.activeX.*;
import java.io.*;

public class Client {

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

   File d = new File(paths);
   // 取得当前文件夹下所有文件和目录的列表
   File lists[] = d.listFiles();
   String path = 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文件

      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);
      } catch (Exception e) {
       e.printStackTrace();
      } finally {
       app.invoke("Quit", new Variant[] {});
      }
      System.out.println("转化完毕!");
     }
    } else {
     path = paths;
     // 进入下一级目录
     path = path + lists[i].getName() + "\\";
     // 递归遍历所有目录
     change(path, savepaths);
    }
   }
}

// ------------------------------------------------------------------------------
// 方法原型: main(String[] args)
// 功能描述: main文件
// 输入参数: 无
// 输出参数: 无
// 返 回 值: 无
// 其它说明: 无
// ------------------------------------------------------------------------------
public static void main(String[] args) {
   String paths = new String("D:\\作业\\day54\\");
   String savepaths = new String("D:\\作业\\day54\\");
   change(paths, savepaths);
}
/*
   * 其中import的jar包是一个开源的东东,网上搜索即得。 Dispatch.invoke(doc, "SaveAs",
   * Dispatch.Method, new Object[]{tpFile,new Variant(8)}, new int[1]);
   * 修改Variant(8)},里面得参数即可将Word转化为各种类型。
   */

}


只有注册用户登录后才能发表评论。


网站导航: