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

C#操作Word實(shí)際應(yīng)用實(shí)例淺析

開發(fā) 后端
C#操作Word實(shí)際應(yīng)用實(shí)例主要向你介紹了一個(gè)使用C#操作Word實(shí)現(xiàn)病例數(shù)據(jù)管理等的應(yīng)用實(shí)現(xiàn),希望對你開發(fā)C#操作Word方面有所幫助。

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

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

C#操作Word實(shí)際應(yīng)用實(shí)例用到的引用:

  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實(shí)際應(yīng)用實(shí)例內(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;//應(yīng)用程序的路徑  
  9. object missing = System.Reflection.Missing.Value;  
  10. public Form1()  
  11. ...{  
  12. InitializeComponent();  
  13. }  
  14. /**//// <summary>  
  15. /// 打開文檔  ,C#操作Word實(shí)際應(yīng)用實(shí)例
  16. /// </summary>  
  17. /// <param name="sender"></param>  
  18. /// <param name="e"></param>  
  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. /// <summary>  
  31. /// 保存到數(shù)據(jù)庫 ,C#操作Word實(shí)際應(yīng)用實(shí)例 
  32. /// </summary>  
  33. /// <param name="sender"></param>  
  34. /// <param name="e"></param>  
  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)鍵的地方:對文檔的所有書簽進(jì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. /**//// <summary>  
  90. /// 插入到數(shù)據(jù)庫,C#操作Word實(shí)際應(yīng)用實(shí)例  
  91. /// </summary>  
  92. /// <param name="name">姓名</param>  
  93. /// <param name="age">年齡</param>  
  94. /// <param name="sex">性別</param>  
  95. /// <param name="address">住址</param>  
  96. /// <param name="post_no">郵編</param>  
  97. /// <param name="job_type">職業(yè)</param>  
  98. /// <param name="hosity">既往史</param>  
  99. /// <param name="NO">病案號</param>  
  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)鍵的地方:對文檔的所有書簽進(jì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實(shí)際應(yīng)用實(shí)例
  210. }  

C#操作Word實(shí)際應(yīng)用實(shí)例的基本情況就向你介紹了這里,希望對你了解和學(xué)習(xí)C#操作Word有所幫助。

【編輯推薦】

  1. C#操作Word表的實(shí)例淺析
  2. C#操作Word表格的常見操作
  3. C#操作Word表格的彪悍實(shí)例
  4. C#操作Word實(shí)用實(shí)例淺析
  5. C#操作Word的一點(diǎn)認(rèn)識(shí)
責(zé)任編輯:仲衡 來源: CSDN博客
相關(guān)推薦

2009-08-19 09:42:52

C#操作Word書簽

2009-08-19 11:13:49

C#操作Word

2009-08-19 11:34:06

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-17 17:49:20

C# 枚舉

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-24 17:58:19

C# 泛型集合

2009-08-27 13:05:06

C#接口特點(diǎn)C#接口實(shí)例

2009-08-31 16:37:20

C#接口定義

2009-08-27 13:30:11

C# interfac

2009-08-25 17:02:20

C#串口操作

2009-09-01 17:50:23

C#截取字符串

2009-08-19 14:12:23

C#操作注冊表
點(diǎn)贊
收藏

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