自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C#操作Word學習實例淺析

開發(fā) 后端
C#操作Word學習實例主要向你介紹了C#操作Word學習中的一個練習,希望對你了解和學習C#操作Word有所幫助。

C#操作Word學習實例:首先引入類庫,Microsoft.Office.Interop.Word,然后進行編程。代碼如下:

  1. using System;   
  2. using System.Collections.Generic;   
  3. using System.ComponentModel;   
  4. using System.Data;   
  5. using System.Drawing;   
  6. using System.Text;   
  7. using System.Windows.Forms;   
  8. using Microsoft.Office.Interop.Word;   
  9.  
  10. namespace WordTest   
  11. {   
  12. public partial class Form1 : Form   
  13. {   
  14. object strFileName;   
  15. Object Nothing;   
  16. Microsoft.Office.Interop.Word.Application myWordApp =  
  17.  new Microsoft.Office.Interop.Word.ApplicationClass();   
  18. Document myWordDoc;   
  19. string strContent = "";   
  20.  
  21. public Form1()   
  22. {   
  23. InitializeComponent();   
  24. }   //C#操作Word學習實例淺析
  25.  
  26. private void button1_Click(object sender, EventArgs e)   
  27. {   
  28. createWord();   
  29. //openWord();   
  30. }   
  31.  
  32. private void createWord()   
  33. {   
  34. strFileName = System.Windows.Forms.Application.StartupPath + "test.doc";   
  35. if (System.IO.File.Exists((string)strFileName))   
  36. System.IO.File.Delete((string)strFileName);   
  37. Object Nothing = System.Reflection.Missing.Value;   
  38. myWordDoc = myWordApp.Documents.Add(ref Nothing,   
  39. ref Nothing, ref Nothing, ref Nothing);   
  40.  
  41. #region 將數(shù)據(jù)庫中讀取得數(shù)據(jù)寫入到word文件中   
  42.  
  43. strContent = "你好nnr";   
  44. myWordDoc.Paragraphs.Last.Range.Text = strContent;   
  45.  
  46. strContent = "這是測試程序";   
  47. myWordDoc.Paragraphs.Last.Range.Text = strContent;   
  48.  //C#操作Word學習實例淺析
  49.  
  50. #endregion   
  51.  
  52. //將WordDoc文檔對象的內(nèi)容保存為DOC文檔   
  53. myWordDoc.SaveAs(ref strFileName, ref Nothing,   
  54. ref Nothing, ref Nothing, ref Nothing, ref Nothing,  
  55.  ref Nothing, ref Nothing, ref Nothing, ref Nothing,  
  56.  ref Nothing, ref Nothing, ref Nothing, ref Nothing,  
  57.  ref Nothing, ref Nothing);   
  58. //關(guān)閉WordDoc文檔對象   
  59. myWordDoc.Close(ref Nothing, ref Nothing, ref Nothing);   
  60. //關(guān)閉WordApp組件對象   
  61. myWordApp.Quit(ref Nothing, ref Nothing, ref Nothing);   
  62.  
  63. this.richTextBox1.Text = strFileName + "rn" + "創(chuàng)建成功";   
  64.  
  65. }   
  66. private void openWord()   
  67. {   //C#操作Word學習實例淺析
  68. fontDialog1.ShowDialog();   
  69. System.Drawing.Font font = fontDialog1.Font;   
  70. object filepath = "D:asp.docx";   
  71. object oMissing = System.Reflection.Missing.Value;   
  72. myWordDoc = myWordApp.Documents.Open(ref filepath,  
  73.  ref oMissing, ref oMissing, ref oMissing,  
  74.  ref oMissing,   
  75. ref oMissing, ref oMissing, ref oMissing,  
  76.  ref oMissing, ref oMissing, ref oMissing,   
  77. ref oMissing, ref oMissing, ref oMissing,  
  78.  ref oMissing, ref oMissing);   
  79. myWordDoc.Content.Font.Size = font.Size;   
  80. myWordDoc.Content.Font.Name = font.Name;   
  81. myWordDoc.Save();   
  82. richTextBox1.Text = myWordDoc.Content.Text;   
  83.  
  84.  //C#操作Word學習實例淺析
  85. myWordDoc.Close(ref oMissing, ref oMissing, ref oMissing);   
  86. myWordApp.Quit(ref oMissing, ref oMissing, ref oMissing);   
  87. }   
  88.  
  89. }  

C#操作Word學習實例的基本內(nèi)容就向你介紹到這里,希望對你了解和學習C#操作Word有所幫助。

【編輯推薦】

  1. C#操作Word表的實例淺析
  2. C#操作Word表格的常見操作
  3. C#操作Word表格的彪悍實例
  4. C#操作Word實用實例淺析
  5. C#操作Word的一點認識
責任編輯:仲衡 來源: itpub.net
相關(guān)推薦

2009-08-19 09:42:52

C#操作Word書簽

2009-08-19 11:13:49

C#操作Word

2009-08-19 11:28:41

C#操作Word

2009-08-19 10:25:14

C#操作Word

2009-08-28 17:34:14

讀取word文檔

2009-09-01 13:13:28

C#打開Word文檔

2009-08-18 13:49:21

C# 操作Excel

2009-08-18 16:04:12

C# 操作Excel

2009-08-31 18:38:59

C#寫文件

2009-08-20 11:07:07

C#共享內(nèi)存

2009-08-26 13:48:31

C#打印條碼

2009-08-19 10:46:48

C#操作Word表格

2009-08-27 13:30:11

C# interfac

2009-08-19 14:12:23

C#操作注冊表

2009-08-19 16:30:55

C#操作Access數(shù)

2009-08-18 17:42:12

C#操作符重載

2009-08-17 13:34:02

C#異步操作

2009-08-17 17:49:20

C# 枚舉

2009-09-09 13:57:28

C# XML解析

2009-08-27 17:59:56

C#接口定義
點贊
收藏

51CTO技術(shù)棧公眾號