溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

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

發(fā)布時(shí)間:2021-12-03 09:45:39 來(lái)源:億速云 閱讀:182 作者:iii 欄目:編程語(yǔ)言

本篇內(nèi)容主要講解“C#操作Word應(yīng)用實(shí)例分析”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“C#操作Word應(yīng)用實(shí)例分析”吧!

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

注意:我用模板是一個(gè)經(jīng)過(guò)處理的word文檔,用書(shū)簽來(lái)進(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>  /// 打開(kāi)文檔  ,C#操作Word實(shí)際應(yīng)用實(shí)例/// </summary>  /// <param name="sender"></param>  /// <param name="e"></param>  private void 打開(kāi)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ù)庫(kù) ,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)鍵的地方:對(duì)文檔的所有書(shū)簽進(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 = "病案號(hào)";  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("請(qǐng)輸入病人信息!");  }  File.Delete(temp_path);  打開(kāi)openToolStripMenuItem1_Click(sender, e);  }  /**//// <summary>  /// 插入到數(shù)據(jù)庫(kù),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">病案號(hào)</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("病歷號(hào)不可為空!");  }  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)鍵的地方:對(duì)文檔的所有書(shū)簽進(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 "病案號(hào)":     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í)例}  }

到此,相信大家對(duì)“C#操作Word應(yīng)用實(shí)例分析”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢(xún),關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI