熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> ASP編程 >> 正文

C#打開WORD文檔內容並顯示

2022-06-13   來源: ASP編程 

  C#打開WORD文檔內容並顯示代碼如下:

  //在項目引用裡添加上對Microsoft Word object library的引用

  private void button_Click(object sender SystemEventArgs e)
      {
    //調用打開文件對話框獲取要打開的文件WORD文件RTF文件文本文件路徑名稱
    OpenFileDialog opd = new OpenFileDialog();
    opdInitialDirectory = \c:\\\\\;
    opdFilter = \Word文檔(*doc)|*doc|文本文檔(*txt)|*txt|RTF文檔(*rtf)|*rtf|所有文檔(**)|**\;
    opdFilterIndex = ;

  if (opdShowDialog() == DialogResultOK && opdFileNameLength > )
    {

  //建立Word類的實例缺點:不能正確讀取表格圖片等等的顯示
    WordApplicationClass app = new WordApplicationClass();
    WordDocument doc = null;
    object missing = SystemReflectionMissingValue;

  object FileName = opdFileName;
    object readOnly = false;
    object isVisible = true;
    object index = ;
    try
    {
     doc = appDocumentsOpen(ref FileName ref missing ref readOnly
      ref missing ref missing ref missing ref missing ref missing
      ref missing ref missing ref missing ref isVisible ref missing
      ref missing ref missing ref missing);

  docActiveWindowSelectionWholeStory();
     docActiveWindowSelectionCopy();
     //從剪切板獲取數據
     IDataObject data=ClipboardGetDataObject();
     thisrichTextBoxText=dataGetData(DataFormatsText)ToString();

  }
    finally
    {
     if (doc != null)
     {
      docClose(ref missing ref missing ref missing);
      doc = null;
     }

  if (app != null)
     {
      appQuit(ref missing ref missing ref missing);
      app = null;[Page]
       }
      }

  }

  }


From:http://tw.wingwit.com/Article/program/ASP/201311/21704.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.