C#读Word

private string ImportFromFile()    

{   

#region Word数据处理    

//创建一个数据链接    

try    

{   

#region 打开Word文档    

object FileName = GetOpenFileName();    

if (FileName==null ||FileName.ToString().Trim()=="") return null;    

object oMissing = System.Reflection.Missing.Value;    

ProgressForm ff = new ProgressForm("正在获取数据,请稍候…………");    

ff.Show();    

Microsoft.Office.Interop.Word._Application oWord = new Microsoft.Office.Interop.Word.ApplicationClass();;    

object oReadOnly = true;    

object oVisible = false;    

oWord.Visible = false;    

oWord.Documents.Open(ref FileName, ref oMissing, ref oReadOnly, ref oMissing, ref oMissing,    

ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,    

ref oVisible, ref oMissing, ref oMissing,ref oMissing,ref oMissing);   

#endregion   

 

#region 从Word表格取值    

DataTable vTable=null;    

DataRow vRow = null;    

DataRow vTempRow = null;    

int pos = 0;    

string vTemp = "";   

 

#region 基本资料    

string oValue = oWord.ActiveDocument.Tables[1].Cell(3,2).Range.Text;//姓名    

oValue = oValue.Remove(oValue.Length-1,1).Trim();    

ds.Tables[0].Rows[0]["HRZPJL_RYBM"]=oValue;    

oValue = oWord.ActiveDocument.Tables[1].Cell(3,4).Range.Text;//出生日期    

oValue = oValue.Remove(oValue.Length-1,1).Trim();    

ds.Tables[0].Rows[0]["HRZPJL_CSRQ"]=oValue.Replace(".","");    

//性别    

oValue = oWord.ActiveDocument.Tables[1].Cell(41,1).Range.Text;    

oValue = oValue.Remove(oValue.Length-1,1).Trim();    

vRow = GSControlFunction.GetValue("select * from codeitems where name='"+oValue+"' and SetID='A020'");    

if(vRow!=null)    

{    

ds.Tables[0].Rows[0]["HRZPJL_XB"]=vRow["Code"].ToString();    

ds.Tables[0].Rows[0]["HRZPJL_XB_Name"]=oValue;    

}    

//民族    

oValue = oWord.ActiveDocument.Tables[1].Cell(4,4).Range.Text;    

oValue = oValue.Remove(oValue.Length-1,1).Trim();    

vRow = GSControlFunction.GetValue("select * from codeitems where name='"+oValue+"' and SetID='A022'");    

if(vRow!=null)    

{    

ds.Tables[0].Rows[0]["HRZPJL_MZ"]=vRow["Code"].ToString();    

ds.Tables[0].Rows[0]["HRZPJL_MZ_Name"]=oValue;    

}    

//血型    

oValue = oWord.ActiveDocument.Tables[1].Cell(41,3).Range.Text;    

oValue = oValue.Remove(oValue.Length-1,1).Trim();    

vRow = GSControlFunction.GetValue("select * from codeitems where name='"+oValue+"' and SetID='H024'");    

if(vRow!=null)    

{    

ds.Tables[0].Rows[0]["HRZPJL_XX"]=vRow["Code"].ToString();    

ds.Tables[0].Rows[0]["HRZPJL_XX_Name"]=oValue;    

}    

//健康状况    

oValue =oWord.ActiveDocument.Tables[1].Cell(41,4).Range.Text;    

oValue = oValue.Remove(oValue.Length-1,1).Trim();    

vRow = GSControlFunction.GetValue("select * from codeitems where name='"+oValue+"' and SetID='H044'");    

if(vRow!=null)    

{    

ds.Tables[0].Rows[0]["HRZPJL_JKZK"]=vRow["Code"].ToString();    

ds.Tables[0].Rows[0]["HRZPJL_JKZK_Name"]=oValue;    

}    

//婚姻状况    

oValue = oWord.ActiveDocument.Tables[1].Cell(42,1).Range.Text;    

oValue = oValue.Remove(oValue.Length-1,1).Trim();    

vRow = GSControlFunction.GetValue("select * from codeitems where name='"+oValue+"' and SetID='A021'");    

if(vRow!=null)    

{    

ds.Tables[0].Rows[0]["HRZPJL_HYZK"]=vRow["Code"].ToString();    

ds.Tables[0].Rows[0]["HRZPJL_HYZK_Name"]=oValue;    

}    

//家庭电话    

oValue = oWord.ActiveDocument.Tables[1].Cell(6,4).Range.Text;    

oValue = oValue.Remove(oValue.Length-1,1).Trim();    

ds.Tables[0].Rows[0]["HRZPJL_JTDH"]=oValue;   

#endregion   

 

 

#endregion   

 

#region 杀死Word进程    

KillProcess("WinWord");    

ff.Close();   

#endregion    

  

return "";    

}    

catch(Exception e)    

{    

MessageBox.Show("写入数据出错: \n\r" + e.Message);    

KillProcess("WinWord");    

return null;    

}   

#endregion    

}  

你可能感兴趣的:(word)