C#操作Word应用实例分析
发表于:2024-09-22 作者:千家信息网编辑
千家信息网最后更新 2024年09月22日,本篇内容主要讲解"C#操作Word应用实例分析",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"C#操作Word应用实例分析"吧!C#操作Word实际应用实例
千家信息网最后更新 2024年09月22日C#操作Word应用实例分析
本篇内容主要讲解"C#操作Word应用实例分析",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"C#操作Word应用实例分析"吧!
C#操作Word实际应用实例:课程是关于电子病历的,内容就是用word 做模板,医生在模板中输入病人的病症,输入完毕后就会把输入的内容存放到数据库。而不是将整个word保存入数据库。当需要打印时就会把数据从数据库中选择出来自动放到模板中的原来位置 而形成完整的电子病历。完成这个工作用的类是office中的word引用,是一个COM类库。
注意:我用模板是一个经过处理的word文档,用书签来进行定位。下面就放一些实现用到的源代码:
C#操作Word实际应用实例用到的引用:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using Word; using System.IO; using System.Reflection; using System.Data.OleDb;
C#操作Word实际应用实例内容代码:
namespace blmb ...{ public partial class Form1 : Form ...{ Word.Application appword = new Word.Application(); Word.Document docword = new Document(); string pathfile = System.AppDomain.CurrentDomain. SetupInformation.ApplicationBase;//应用程序的路径 object missing = System.Reflection.Missing.Value; public Form1() ...{ InitializeComponent(); } /**/////// 打开文档 ,C#操作Word实际应用实例/// /// /// private void 打开openToolStripMenuItem1_Click( object sender, EventArgs e) ...{ string path = pathfile + @"fill.doc"; string temp_path = pathfile + @"temp.doc"; File.Delete(temp_path); File.Copy(path, temp_path); webBrowser1.Navigate(temp_path); saveToolStripMenuItem.Enabled = true; } /**//// ////// 保存到数据库 ,C#操作Word实际应用实例 /// /// /// private void saveToolStripMenuItem_Click( object sender, EventArgs e) ...{ string temp_path = pathfile + @"temp.doc"; try ...{ appword.Visible = true; object missing = System.Reflection.Missing.Value; object Readonly = true; object isvisible = true; object filepath = (object)temp_path; docword = null; docword = appword.Documents.Open(ref filepath, ref missing, ref Readonly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isvisible, ref missing, ref missing, ref missing, ref missing); /**/////这是最关键的地方:对文档的所有书签进行便利匹配 object name_bm = "姓名"; string name = docword.Bookmarks. get_Item(ref name_bm).Range.Text.Replace(" a"," "); object age_bm = "年龄"; string age = docword.Bookmarks. get_Item(ref age_bm).Range.Text.Replace(" a", " "); object sex_bm = "性别"; string sex = docword.Bookmarks. get_Item(ref sex_bm).Range.Text.Replace(" a", " "); object address_bm = "家庭地址"; string address = docword.Bookmarks. get_Item(ref address_bm).Range.Text.Replace(" a", " "); object post_no_bm = "邮编"; string post_no = docword.Bookmarks. get_Item(ref post_no_bm).Range.Text.Replace(" a", " "); object job_bm = "职业"; string job = docword.Bookmarks. get_Item(ref job_bm).Range.Text.Replace(" a", " "); object host_bm = "既往史"; string host = docword.Bookmarks. get_Item(ref host_bm).Range.Text.Replace(" a", " "); object NO_bm = "病案号"; string NO = docword.Bookmarks.get_Item(ref NO_bm). Range.Text.Replace(" a", " "); insertData(name, age, sex, address, post_no, job, host, NO); docword.Close(ref missing, ref missing, ref missing); appword.Quit(ref missing, ref missing, ref missing); } catch ...{ MessageBox.Show("请输入病人信息!"); } File.Delete(temp_path); 打开openToolStripMenuItem1_Click(sender, e); } /**/////// 插入到数据库,C#操作Word实际应用实例 /// /// 姓名 /// 年龄 /// 性别 /// 住址 /// 邮编 /// 职业 /// 既往史 /// 病案号 private void insertData(string name,string age, string sex,string address,string post_no, string job_type,string host,string NO) ...{ string DB_path=pathfile+@"blmb.mdb"; string strCon = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + DB_path; OleDbConnection con = new OleDbConnection(strCon); OleDbCommand cmd = new OleDbCommand(); con.Open(); string insert_str = "insert into patient values ('"+name +"','"+age+"','"+sex+"','"+address+"','"+ post_no+"','"+job_type+"','"+host+"','"+NO+"')"; cmd.CommandText = insert_str; cmd.Connection = con; cmd.ExecuteNonQuery(); con.Close(); } private void button1_Click(object sender, EventArgs e) ...{ if (textBox1.Text == "") ...{ MessageBox.Show("病历号不可为空!"); } else ...{ string DB_path = pathfile + @"blmb.mdb"; string strCon = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + DB_path; OleDbConnection con = new OleDbConnection(strCon); OleDbCommand cmd = new OleDbCommand(); con.Open(); string insert_str = "select * from patient where num='"+textBox1.Text.Trim()+"'"; cmd.CommandText = insert_str; cmd.Connection = con; OleDbDataAdapter da = new OleDbDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds, "temp"); con.Close(); ds.WriteXml(textBox1.Text+".xml"); try ...{ string path = pathfile + @"fill.doc"; string temp_path = pathfile + textBox1.Text+".doc"; File.Delete(temp_path); File.Copy(path, temp_path); appword.Visible = true; object missing = System.Reflection.Missing.Value; object Readonly = false; object isvisible = true; object filepath = (object)temp_path; docword = null; docword = appword.Documents.Open(ref filepath, ref missing, ref Readonly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isvisible, ref missing, ref missing, ref missing, ref missing); foreach(Word.Bookmark BM in docword .Bookmarks) /**/////这是最关键的地方:对文档的所有书签进行便利匹配 ...{ switch(BM.Name.ToLower()) ...{ case "姓名": BM.Select(); BM.Range.Text=ds.Tables["temp"].Rows[0][0].ToString(); break; case "年龄": BM.Select(); BM.Range.Text = ds.Tables["temp"].Rows[0][1].ToString(); break; case "性别": BM.Select(); BM.Range.Text = ds.Tables["temp"].Rows[0][2].ToString(); break; case "家庭地址": BM.Select(); BM.Range.Text = ds.Tables["temp"].Rows[0][3].ToString(); break; case "邮编": BM.Select(); BM.Range.Text = ds.Tables["temp"].Rows[0][4].ToString(); break; case "职业": BM.Select(); BM.Range.Text = ds.Tables["temp"].Rows[0][5].ToString(); break; case "既往史": BM.Select(); BM.Range.Text = ds.Tables["temp"].Rows[0][6].ToString(); break; case "病案号": BM.Select(); BM.Range.Text = ds.Tables["temp"].Rows[0][7].ToString(); break; } } docword.Save(); docword.Close(ref missing,ref missing,ref missing); appword.Quit(ref missing ,ref missing ,ref missing); } catch ...{ } } } private void Form1_Load(object sender, EventArgs e) ...{ } //C#操作Word实际应用实例} }
到此,相信大家对"C#操作Word应用实例分析"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
应用
实例
实际
C#
数据
内容
数据库
文档
模板
输入
实例分析
分析
书签
姓名
年龄
性别
病历
病案
职业
邮编
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
财务软件开发的验收指标
对网络安全危害主要是
陌生聊天软件开发多少钱一个
乌托邦网络安全知识
软件开发中fr什么意思
在北京搞软件开发
实惠的网络安全防范
浙江企符号网络技术有限公司
金山文档数据库怎么开
sql 跨数据库 查询
网络安全设备的加固方案
sql 数据库如何增加列
南昌软件开发公司待遇怎么样
r710服务器网卡
软件开发在螺旋模型优点
oracle清空某些数据库
qt访问数据库的某一行
物流系统数据库
网络安全900字征文
网络安全教育动员部署会
皮书数据库网站
创建数据库提示错误
数据库修改默认值的语法错误
刺激战场年兽数据库
气象部门网络安全周
赴日软件开发学4个月
湖北人工智能软件开发需要多少钱
贴吧验证手机网络安全风险
四川应用软件开发哪家可靠
机器学习服务器配置报价