RT!
在线等待!

解决方案 »

  1.   

    package com.hhzskj.law;
    import java.io.*;import com.jacob.activeX.*;
    import com.jacob.com.*;//取得指定目录下面所有的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());
            filetype = filetype.toLowerCase();
            //判断是否为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
              System.out.println("use word success");
              String docpath = paths + filename;
              String htmlpath = savepaths +
                  filename.substring(0, (filename.length() - 4));          String inFile = docpath;
              //要转换的word文件
              String tpFile = htmlpath;
              //HTML文件          boolean flag = true;          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(2)}, new int[1]); // 0,10,11,15 doc 1 dot 2,3,4,5,7 txt  6 rtf 8 htm
                //作为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:\\test\\");
        String savepaths = new String("D:\\test1\\");    change(paths, savepaths);  }
    }
      

  2.   

    Dispatch.invoke(doc, "SaveAs", Dispatch.Method,
                                new Object[] {tpFile, new Variant(2)}, new int[1]); // 0,10,11,15 doc 1 dot 2,3,4,5,7 txt  6 rtf 8 htm上面new Variant(2)中的2表示将word转换成txt
      

  3.   

    ActiveXComponent("Word.Application");好像有错
      

  4.   

    1.jacob.dll文件必须放到系统path环境中去
    2.只支持office2000,2003好像不行
    3.客户端必须安装office2000
      

  5.   

    我用的jdk 是1.5的好像不行
    装了jdk 1.3的就可以了
    谢谢楼上
      

  6.   

    java操作word详解,并且有开发包下载http://www.chinajavalab.com/cgi-bin/topic.cgi?forum=1&topic=46