C#讀取word文檔實(shí)例淺析
C#讀取word文檔是如何實(shí)現(xiàn)的呢?我們可以使用FileStream對(duì)象來把文本文件里面的信息讀取出來,但是對(duì)于word文檔來說就不能使用這樣的方法了.
這種情況下C#讀取word文檔的實(shí)現(xiàn)我們需要使用叫做” Microsoft Word 9.0 object library”COM組件來實(shí)現(xiàn),它為我們提供了所有用來讀取word文檔的對(duì)象和方法.
這里我們主要用Word.ApplicationClass下的方法來處理word應(yīng)用程序.實(shí)現(xiàn)的思路是先在內(nèi)存中把這個(gè)word文檔打開,然后把里面的內(nèi)容全部拷貝的剪切板中,***再把數(shù)據(jù)從剪切板里面取出來.
C#讀取word文檔實(shí)例代碼如下:
- Word.ApplicationClass wordApp=new ApplicationClass();
- object file=path;
- object nullobj=System.Reflection.Missing.Value;
- Word.Document doc = wordApp.Documents.Open(
- ref file, 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();
- doc.ActiveWindow.Selection.Copy();
- IDataObject data=Clipboard.GetDataObject();
- txtFileContent.Text=data.GetData(DataFormats.Text).ToString();
- doc.Close();
C#讀取word文檔的相關(guān)內(nèi)容就向你介紹到這里,希望對(duì)你了解和學(xué)習(xí)C#讀取word文檔有所幫助。
【編輯推薦】