posts - 40,  comments - 4,  trackbacks - 0
          使用Java將Word轉(zhuǎn)為Html或txt。前一段時間為這個問題頭疼的不得了,后來查閱了資料終于解決了;現(xiàn)將程序放出,以便以后參考。
            //------------------------------------------------------------------------------
            //版權(quán)所有 (C) 浪潮集團商用系統(tǒng)有限公司? 保留所有權(quán)利
            //文件名稱: wordtohtml?????????? 文件版本: 1.00.00
            //作??? 者: 郭鑄???? 作者郵箱: guozhu@langchao.com? 完成日期: 2004-10-20
            //文件描述:
            //其它描述:
            //類 列 表:
            //? wordtohtml: 將指定目錄下面所有的doc文件轉(zhuǎn)化為HTML并存儲在相同目錄下
            //修改歷史:
            //? #?? 版本???? 修改日期??? 作者???????????????? 修改內(nèi)容
            //? ----------------------------------------------------------------------------
            //? 1?? 1.00.01? 2004-10-14? 作者姓名???????????? 修改內(nèi)容描述
            //? ----------------------------------------------------------------------------
            //------------------------------------------------------------------------------
            import com.jacob.com.*;
            import com.jacob.activeX.*;
            import java.io.*;

            //取得指定目錄下面所有的doc文件名稱
            public class wordtohtml
            {
            //------------------------------------------------------------------------------
            //方法原型: change(String paths)
            //功能描述: 將指定目錄下面所有的doc文件轉(zhuǎn)化為HTML并存儲在相同目錄下
            //輸入?yún)?shù): String
            //輸出參數(shù): 無
            //返 回 值: 無
            //其它說明: 遞歸
            //------------------------------------------------------------------------------
            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("當前正在轉(zhuǎn)換......");
            //打印當前目錄路徑
            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;
            //要轉(zhuǎn)換的word文件
            String tpFile = htmlpath;
            //HTML文件

            boolean flag = false;
            
            try
            {
            app.setProperty("Visible", new Variant(false));
            //設(shè)置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("轉(zhuǎn)化完畢!");
            }
            }
            else
            {
            pathss = paths;
            //進入下一級目錄
            pathss = pathss + lists[i].getName() + "\\";???
            //遞歸遍歷所有目錄
            change(pathss, savepaths);
            }
            }
            
            }
            //------------------------------------------------------------------------------
            //方法原型: main(String[] args)
            //功能描述: main文件
            //輸入?yún)?shù): 無
            //輸出參數(shù): 無
            //返 回 值: 無
            //其它說明: 無
            //------------------------------------------------------------------------------?
            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包是一個開源的東東,網(wǎng)上搜索即得。
            Dispatch.invoke(doc,"SaveAs", Dispatch.Method, new Object[]{tpFile,new Variant(8)}, new int[1]);
            修改Variant(8)},里面得參數(shù)即可將Word轉(zhuǎn)化為各種類型。

          posted on 2006-10-25 16:29 larryjava 閱讀(239) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導航:
           
          主站蜘蛛池模板: 盐山县| 侯马市| 冷水江市| 芮城县| 噶尔县| 普格县| 资溪县| 岢岚县| 安丘市| 措勤县| 柳河县| 正宁县| 乾安县| 温州市| 江津市| 海丰县| 陵川县| 宝山区| 芦山县| 临湘市| 隆昌县| 明光市| 繁昌县| 镶黄旗| 土默特左旗| 杭锦后旗| 阿克苏市| 清远市| 阳城县| 铜梁县| 哈尔滨市| 乌兰县| 昭平县| 教育| 襄樊市| 酒泉市| 江都市| 安龙县| 专栏| 三江| 苍山县|