不要说JavaScript了,连java都难以做到。
因为word文档是微软自己设置特定格式的文档。它不像txt是纯文本的,也不像pdf是开源的,它是商业机密。
所以目前能够操作word文档的程序很少,apache有一个POI的项目,关于word的部分也不得不停做了。

解决方案 »

  1.   

    可以做到,不过需要开放权限
    例子如下:
    1、先在C盘根目录建立 test.doc (模拟下载)
    2、在 test.doc 中加入 #test# (将被替换的内容。如果有多个关键词,就调用多次 replaceString 方法)
    3、运行下面的代码
    4、上传,并删除本地文件(这一步不知道怎么实现)<script>
    var cntOperation_Replace = 1;
    var cntOperation_Paste = 2;
    function toWord(){
    //打开文件
    var wdApp = null;
    try{
    wdApp = new ActiveXObject("Word.Application");
    wdApp.Application.Visible =1;
    var wdDoc=wdApp.Documents.Open("c:\\test.doc");// 绝对路径
    wdApp.Application.ActiveWindow.WindowState=2;
    }catch(e){
    alert("打开文件出错:"+e.message);
    return false;
    }
    //替换内容
    try{
    var testcontent="替换后的内容";
    replaceString(cntOperation_Replace, wdApp, "#test#",testcontent);
    }catch(e){
    alert("替换文件内容出错:"+e.message);
    return false;
    }finally{
    wdApp = null;
    wdDoc = null;
    }
    }/*
    功能: 将标记替换为指定文本
    intOperation: 1-替换指定文本 2-插入(粘贴)正文
    App: 文档对象
    strLabel: 需要进行替换的标记
    strValue: 替换内容
    */
    function replaceString(intOperation, App, strLabel, strValue){
    try{
    App.Selection.Find.Text = strLabel;
    App.Selection.Find.Execute();
    App.Selection.Find.Forward = true;
    App.Selection.Find.Wrap = 2;
    App.Selection.Find.MatchByte = true;
    if(App.Selection.Find.Found){
    App.Selection.Delete();
    if (intOperation==cntOperation_Replace){
    // 替换指定文本
    App.Selection.InsertAfter(strValue);
    }else{
    // 粘贴正文
    App.Selection.Paste();
    }
    }
    }catch(e){
    alert("替换文件内容出错!出错描述:"+e.message);
    return false;
    }
    }toWord();
    </script>
      

  2.   

    sd5816690 兄弟,那个模拟下载不行啊,怎么能下载到?比如我的下载路径是http://xxxx.xxxx.xxx/xxx.doc,我要下到c盘根目录怎么做?
      

  3.   

    在线编辑office文件用java实现很困难
    你需要捕捉文件的关闭或保存事件,然后做上传的动作。2楼你用的是vbscript,javascript几乎不可能完成这个任务啊
    另,wdApp.Documents.Open("c:\\test.doc");就不能Open一个url吗?我懒得试了
    如果谁有好主意我也想看看。
      

  4.   

    必须部署到服务器上,直接双击无效<script>
    var cntOperation_Replace = 1;
    var cntOperation_Paste = 2;
    var filepath = "c:\\aaa\\"// 存放到哪个文件夹
    var strUrl = "./test.doc";// URL路径,可以是绝对
    var filename = "aaa.doc";// 要保存的名称
    function toWord(){
    try{
    var objFile = new ActiveXObject("Scripting.FileSystemObject");
    if(objFile == null){
    alert("您的机器不支持Scripting.FileSystemObject.");
    return false;
    }
    if(!objFile.FolderExists(filepath))
    objFile.CreateFolder(filepath);//创建存放的临时文件
    }catch(e){
    alert("创建FileSystemObject对象出错!"+e.message);
    return false;
    } try{
    var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    if(xmlhttp == null){
    alert("您的机器不支持Microsoft.XMLHTTP.");
    return false;
    }
    xmlhttp.open("Get", strUrl,false);
    xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xmlhttp.send();
    }catch(e){
    alert("创建XMLHTTP对象出错!"+e.message);
    return false;
    }
    var ado_stream = null;
    // 文件存盘
    try{
             ado_stream = new ActiveXObject("ADODB.Stream");
    if(ado_stream == null){
    alert("您的机器不支持ADODB.Stream.");
    return false;
    }
    ado_stream.Type = 1;
    ado_stream.open();
    ado_stream.Write(xmlhttp.responseBody);
    ado_stream.SaveToFile(filepath + filename,2);
    }catch(e){
    alert("文件存盘出错:"+e.message);
    return false;
    }finally{
    ado_stream.close();
    ado_stream = null;
    } //打开文件
    var wdApp = null;
    try{
    wdApp = new ActiveXObject("Word.Application");
    wdApp.Application.Visible =1;
    var wdDoc=wdApp.Documents.Open(filepath + filename);
    wdApp.Application.ActiveWindow.WindowState=2;
    }catch(e){
    alert("打开文件出错:"+e.message);
    return false;
    }
    //替换内容
    try{
    var testcontent="替换后的内容";
    replaceString(cntOperation_Replace, wdApp, "#test#",testcontent);
    }catch(e){
    alert("替换文件内容出错:"+e.message);
    return false;
    }finally{
    wdApp = null;
    wdDoc = null;
    }
    }/*
    功能: 将标记替换为指定文本
    intOperation: 1-替换指定文本 2-插入(粘贴)正文
    App: 文档对象
    strLabel: 需要进行替换的标记
    strValue: 替换内容
    */
    function replaceString(intOperation, App, strLabel, strValue){
    try{
    App.Selection.Find.Text = strLabel;
    App.Selection.Find.Execute();
    App.Selection.Find.Forward = true;
    App.Selection.Find.Wrap = 2;
    App.Selection.Find.MatchByte = true;
    if(App.Selection.Find.Found){
    App.Selection.Delete();
    if (intOperation==cntOperation_Replace){
    // 替换指定文本
    App.Selection.InsertAfter(strValue);
    }else{
    // 粘贴正文
    App.Selection.Paste();
    }
    }
    }catch(e){
    alert("替换文件内容出错!出错描述:"+e.message);
    return false;
    }
    }toWord();
    </script>
      

  5.   

    感谢各位,对ActiveXObject有了一点点的了解,请问各位,有没有这方面的书籍?上网查了查都是VB的,对于用jscript的很少,有没有这方面的介绍啊?多谢
      

  6.   

    先说明我没想到什么办法,但是LZ需要的操作至少存在以下问题
    1.对权限太敏感了,随意下载文件有中毒的风险,随意上传文件有泄漏隐私的风险,一般计算机的浏览器都会严格限制这两种操作(特别是上传,很容易造就第二个灌稀哥)。
    2.怎么检测文件关闭呢?
    3.ActiveXObject是依赖于IE的永远都不可能跨浏览器。
    最后还是建议你用.NET做客户端去实现,.NET操作Office组件很好很强大,文件的上传下载可以用WebDAV,不黄也不暴力>_<
      

  7.   

    Office2007以后的Word文档走XML的标准,原则上说是可以控制的,不过你需要很明白它的文件格式标准。
      

  8.   

    我最近又学习了一下,好像是掉VBA的一些函数,都是word自带的库函数,本机是一定要安装word的,跟你用word操作本地word文档是一样的道理。只是安全性上要信任这个站点。
    我现在可以下载和修改保存了,多谢 sd5816690