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

創(chuàng)新互聯(lián)公司專注于橫縣企業(yè)網(wǎng)站建設(shè),成都響應(yīng)式網(wǎng)站建設(shè),購物商城網(wǎng)站建設(shè)。橫縣網(wǎng)站建設(shè)公司,為橫縣等地區(qū)提供建站服務(wù)。全流程按需求定制開發(fā),專業(yè)設(shè)計,全程項目跟蹤,創(chuàng)新互聯(lián)公司專業(yè)和態(tài)度為您提供的服務(wù)
C#操作Word實際應(yīng)用實例:課程是關(guān)于電子病歷的,內(nèi)容就是用word 做模板,醫(yī)生在模板中輸入病人的病癥,輸入完畢后就會把輸入的內(nèi)容存放到數(shù)據(jù)庫。而不是將整個word保存入數(shù)據(jù)庫。當需要打印時就會把數(shù)據(jù)從數(shù)據(jù)庫中選擇出來自動放到模板中的原來位置 而形成完整的電子病歷。完成這個工作用的類是office中的word引用,是一個COM類庫。
注意:我用模板是一個經(jīng)過處理的word文檔,用書簽來進行定位。下面就放一些實現(xiàn)用到的源代碼:
C#操作Word實際應(yīng)用實例用到的引用:
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實際應(yīng)用實例內(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(); } /**//// /// 打開文檔 ,C#操作Word實際應(yīng)用實例/// /// /// 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; } /**//// /// /// 保存到數(shù)據(jù)庫 ,C#操作Word實際應(yīng)用實例 /// /// /// 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)鍵的地方:對文檔的所有書簽進行便利匹配 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); } /**//// /// 插入到數(shù)據(jù)庫,C#操作Word實際應(yīng)用實例 /// /// 姓名 /// 年齡 /// 性別 /// 住址 /// 郵編 /// 職業(yè) /// 既往史 /// 病案號 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)鍵的地方:對文檔的所有書簽進行便利匹配 ...{ 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實際應(yīng)用實例} }到此,相信大家對“C#操作Word應(yīng)用實例分析”有了更深的了解,不妨來實際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進入相關(guān)頻道進行查詢,關(guān)注我們,繼續(xù)學習!
新聞名稱:C#操作Word應(yīng)用實例分析
文章鏈接:http://m.biofuelwatch.net/article/ispood.html


咨詢
建站咨詢
