用页面打开一个word 对其中一些信息添加超链接 能够在word中点击打开相关页面

解决方案 »

  1.   

    用jacob吧,它对word操作还算可以,网上可以找到相关资料
      

  2.   


    jacob 能在word的某些内容上加超链接不
      

  3.   

    jacob确实可以实现:
    import com.jacob.activeX.ActiveXComponent;
    import com.jacob.com.ComThread;
    import com.jacob.com.Dispatch;
    import com.jacob.com.Variant;public class WordManager {
    private Dispatch doc;
    private Dispatch docu;
    private ActiveXComponent word;// word运行程序对象
    private Dispatch documents;// 所有word文档集合
    private Dispatch selection;// 选定的范围或插入点
     private boolean saveOnExit = true;//退出时是否保存文件 默认保存
    public WordManager() {
            ComThread.InitSTA();
            if (word == null) {
                word = new ActiveXComponent("Word.Application");
                word.setProperty("Visible", new Variant(true));
            }
            if (documents == null)
                documents = word.getProperty("Documents").toDispatch();
        }
    public WordManager(boolean flag) {//word对象可见可控
            ComThread.InitSTA();
            if (word == null) {
                word = new ActiveXComponent("Word.Application");
                word.setProperty("Visible", new Variant(flag));//是否可见
            }
            if (documents == null)
                documents = word.getProperty("Documents").toDispatch();
        }
    /**
         * 打开一个已存在的文档
         * 
         * @param docPath
         */
        public void openDocument(String docPath) {
            closeDocument();
            doc = Dispatch.call(documents, "Open", docPath).toDispatch();
            selection = Dispatch.get(word, "Selection").toDispatch();
            
        }
       /**
         * 设置退出时参数
         * 
         * @param saveOnExit
         *            boolean true-退出时保存文件,false-退出时不保存文件
         */
        public void setSaveOnExit(boolean saveOnExit) {
            this.saveOnExit = saveOnExit;
        }
        
        /**
         * 关闭当前word文档
         * 
         */
        public void closeDocument() {
            if (doc != null) {
                Dispatch.call(doc, "Save");
                Dispatch.call(doc, "Close", new Variant(saveOnExit));
                doc = null;
            }
        }
        /**
         * 把插入点移动到文件首位置
         * 
         */
        public void moveStart() {
            if (selection == null)
                selection = Dispatch.get(word, "Selection").toDispatch();
            Dispatch.call(selection, "HomeKey", new Variant(6));
        }
        /**
         * 文件保存或另存为
         * 
         * @param savePath
         *            保存或另存为路径
         */
        public void save(String savePath) {
            Dispatch.call(doc, "SaveAs", savePath); // 保存
            /*
             * Dispatch.call(Dispatch.call(word, "WordBasic").getDispatch(),
             * "FileSaveAs", savePath);//另存为
             */
        }
        /**
         * 从选定内容或插入点开始查找文本
         * 
         * @param toFindText
         *            要查找的文本
         * @return boolean true-查找到并选中该文本,false-未查找到文本
         */
        public boolean find(String toFindText) {
            if (toFindText == null || toFindText.equals(""))
                return false;
            // 从selection所在位置开始查询
            Dispatch find = Dispatch.call(selection, "Find").toDispatch();
            // 设置要查找的内容
            Dispatch.put(find, "Text", toFindText);
            // 向前查找
           // Dispatch.put(find, "Forward", "True");
            // 设置格式
           // Dispatch.put(find, "Format", "True");
            // 大小写匹配
          //  Dispatch.put(find, "MatchCase", "True");
            // 全字匹配
          //  Dispatch.put(find, "MatchWholeWord", "True");
            // 查找并选中
            return Dispatch.call(find, "Execute").getBoolean();
        }
        /**
         * 添加超链接
         * @param name 链接显示的文字
         * @param url  链接地址
         */
        public void addLink(String name,String url){
         word.setProperty("Visible", new Variant(true));
    Dispatch.put(selection, "Text", name);
    Object oRange = Dispatch.call(selection, "Range");
    Dispatch oLink = Dispatch.get(doc, "Hyperlinks").toDispatch();
    Dispatch.call(oLink, "Add", oRange, url);
        }
        /**
         * 全局替换链接
         * @param toFindText
         * @param newText
         */
            public void replaceAllLink(String toFindText, String link) {
                while (find(toFindText)) {
                    addLink(toFindText,link);
                    Dispatch.call(selection, "MoveRight");
                }
            }
            /**
             * 关闭全部应用
             * 
             */
            public void close() {
                closeDocument();
                if (word != null) {
                    Dispatch.call(word, "Quit");
                    word = null;
                }
                selection = null;
                documents = null;
                ComThread.Release();
            }
            public static void main(String [] arg0){
             WordManager ws=new WordManager();
             ws.openDocument("D://text.doc");
             ws.replaceAllLink("Google", "http://localhost:8092/");
             ws.save("D://text.doc");
             ws.closeDocument();
             ws.close();
            }
    }
    不过使用jacob必须在本地机上安装office 和jacob插件 而且只能在服务器上打开添加 不适合web开发。