av激情亚洲男人的天堂国语,日韩欧美精品一中文字幕,无码av一区二区三区无码,国产又色又爽又刺激的a片,国产又色又爽又刺激的a片

C#操作Word實際應用實例淺析

C#操作Word實際應用實例:課程是關(guān)于電子病歷的,內(nèi)容就是用word 做模板,醫(yī)生在模板中輸入病人的病癥,輸入完畢后就會把輸入的內(nèi)容存放到數(shù)據(jù)庫。而不是將整個word保存入數(shù)據(jù)庫。當需要打印時就會把數(shù)據(jù)從數(shù)據(jù)庫中選擇出來自動放到模板中的原來位置 而形成完整的電子病歷。完成這個工作用的類是office中的word引用,是一個COM類庫。

創(chuàng)新互聯(lián)建站自2013年起,公司以網(wǎng)站設計、網(wǎng)站建設、系統(tǒng)開發(fā)、網(wǎng)絡推廣、文化傳媒、企業(yè)宣傳、平面廣告設計等為主要業(yè)務,適用行業(yè)近百種。服務企業(yè)客戶上千余家,涉及國內(nèi)多個省份客戶。擁有多年網(wǎng)站建設開發(fā)經(jīng)驗。為企業(yè)提供專業(yè)的網(wǎng)站建設、創(chuàng)意設計、宣傳推廣等服務。 通過專業(yè)的設計、獨特的風格,為不同客戶提供各種風格的特色服務。

注意:我用模板是一個經(jīng)過處理的word文檔,用書簽來進行定位。下面就放一些實現(xiàn)用到的源代碼:

C#操作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 Word;  
  9. using System.IO;  
  10. using System.Reflection;  
  11. using System.Data.OleDb; 

C#操作Word實際應用實例內(nèi)容代碼:

 
 
 
  1. namespace blmb  
  2. ...{  
  3. public partial class Form1 : Form  
  4. ...{  
  5. Word.Application appword = new Word.Application();  
  6. Word.Document docword = new Document();  
  7. string pathfile = System.AppDomain.CurrentDomain.  
  8. SetupInformation.ApplicationBase;//應用程序的路徑  
  9. object missing = System.Reflection.Missing.Value;  
  10. public Form1()  
  11. ...{  
  12. InitializeComponent();  
  13. }  
  14. /**////   
  15. /// 打開文檔  ,C#操作Word實際應用實例
  16. ///   
  17. ///   
  18. ///   
  19. private void 打開openToolStripMenuItem1_Click(  
  20. object sender, EventArgs e)  
  21. ...{  
  22. string path = pathfile + @"fill.doc";  
  23. string temp_path = pathfile + @"temp.doc";  
  24. File.Delete(temp_path);  
  25. File.Copy(path, temp_path);  
  26. webBrowser1.Navigate(temp_path);  
  27. saveToolStripMenuItem.Enabled = true;  
  28. }  
  29. /**////  
  30. ///   
  31. /// 保存到數(shù)據(jù)庫 ,C#操作Word實際應用實例 
  32. ///   
  33. ///   
  34. ///   
  35. private void saveToolStripMenuItem_Click(  
  36. object sender, EventArgs e)  
  37. ...{  
  38. string temp_path = pathfile + @"temp.doc";  
  39. try 
  40. ...{  
  41. appword.Visible = true;  
  42. object missing = System.Reflection.Missing.Value;  
  43. object Readonly = true;  
  44. object isvisible = true;  
  45. object filepath = (object)temp_path;  
  46. docword = null;  
  47. docword = appword.Documents.Open(ref filepath,  
  48.  ref missing, ref Readonly, ref missing,   
  49. ref missing, ref missing, ref missing,   
  50. ref missing, ref missing, ref missing,   
  51. ref missing, ref isvisible, ref missing,  
  52.  ref missing, ref missing, ref missing);  
  53. /**/////這是最關(guān)鍵的地方:對文檔的所有書簽進行便利匹配  
  54. object name_bm = "姓名";  
  55. string name = docword.Bookmarks.  
  56. get_Item(ref name_bm).Range.Text.Replace(" a"," ");  
  57. object age_bm = "年齡";  
  58. string age = docword.Bookmarks.  
  59. get_Item(ref age_bm).Range.Text.Replace(" a", " ");  
  60. object sex_bm = "性別";  
  61. string sex = docword.Bookmarks.  
  62. get_Item(ref sex_bm).Range.Text.Replace(" a", " ");  
  63. object address_bm = "家庭地址";  
  64. string address = docword.Bookmarks.  
  65. get_Item(ref address_bm).Range.Text.Replace(" a", " ");  
  66. object post_no_bm = "郵編";  
  67. string post_no = docword.Bookmarks.  
  68. get_Item(ref post_no_bm).Range.Text.Replace(" a", " ");  
  69. object job_bm = "職業(yè)";  
  70. string job = docword.Bookmarks.  
  71. get_Item(ref job_bm).Range.Text.Replace(" a", " ");  
  72. object host_bm = "既往史";  
  73. string host = docword.Bookmarks.  
  74. get_Item(ref host_bm).Range.Text.Replace(" a", " ");  
  75. object NO_bm = "病案號";  
  76. string NO = docword.Bookmarks.get_Item(ref NO_bm).  
  77. Range.Text.Replace(" a", " ");  
  78. insertData(name, age, sex, address, post_no, job, host, NO);  
  79. docword.Close(ref missing, ref missing, ref missing);  
  80. appword.Quit(ref missing, ref missing, ref missing);  
  81. }  
  82. catch 
  83. ...{  
  84. MessageBox.Show("請輸入病人信息!");  
  85. }  
  86. File.Delete(temp_path);  
  87. 打開openToolStripMenuItem1_Click(sender, e);  
  88. }  
  89. /**////   
  90. /// 插入到數(shù)據(jù)庫,C#操作Word實際應用實例  
  91. ///   
  92. /// 姓名  
  93. /// 年齡  
  94. /// 性別  
  95. /// 住址  
  96. /// 郵編  
  97. /// 職業(yè)  
  98. /// 既往史  
  99. /// 病案號  
  100. private void insertData(string name,string age,  
  101. string sex,string address,string post_no,  
  102. string job_type,string host,string NO)  
  103. ...{  
  104. string DB_path=pathfile+@"blmb.mdb";  
  105. string strCon = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + DB_path;  
  106. OleDbConnection con = new OleDbConnection(strCon);  
  107. OleDbCommand cmd = new OleDbCommand();  
  108. con.Open();  
  109. string insert_str = "insert into patient values ('"+name  
  110. +"','"+age+"','"+sex+"','"+address+"','"+  
  111. post_no+"','"+job_type+"','"+host+"','"+NO+"')";  
  112. cmd.CommandText = insert_str;  
  113. cmd.Connection = con;  
  114. cmd.ExecuteNonQuery();  
  115. con.Close();  
  116. }  
  117.  
  118. private void button1_Click(object sender, EventArgs e)  
  119. ...{  
  120. if (textBox1.Text == "")  
  121. ...{  
  122. MessageBox.Show("病歷號不可為空!");  
  123. }  
  124. else 
  125. ...{  
  126. string DB_path = pathfile + @"blmb.mdb";  
  127. string strCon = @"Provider=Microsoft.Jet.OLEDB.4.0;  
  128. Data Source=" + DB_path;  
  129. OleDbConnection con = new OleDbConnection(strCon);  
  130. OleDbCommand cmd = new OleDbCommand();  
  131. con.Open();  
  132. string insert_str = "select * from patient  
  133.  where num='"+textBox1.Text.Trim()+"'";  
  134. cmd.CommandText = insert_str;  
  135. cmd.Connection = con;  
  136. OleDbDataAdapter da = new OleDbDataAdapter(cmd);  
  137. DataSet ds = new DataSet();  
  138. da.Fill(ds, "temp");  
  139. con.Close();  
  140. ds.WriteXml(textBox1.Text+".xml");  
  141.    try 
  142.    ...{  
  143.    string path = pathfile + @"fill.doc";  
  144.    string temp_path = pathfile + textBox1.Text+".doc";  
  145.    File.Delete(temp_path);  
  146.    File.Copy(path, temp_path);  
  147.    appword.Visible = true;  
  148.    object missing = System.Reflection.Missing.Value;  
  149.    object Readonly = false;  
  150.    object isvisible = true;  
  151.    object filepath = (object)temp_path;  
  152.    docword = null;  
  153.    docword = appword.Documents.Open(ref filepath,  
  154.  ref missing, ref Readonly, ref missing, ref missing,  
  155.  ref missing, ref missing, ref missing, ref missing,  
  156.  ref missing, ref missing, ref isvisible, ref missing,  
  157.  ref missing, ref missing, ref missing);  
  158.    foreach(Word.Bookmark BM in docword .Bookmarks)  
  159.  /**/////這是最關(guān)鍵的地方:對文檔的所有書簽進行便利匹配  
  160. ...{  
  161.  switch(BM.Name.ToLower())  
  162.  ...{  
  163.   case "姓名":   
  164.    BM.Select();  
  165.    BM.Range.Text=ds.Tables["temp"].Rows[0][0].ToString();  
  166.    break;  
  167.   case "年齡":  
  168.    BM.Select();  
  169.    BM.Range.Text = ds.Tables["temp"].Rows[0][1].ToString();  
  170.    break;  
  171.    case "性別":  
  172.    BM.Select();  
  173.    BM.Range.Text = ds.Tables["temp"].Rows[0][2].ToString();  
  174.    break;  
  175.    case "家庭地址":  
  176.    BM.Select();  
  177.    BM.Range.Text = ds.Tables["temp"].Rows[0][3].ToString();  
  178.    break;  
  179.    case "郵編":  
  180.    BM.Select();  
  181.    BM.Range.Text = ds.Tables["temp"].Rows[0][4].ToString();  
  182.    break;  
  183.    case "職業(yè)":  
  184.    BM.Select();  
  185.    BM.Range.Text = ds.Tables["temp"].Rows[0][5].ToString();  
  186.    break;  
  187.    case "既往史":  
  188.    BM.Select();  
  189.    BM.Range.Text = ds.Tables["temp"].Rows[0][6].ToString();  
  190.    break;  
  191.    case "病案號":  
  192.    BM.Select();  
  193.    BM.Range.Text = ds.Tables["temp"].Rows[0][7].ToString();  
  194.    break;  
  195.  }   
  196. }  
  197.    docword.Save();  
  198.    docword.Close(ref missing,ref missing,ref missing);  
  199.    appword.Quit(ref missing ,ref missing ,ref missing);  
  200.    }  
  201.    catch 
  202.    ...{  
  203.    }  
  204. }  
  205. }  
  206. private void Form1_Load(object sender, EventArgs e)  
  207. ...{  
  208.  
  209. }  //C#操作Word實際應用實例
  210. }  

C#操作Word實際應用實例的基本情況就向你介紹了這里,希望對你了解和學習C#操作Word有所幫助。


本文名稱:C#操作Word實際應用實例淺析
網(wǎng)站鏈接:http://uogjgqi.cn/article/cdjhojg.html
掃二維碼與項目經(jīng)理溝通

我們在微信上24小時期待你的聲音

解答本文疑問/技術(shù)咨詢/運營咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流