C#操作Word實(shí)際應(yīng)用實(shí)例淺析
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í)例用到的引用:
- 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實(shí)際應(yīng)用實(shí)例內(nèi)容代碼:
- 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;//應(yīng)用程序的路徑
- object missing = System.Reflection.Missing.Value;
- public Form1()
- ...{
- InitializeComponent();
- }
- /**//// <summary>
- /// 打開文檔 ,C#操作Word實(shí)際應(yīng)用實(shí)例
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- 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;
- }
- /**////
- /// <summary>
- /// 保存到數(shù)據(jù)庫 ,C#操作Word實(shí)際應(yīng)用實(shí)例
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- 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);
- /**/////這是最關(guān)鍵的地方:對文檔的所有書簽進(jìn)行便利匹配
- 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 = "職業(yè)";
- 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);
- }
- /**//// <summary>
- /// 插入到數(shù)據(jù)庫,C#操作Word實(shí)際應(yīng)用實(shí)例
- /// </summary>
- /// <param name="name">姓名</param>
- /// <param name="age">年齡</param>
- /// <param name="sex">性別</param>
- /// <param name="address">住址</param>
- /// <param name="post_no">郵編</param>
- /// <param name="job_type">職業(yè)</param>
- /// <param name="hosity">既往史</param>
- /// <param name="NO">病案號</param>
- 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)
- /**/////這是最關(guān)鍵的地方:對文檔的所有書簽進(jìn)行便利匹配
- ...{
- 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 "職業(yè)":
- 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實(shí)際應(yīng)用實(shí)例
- }
- }
C#操作Word實(shí)際應(yīng)用實(shí)例的基本情況就向你介紹了這里,希望對你了解和學(xué)習(xí)C#操作Word有所幫助。
【編輯推薦】