前几天,因为学期快要结束了,而老师要求的作业都应要上交。由于这门课程是关于电子病历的,内容就是用word 做模板,医生在模板中输入病人的病症,输入完毕后就会把输入的内容存放到数据库。而不是将整个word保存入数据库。当需要打印时就会把数据从数据库中选择出来自动放到模板中的原来位置 而形成完整的电子病历。完成这个工作用的类是office中的word引用,是一个COM类库。
注意:我用模板是一个经过处理的word文档,用书签来进行定位。想了解更多的朋友可能发E_Mail给我!
下面就放一些我实现用到的源代码:
用到的引用:
内容代码:
namespace blmb
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
public partial class Form1 : Form
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
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()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
InitializeComponent();
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 打开文档
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void 打开openToolStripMenuItem1_Click(object sender, EventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
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;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//////////////////////////////////////////////////////////////////////////////////////////////
/// <summary>
/// 保存到数据库
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
string temp_path = pathfile + @"temp.doc";
try
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
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);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/**/////这是最关键的地方:对文档的所有书签进行便利匹配
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
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
MessageBox.Show("请输入病人信息!");
}
File.Delete(temp_path);
打开openToolStripMenuItem1_Click(sender, e);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 插入到数据库
/// </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">职业</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)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
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();
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private void button1_Click(object sender, EventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
if (textBox1.Text == "")
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
MessageBox.Show("病历号不可为空!");
}
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
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
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
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);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
foreach(Word.Bookmark BM in docword .Bookmarks) /**/////这是最关键的地方:对文档的所有书签进行便利匹配
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
switch(BM.Name.ToLower())
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
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
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
}
}
}
private void Form1_Load(object sender, EventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
}
}
}