ASP.NET 匯入word內容到網頁技術詳解

來源:互聯網
上載者:User

開發平台:Visual Studio 2005 語言:C#

首先添加應用"Microsoft Word 11.0 Object library"的COM組件,從而來實現讀取word文檔的對象。

然後添加命名空間using System.Windows.Forms; //添加應用,添加System.Windows.Forms  的.NET組件

代碼實現:
object filepath = this.FileUpload1.PostedFile.FileName;//取檔案名稱
Word.Application WordApp = new Word.ApplicationClass();
object nullobj = System.Reflection.Missing.Value;
Word.Document doc = WordApp.Documents.Open(ref filepath, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj);
doc.ActiveWindow.Selection.WholeStory();//全部選擇word中的文檔
//doc.ActiveWindow.Selection.Copy();
//IDataObject data=Clipboard.GetDataObject();
//txtFileContent.Text=data.GetData(DataFormats.Text).ToString();
//有些以上的做法,通過copy的方式,得到剪貼簿的內容,這種做法取到的data為null根本不能達到效果,此時是因為word中的剪下板和系統剪下板並不一樣
this.txtFileContent.Text = doc.ActiveWindow.Selection.Text;//使網頁中的txtFileContent控制項的text等於選擇的文檔
doc.Close(ref nullobj, ref nullobj, ref nullobj);//關閉
WordApp.Quit(ref nullobj, ref nullobj, ref nullobj);//離開

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.