首頁  >  文章  >  web前端  >  JavaScript開啟word文檔

JavaScript開啟word文檔

高洛峰
高洛峰原創
2016-11-26 10:07:232769瀏覽

在C#中開啟word文件其實不算太難,方法也比較多。
一.C#中開啟word文件方法
//在專案參考裡新增上對Microsoft Word 11.0 object library的參考

    private void button1_Click(object sender, System..Arg 它取得要開啟的文件WORD文件,RTF文件,文字檔案路徑名稱
 OpenFileDialog opd = new OpenFileDialog();
    opd.InitialDirectory = "c:\\";
 opd.Filter = "Word*.docdoc = "W| *.doc|文字文件(*.txt)|*.txt|RTF文檔(*.rtf)|*.rtf|所有文件(*.*)|*.*";
 opd.FilterIndex = 1;

if (opd.ShowDialog() == DialogResult.OK && opd.FileName.Length > 0)

    {


    //建立Wordcation . app = new Word.ApplicationClass();

    Word.Document doc = null;

    object missing = System.Reflection.Missing.Value;

    object isVisible = true;
    object index = 0;

    try

    {
     doc = app.Documents.Open(ref FileName0 sing, ref missing,
      ref missing, ref missing , ref missing, ref isVisible, ref missing,
      ref missing, ref missing, ref missing);

     doc.ActiveWindow.Selection.Whptory(       //從剪切板取得資料
 IDataObject data=Clipboard.GetDataObject();
     this.richTextBox1.Text=data.GetData(DataFormats.Text).ToString();
!= null)
     {

      doc.Close(ref missing, ref missing, ref missing);

      doc = null;
    ,  app.Quit(ref missing, ref missing, ref missing);
      app = null;[Page]
       }
      }

    }

   }
但是,如果我們怎麼用javascript怎麼打開呢?其實,也不難。
二.在javascript開啟word文檔 www.2cto.com
我們新建一個html文件,並且寫一個FileUpLoad以及button控制項。
flUpload

然後,在寫一個javascript OpenFile方法。
function OpenFile()
    {
       if (document.getElementById("flUpload").value.toUpperCase().indexOf     var objExcel;
            objExcel = new ActiveXObject(" Excel.Application");
            objExcel.Visible = true;
           }
      
       else if (document.getElementById("flUpload").value. toUpperCase().indexOf(".DOC") != -1)
       {
             
            objDoc.Visible = true;
            objDoc.Documents.Open(document .getElementById("flUpload").value);
       }
       else
       {
            return false;
       }
    }
OK。然後 在IE中就能先選入一個doc文檔,然後點open,就可以開啟了。
希望對你有幫助。



陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn