C#操作Word实际应用实例浅析

开发 后端
C#操作Word实际应用实例主要向你介绍了一个使用C#操作Word实现病例数据管理等的应用实现,希望对你开发C#操作Word方面有所帮助。

C#操作Word实际应用实例:课程是关于电子病历的,内容就是用word 做模板,医生在模板中输入病人的病症,输入完毕后就会把输入的内容存放到数据库。而不是将整个word保存入数据库。当需要打印时就会把数据从数据库中选择出来自动放到模板中的原来位置 而形成完整的电子病历。完成这个工作用的类是office中的word引用,是一个COM类库。

注意:我用模板是一个经过处理的word文档,用书签来进行定位。下面就放一些实现用到的源代码:

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实际应用实例内容代码:

  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. /**//// <summary>  
  15. /// 打开文档  ,C#操作Word实际应用实例
  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. /// 保存到数据库 ,C#操作Word实际应用实例 
  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. /**/////这是最关键的地方:对文档的所有书签进行便利匹配  
  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 = "职业";  
  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. /// 插入到数据库,C#操作Word实际应用实例  
  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">职业</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.  /**/////这是最关键的地方:对文档的所有书签进行便利匹配  
  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 "职业":  
  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有所帮助。

【编辑推荐】

  1. C#操作Word表的实例浅析
  2. C#操作Word表格的常见操作
  3. C#操作Word表格的彪悍实例
  4. C#操作Word实用实例浅析
  5. C#操作Word的一点认识
责任编辑:仲衡 来源: CSDN博客
相关推荐

2009-08-19 11:34:06

C#操作Word

2009-08-19 11:13:49

C#操作Word

2009-08-19 09:42:52

C#操作Word书签

2009-08-19 10:25:14

C#操作Word

2009-09-01 13:13:28

C#打开Word文档

2009-08-28 17:34:14

读取word文档

2009-08-18 13:49:21

C# 操作Excel

2009-08-17 17:49:20

C# 枚举

2009-08-31 18:38:59

C#写文件

2009-08-18 16:04:12

C# 操作Excel

2009-08-20 11:07:07

C#共享内存

2009-08-26 13:48:31

C#打印条码

2009-08-19 10:46:48

C#操作Word表格

2009-08-27 13:05:06

C#接口特点C#接口实例

2009-08-31 16:37:20

C#接口定义

2009-08-24 17:58:19

C# 泛型集合

2009-09-01 17:50:23

C#截取字符串

2009-08-25 17:02:20

C#串口操作

2009-08-27 13:30:11

C# interfac

2009-08-18 17:42:12

C#操作符重载
点赞
收藏

51CTO技术栈公众号