利用C#向word文檔中寫入資料

來源:互聯網
上載者:User
一.建立WORD模板 完整安裝Word 2003

       建立一個word,選中視圖--工具列--表單,點擊文字型表單域添加一個域。雙擊新添加的域,預設文字填寫你要顯示的文字,網域設定--書籤填入一個標籤名,用於在程式中識別這個域

  二.添加COM引用---Microsoft  Word  11.0  Object Library

  三 .添加命名空間---using Microsoft.Office.Interop.Word;

  四.定義對象

       ApplicationClass app = null;//定義應用程式物件         Document doc = null;        //定義word文檔對象         Object missing = System.Reflection.Missing.Value;//定義空變數         Object isReadOnly = false;  五.方法

1)開啟word模板

view plaincopy to clipboardprint?
  1. /// <summary>   
  2.   
  3. /// 開啟模板word文檔   
  4.   
  5. /// </summary>   
  6.   
  7. /// <param name="parFilePath">文檔路徑</param>   
  8.   
  9. private void OpenDocument(string parFilePath)   
  10.   
  11. {   
  12.   
  13. object filePath = parFilePath;//文檔路徑   
  14.   
  15. app = new ApplicationClass();   
  16.   
  17. //開啟文檔   
  18.   
  19. doc = app.Documents.Open(ref filePath, ref missing, ref isReadOnly, ref missing, ref missing,   
  20.   
  21.            ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,   
  22.   
  23.            ref missing, ref missing, ref missing, ref missing);   
  24.   
  25.            doc.Activate();//啟用文檔   
  26.   
  27. }  

/// <summary></p><p>/// 開啟模板word文檔</p><p>/// </summary></p><p>/// <param name="parFilePath">文檔路徑</param></p><p>private void OpenDocument(string parFilePath)</p><p>{</p><p>object filePath = parFilePath;//文檔路徑</p><p>app = new ApplicationClass();</p><p>//開啟文檔</p><p>doc = app.Documents.Open(ref filePath, ref missing, ref isReadOnly, ref missing, ref missing,</p><p> ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,</p><p> ref missing, ref missing, ref missing, ref missing);</p><p> doc.Activate();//啟用文檔</p><p>}view plaincopy to clipboardprint?

  1. 2.向word文檔寫入資料  

2.向word文檔寫入資料view plaincopy to clipboardprint?

  1. <PRE class=csharp name="code">/// <summary>   
  2.   
  3. /// 向word文檔寫入資料   
  4.   
  5. /// </summary>   
  6.   
  7. /// <param name="parLableName">域標籤</param>   
  8.   
  9. /// <param name="parFillName">寫入域中的內容</param>   
  10.   
  11. private void WriteIntoDocument(string parLableName, string parFillName)   
  12.   
  13. {   
  14.   
  15. object lableName = parLableName;   
  16.   
  17. Bookmark bm = doc.Bookmarks.get_Item(ref lableName);//返回標籤   
  18.   
  19. bm.Range.Text = parFillName;//設定域標籤的內容   
  20.   
  21. }</PRE>   
  22. <PRE class=csharp name="code"> </PRE>   
  23. <PRE class=csharp name="code">3.儲存並關閉</PRE>   
  24. <PRE class=csharp name="code"><PRE class=csharp name="code"> /// <summary>   
  25.   
  26. /// 儲存並關閉   
  27.   
  28. /// </summary>   
  29.   
  30. /// <param name="parSaveDocPath">文檔另存新檔的路徑</param>   
  31.   
  32. private void SaveAndClose(string parSaveDocPath)   
  33.   
  34.  {   
  35.   
  36. object savePath = parSaveDocPath;//文檔另存新檔的路徑   
  37.   
  38. Object saveChanges = app.Options.BackgroundSave;//關閉doc文檔不提示儲存   
  39.   
  40. //文檔另存新檔   
  41.   
  42. doc.SaveAs(ref savePath, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,   
  43.   
  44.                 ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);   
  45.   
  46. doc.Close(ref saveChanges, ref missing, ref missing);//關閉文檔   
  47.   
  48. app.Quit(ref missing, ref missing, ref missing);     //關閉應用程式   
  49.   
  50. }</PRE>   
  51. </PRE>  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.