C# 操作World生成报告

直接上代码

using System;
using System.IO;
using System.Data;
using Word = Microsoft.Office.Interop.Word;

namespace CreateWord
{
	/// <summary>
	/// Class1 的摘要说明。
	/// </summary>
	public class CreateWord
	{
		public CreateWord()
		{
			//
			// TODO: 在此处添加构造函数逻辑
			//
		}

		public static DataSet ds = new DataSet();//DateSet数据集
		public static string Mem_ID ="";//病案号
		public static string OrderNO = "";//订单号
		public static string CheckOrder = "";//体检订单
		public static string docPath = "";//文档路径
		public static string Name="";//体检人姓名


		#region 绘制所表格
		public static bool CreateWordDoc()
		{
			
			string SignDoctor = "";//检验的医生签字

			int Row=0;//需要绘制表的行
			int Column=0;//需要绘制的

			string PatientName="";
			string PatientSex="";
					
			
			object what = Microsoft.Office.Interop.Word.WdGoToItem.wdGoToLine;//定位到文档最后一行
			object which = Microsoft.Office.Interop.Word.WdGoToDirection.wdGoToLast;//定位到文档最后一行
			object count = 99999999;
			
			object Nothing = System.Reflection.Missing.Value;
			object strFalse = false;//设置False
			object strTrue = true;//设置true
			object strPwd = "HLH";//设置文档密码
			
			object Template = System.Windows.Forms.Application.StartupPath + @"\dot.doc";

			Word.Application appWord = new Word.ApplicationClass();//创建一个Word应用程序对象
			Word.Document docWord = appWord.Documents.Add(ref Template, ref Nothing, ref Nothing, ref Nothing);//创建一个Word文档对象
			


			#region 生成表格

			
			foreach(DataRow PatientInfo in ds.Tables["Patient"].Rows)
			{
				PatientName = PatientInfo["Name"].ToString().Trim();
			
				if(PatientInfo["Sex"].ToString().Trim() =="男")
				{
					PatientSex="先生";
				}
				else
				{
					PatientSex="女士";
				}
			}
			

			appWord.Selection.GoTo(ref what, ref which, ref count, ref Nothing); //定位到文档最后一行
			appWord.ActiveWindow.ActivePane.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekMainDocument;//换页
			object pageBreak = Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak;//换页
			appWord.Selection.InsertBreak(ref pageBreak);//换页

			foreach(DataRow Code in ds.Tables["Stage_Type"].Rows)
			{
				string TypeCode = Code["Code"].ToString();//取出当前检查类型的Code
				if(TypeCode=="8")//如果等于8就是体格检查
				{
					Column = 4;//如果是8体格检查那么就可以定下需要生成表格的列数了为4列
					foreach(DataRow Type_Code in ds.Tables["Stage"].Select("Type_Code="+TypeCode))
					{
						string StageCode = Type_Code["Stage_Code"].ToString();//循环取出当前Type_Code等于8也就是体格检查的StageCode

						foreach(DataRow Stage_Code in ds.Tables["Item_Result"].Select("Stage_Code="+StageCode))//循环检验结果里面等于当前StageCode的检验项
						{
							if((ds.Tables["Item_Result"].Select("Stage_Code="+StageCode).Length) % 2 != 0)//要在原有的行数基础上加1行留着写标题头的1行,如果当前匹配的行数不能对2整除就加1在整除
							{
								Row = ((ds.Tables["Item_Result"].Select("Stage_Code="+StageCode).Length + 1) / 2) + 1;//因为是4列所以表里面两条数据占用1行并且不要出现单数行
							}
							else//能被2整除是双数行
							{
								Row = (ds.Tables["Item_Result"].Select("Stage_Code="+StageCode).Length / 2) + 1;
							}

							//绘制表格所需要的行数就是能匹配当前StageCode检验项的行数(长度)有了行列就可以绘制表格了
							appWord.Selection.TypeText("【"+Type_Code["Name"].ToString()+"】");
							Word.Table tabTitle = docWord.Tables.Add(appWord.Selection.Range,Row,Column,ref Nothing, ref Nothing);
							tabTitle.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;//表格边框
							tabTitle.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;//设置表格边框
							tabTitle.Borders.OutsideColor = Word.WdColor.wdColorPaleBlue;//表格边框颜色
							tabTitle.Borders.InsideColor = Word.WdColor.wdColorPaleBlue;//表格边框颜色
							tabTitle.Rows[1].Range.Shading.ForegroundPatternColor = Word.WdColor.wdColorYellow;//设置第一行的单元格背景色为黄色
							appWord.Selection.GoTo(ref what, ref which, ref count, ref Nothing); //定位到文档最后一行
							appWord.Selection.PageSetup.LeftMargin = appWord.CentimetersToPoints((float)3.49);//设置标尺
							appWord.Selection.ParagraphFormat.LineSpacing = 15f;//设置文档的行间距
							
							tabTitle.Columns[1].Width = 105;
							tabTitle.Columns[2].Width = 105;
							tabTitle.Columns[3].Width = 104;
							tabTitle.Columns[4].Width = 104;
							//向表格单元格添加值
							tabTitle.Cell(1,1).Range.Text = "项目名称";//设置第一行第一列单元格内容
							tabTitle.Cell(1,2).Range.Text = "检查结果";//设置第一行第二列单元格内容
							tabTitle.Cell(1,3).Range.Text = "项目名称";//设置第一行第三列单元格内容
							tabTitle.Cell(1,4).Range.Text = "检查结果";//设置第一行第四列单元格内容

							int tabRow=2;//从第二列开始写单元格头
							int tabColumn=1;//从第1列开始
							foreach(DataRow Result in ds.Tables["Item_Result"].Select("Stage_Code="+StageCode))
							{
								if(tabColumn > 4)//因为知道体格检查类型表格列为4如果列大4就换行 在从第1列开始
								{
									tabRow++;
									tabColumn=1;
								}
								tabTitle.Cell(tabRow,tabColumn).Range.Text = Result["Name"].ToString().Trim();
								tabColumn++;
								tabTitle.Cell(tabRow,tabColumn).Range.Text = Result["Result"].ToString().Trim();
								tabColumn++;
							}

							//添加科室小结
							if(ds.Tables["Sub_total"].Select("Stage_Code="+StageCode).Length > 0)//判断有小结的就添加小结
							{
								appWord.Selection.Font.Bold = 5;//加粗
								appWord.Selection.TypeText("科室小结"+"
");
								appWord.Selection.Font.Bold = 0;//取消加粗
								
								int SummaryNo = 0;//小结数量
								foreach(DataRow Summary in ds.Tables["Sub_total"].Select("Stage_Code="+StageCode))
								{
									SummaryNo++;
									appWord.Selection.ParagraphFormat.IndentCharWidth(2);//增加缩进
									appWord.Selection.TypeText(SummaryNo.ToString()+"、"+Summary["Impression"].ToString()+"
");
									appWord.Selection.ParagraphFormat.IndentCharWidth(-2);//减少缩进
								}

							}

							//添加医生签字
							foreach(DataRow Sign in ds.Tables["Input_Stage_Record"].Select("Stage_Code="+StageCode))
							{
								appWord.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;//右对齐
								appWord.Selection.TypeText("医师:"+Sign["doctor_name"].ToString()+"
");
								appWord.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphJustify;//两端齐
							}
							break;
						}
						
					}
				}
				else if(TypeCode=="9")//是医技检查
				{
					Column = 2;//如果是9医技检查那么就可以定下需要生成表格的列数了为2列
					foreach(DataRow Type_Code in ds.Tables["Stage"].Select("Type_Code="+TypeCode))
					{
						string StageCode = Type_Code["Stage_Code"].ToString();//循环取出当前Type_Code等于8也就是体格检查的StageCode

						foreach(DataRow Stage_Code in ds.Tables["Item_Result"].Select("Stage_Code="+StageCode))//循环检验结果里面等于当前StageCode的检验项
						{
							
							Row = ds.Tables["Item_Result"].Select("Stage_Code="+StageCode).Length + 1;//因为是2列所以
						
							//绘制表格所需要的行数就是能匹配当前StageCode检验项的行数(长度)有了行列就可以绘制表格了
							appWord.Selection.TypeText("【"+Type_Code["Name"].ToString()+"】");
							Word.Table tabTitle = docWord.Tables.Add(appWord.Selection.Range,Row,Column,ref Nothing, ref Nothing);
							tabTitle.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;//表格边框
							tabTitle.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;//设置表格边框
							tabTitle.Borders.OutsideColor = Word.WdColor.wdColorPaleBlue;//表格边框颜色
							tabTitle.Borders.InsideColor = Word.WdColor.wdColorPaleBlue;//表格边框颜色
							tabTitle.Rows[1].Range.Shading.ForegroundPatternColor = Word.WdColor.wdColorYellow;//设置第一行的单元格背景色为黄色
							appWord.Selection.GoTo(ref what, ref which, ref count, ref Nothing); //定位到文档最后一行
							appWord.Selection.PageSetup.LeftMargin = appWord.CentimetersToPoints((float)3.49);//设置标尺
							appWord.Selection.ParagraphFormat.LineSpacing = 15f;//设置文档的行间距

							//向表格单元格添加值
							tabTitle.Cell(1,1).Range.Text = "项目名称";//设置第一行第一列单元格内容
							tabTitle.Cell(1,2).Range.Text = "检查结果";//设置第一行第二列单元格内容
							
							int tabRow=2;//从第二列开始写单元格头
							int tabColumn=1;//从第1列开始
							foreach(DataRow Result in ds.Tables["Item_Result"].Select("Stage_Code="+StageCode))
							{
								if(tabColumn > 4)//因为知道体格检查类型表格列为4如果列大4就换行 在从第1列开始
								{
									tabRow++;
									tabColumn=1;
								}
								tabTitle.Cell(tabRow,tabColumn).Range.Text = Result["Name"].ToString().Trim();
								tabColumn++;
								tabTitle.Cell(tabRow,tabColumn).Range.Text = Result["Result"].ToString().Trim();
								tabColumn++;
							}

							//添加科室小结
							if(ds.Tables["Sub_total"].Select("Stage_Code="+StageCode).Length > 0)//判断有小结的就添加小结
							{
								appWord.Selection.Font.Bold = 5;//加粗
								appWord.Selection.TypeText("科室小结"+"
");
								appWord.Selection.Font.Bold = 0;//取消加粗
								
								int SummaryNo = 0;//小结数量
								foreach(DataRow Summary in ds.Tables["Sub_total"].Select("Stage_Code="+StageCode))
								{
									SummaryNo++;
									appWord.Selection.ParagraphFormat.IndentCharWidth(2);//增加缩进
									appWord.Selection.TypeText(SummaryNo.ToString()+"、"+Summary["Impression"].ToString()+"
");
									appWord.Selection.ParagraphFormat.IndentCharWidth(-2);//减少缩进
								}

							}

							//添加医生签字
							foreach(DataRow Sign in ds.Tables["Input_Stage_Record"].Select("Stage_Code="+StageCode))
							{
								appWord.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;//右对齐
								appWord.Selection.TypeText("医师:"+Sign["doctor_name"].ToString()+"
");
								appWord.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphJustify;//两端齐
							}
							break;
						}
					}
				}
				else if(TypeCode=="10")//检验类型
				{
					Column = 6;//如果是9医技检查那么就可以定下需要生成表格的列数了为2列
					foreach(DataRow Type_Code in ds.Tables["Stage"].Select("Type_Code="+TypeCode))
					{
						string StageCode = Type_Code["Stage_Code"].ToString();//循环取出当前Type_Code等于8也就是体格检查的StageCode

						foreach(DataRow Stage_Code in ds.Tables["Lab_Result"].Select("Stage_Code="+StageCode))//循环检验结果里面等于当前StageCode的检验项
						{
							
							Row = ds.Tables["Lab_Result"].Select("Stage_Code="+StageCode).Length + 1;//因为是2列所以
						
							//绘制表格所需要的行数就是能匹配当前StageCode检验项的行数(长度)有了行列就可以绘制表格了
							appWord.Selection.TypeText("【"+Type_Code["Name"].ToString()+"】");
							Word.Table tabTitle = docWord.Tables.Add(appWord.Selection.Range,Row,Column,ref Nothing, ref Nothing);
							tabTitle.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;//表格边框
							tabTitle.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;//设置表格边框
							tabTitle.Borders.OutsideColor = Word.WdColor.wdColorPaleBlue;//表格边框颜色
							tabTitle.Borders.InsideColor = Word.WdColor.wdColorPaleBlue;//表格边框颜色
							tabTitle.Rows[1].Range.Shading.ForegroundPatternColor = Word.WdColor.wdColorYellow;//设置第一行的单元格背景色为黄色
							appWord.Selection.GoTo(ref what, ref which, ref count, ref Nothing); //定位到文档最后一行
							appWord.Selection.PageSetup.LeftMargin = appWord.CentimetersToPoints((float)3.49);//设置标尺
							appWord.Selection.ParagraphFormat.LineSpacing = 15f;//设置文档的行间距
							//设置第一列表格的宽度
							tabTitle.Columns[1].Width = 40; 
							tabTitle.Columns[2].Width = 130; 
							tabTitle.Columns[6].Width = 40;
						
							//向表格单元格添加值
							tabTitle.Cell(1,1).Range.Text = "序号";//设置第一行第一列单元格内容
							tabTitle.Cell(1,2).Range.Text = "项目名称";//设置第一行第一列单元格内容
							tabTitle.Cell(1,3).Range.Text = "检查结果";//设置第一行第二列单元格内容
							tabTitle.Cell(1,4).Range.Text = "单位";//设置第一行第三列单元格内容
							tabTitle.Cell(1,5).Range.Text = "参考范围";//设置第一行第四列单元格内容
							tabTitle.Cell(1,6).Range.Text = "标志";//设置第一行第五列单元格内容
							
							int tabRow=2;//从第二列开始写单元格头
							int tabColumn=1;//从第1列开始
							foreach(DataRow Result in ds.Tables["Lab_Result"].Select("Stage_Code="+StageCode))
							{
								if(tabColumn > 6)//因为知道体格检查类型表格列为4如果列大4就换行 在从第1列开始
								{
									tabRow++;
									tabColumn=1;
								}
								tabTitle.Cell(tabRow,tabColumn).Range.Text = (tabRow-1).ToString().Trim();
								tabColumn++;
								tabTitle.Cell(tabRow,tabColumn).Range.Text = Result["Name"].ToString().Trim();
								tabColumn++;
								tabTitle.Cell(tabRow,tabColumn).Range.Text = Result["Result"].ToString().Trim();
								tabColumn++;
								tabTitle.Cell(tabRow,tabColumn).Range.Text = Result["Unit"].ToString().Trim();
								tabColumn++;
								tabTitle.Cell(tabRow,tabColumn).Range.Text = Result["NorMal_Range"].ToString().Trim();
								tabColumn++;
								tabTitle.Cell(tabRow,tabColumn).Range.Text = Result["Flag"].ToString().Trim();
								tabColumn++;
								SignDoctor = Result["Doctor_name"].ToString().Trim();
							}
							//添加医生签字
							appWord.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;//右对齐
							appWord.Selection.TypeText("医师:"+SignDoctor+"
");
							appWord.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphJustify;//两端齐
							break;	
						}
					}
				}
				if(TypeCode=="10")
				{
					foreach(DataRow Type_Code in ds.Tables["Stage"].Select("Stage_Code=44"))
					{
						foreach(DataRow Rows in ds.Tables["BioChemResult"].Rows)
						{
							Row = ds.Tables["BioChemResult"].Rows.Count + 1;//因为是2列所以

							appWord.Selection.TypeText("【"+Type_Code["Name"].ToString()+"】");
							Word.Table tabTitle = docWord.Tables.Add(appWord.Selection.Range,Row,6,ref Nothing, ref Nothing);
							tabTitle.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;//表格边框
							tabTitle.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;//设置表格边框
							tabTitle.Borders.OutsideColor = Word.WdColor.wdColorPaleBlue;//表格边框颜色
							tabTitle.Borders.InsideColor = Word.WdColor.wdColorPaleBlue;//表格边框颜色
							tabTitle.Rows[1].Range.Shading.ForegroundPatternColor = Word.WdColor.wdColorYellow;//设置第一行的单元格背景色为黄色
							appWord.Selection.GoTo(ref what, ref which, ref count, ref Nothing); //定位到文档最后一行
							appWord.Selection.PageSetup.LeftMargin = appWord.CentimetersToPoints((float)3.49);//设置标尺
							appWord.Selection.ParagraphFormat.LineSpacing = 15f;//设置文档的行间距
							//设置第一列表格的宽度
							tabTitle.Columns[1].Width = 40; 
							tabTitle.Columns[2].Width = 130; 
							tabTitle.Columns[6].Width = 40;
							//向表格单元格添加值
							tabTitle.Cell(1,1).Range.Text = "序号";//设置第一行第一列单元格内容
							tabTitle.Cell(1,2).Range.Text = "项目名称";//设置第一行第一列单元格内容
							tabTitle.Cell(1,3).Range.Text = "检查结果";//设置第一行第二列单元格内容
							tabTitle.Cell(1,4).Range.Text = "单位";//设置第一行第三列单元格内容
							tabTitle.Cell(1,5).Range.Text = "参考范围";//设置第一行第四列单元格内容
							tabTitle.Cell(1,6).Range.Text = "标志";//设置第一行第五列单元格内容

							int tabRow=2;//从第二列开始写单元格头
							int tabColumn=1;//从第1列开始
							foreach(DataRow Result in ds.Tables["BioChemResult"].Rows)
							{
								if(tabColumn > 6)//因为知道体格检查类型表格列为4如果列大4就换行 在从第1列开始
								{
									tabRow++;
									tabColumn=1;
								}
								tabTitle.Cell(tabRow,tabColumn).Range.Text = (tabRow-1).ToString().Trim();
								tabColumn++;
								tabTitle.Cell(tabRow,tabColumn).Range.Text = Result["ItemName"].ToString().Trim();
								tabColumn++;
								tabTitle.Cell(tabRow,tabColumn).Range.Text = Result["ResultValue"].ToString().Trim();
								tabColumn++;
								tabTitle.Cell(tabRow,tabColumn).Range.Text = Result["Unit"].ToString().Trim();
								tabColumn++;
								tabTitle.Cell(tabRow,tabColumn).Range.Text = Result["NorMal_Range"].ToString().Trim();
								tabColumn++;
								tabTitle.Cell(tabRow,tabColumn).Range.Text = Result["Flag"].ToString().Trim();
								tabColumn++;
								SignDoctor = Result["Name"].ToString().Trim();
							}

							foreach(DataRow Comment in ds.Tables["BioChemComment"].Rows)
							{
								if(Comment["Remark"].ToString().Trim() !="")
								{
									appWord.Selection.TypeText("备注:"+Comment["Remark"].ToString().Trim()+"
");
								}
								
							}
							//添加医生签字
							appWord.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;//右对齐
							appWord.Selection.TypeText("医师:"+SignDoctor+"
");
							appWord.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphJustify;//两端齐
							break;	
						}
					}

					//换页写结果及建议
					appWord.ActiveWindow.ActivePane.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekMainDocument;//换页
					pageBreak = Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak;//换页
					appWord.Selection.InsertBreak(ref pageBreak);//换页

					appWord.Selection.Font.Size = 12;
					appWord.Selection.TypeText(PatientName+PatientSex);
					appWord.Selection.TypeParagraph();//换段落
					appWord.Selection.ParagraphFormat.IndentCharWidth(2);//增加缩进
					appWord.Selection.Font.Bold = 5;//加粗
					appWord.Selection.TypeText("您好!您这次在我院体检发现具有临床意义的结果如下:"+"
");
					appWord.Selection.Font.Bold = 0;//取消加粗
					appWord.Selection.ParagraphFormat.IndentCharWidth(-2);//减少缩进
					int ResultNo = 0;//小结数量
					foreach(DataRow Result in ds.Tables["Senior_Result"].Rows)
					{
						ResultNo++;
						appWord.Selection.ParagraphFormat.FirstLineIndent = appWord.CentimetersToPoints((float)1.30);//增加首行缩进
						appWord.Selection.TypeText(ResultNo.ToString()+"、"+Result["Result"].ToString()+"
");
					
						
					}

					appWord.Selection.Font.Bold = 5;//加粗
					appWord.Selection.TypeText("我们根据您现在的实际健康情况提出如下建议:"+"
");
					appWord.Selection.Font.Bold = 0;//取消加粗
				
					ResultNo = 0;
					foreach(DataRow Suggestion in ds.Tables["Senior_Result"].Rows)
					{
						ResultNo++;
						if(Suggestion["Suggestion"].ToString()=="")
						{
							ResultNo--;
						}
						else
						{
							appWord.Selection.ParagraphFormat.FirstLineIndent = appWord.CentimetersToPoints((float)1.30);//增加首行缩进
							appWord.Selection.TypeText(ResultNo.ToString()+"、"+Suggestion["Suggestion"].ToString()+"
");
						}
					}
				}
			}
			
			//生成客套话
			appWord.Selection.TypeParagraph();//换段落
			appWord.Selection.Font.Bold = 5;//加粗
			appWord.Selection.TypeText("关爱健康、全人照顾是北京惠兰医院的宗旨。热烈欢迎您及家人、朋友再次来我院体检及就诊。我们将竭诚为您提供完美的服务。"+"
");
			appWord.Selection.TypeText("北京惠兰医院就诊咨询联系电话:64390831、64390832"+"
");
			appWord.Selection.Font.Bold = 0;//加粗
			appWord.Selection.ParagraphFormat.IndentCharWidth(10);//增加缩进
			appWord.Selection.TypeText("主检医师:"+"
");
			appWord.Selection.TypeText("首席总检医师签名:"+"
");
			appWord.Selection.TypeText("北京惠兰医院(盖章)"+"
");
			appWord.Selection.TypeText("日期:"+"
");
			appWord.Selection.ParagraphFormat.IndentCharWidth(-10);//增加缩进
			
			
			#endregion

			
	
			foreach (Word.Bookmark bm in docWord.Bookmarks)
			{
				if(bm.Name == "CheckNo")
				{
					bm.Select();
					bm.Range.Text = Mem_ID;
				}
				if(bm.Name == "TitleName")
				{
					bm.Select();
					bm.Range.Text = ds.Tables["Patient"].Rows[0]["Name"].ToString();
				}
				if(bm.Name == "Sex")
				{
					bm.Select();
					bm.Range.Text = ds.Tables["Patient"].Rows[0]["Sex"].ToString();
				}
				if(bm.Name == "Age")
				{
					bm.Select();
					bm.Range.Text = ds.Tables["Patient"].Rows[0]["Age"].ToString();

				}
				if(bm.Name == "OrderNo")
				{
					bm.Select();
					bm.Range.Text = OrderNO;

				}
				if(bm.Name == "CheckOrder")
				{
					bm.Select();
					bm.Range.Text = CheckOrder;
					bm.Range.Font.Size = 12;
					bm.Range.Font.Bold = 5;

				}
				if(bm.Name == "RegDate")
				{
					bm.Select();
					foreach(DataRow row in ds.Tables["Input_Stage_Record"].Select("Stage_Code=0"))
					{
						bm.Range.Text = row["Record_Time"].ToString();
					}
				}

				if(bm.Name == "Date")
				{
					bm.Select();
					bm.Range.Text = DateTime.Now.Date.ToShortDateString();

				}
				if(bm.Name == "Name")
				{
					bm.Select();
					bm.Range.Text = ds.Tables["Patient"].Rows[0]["Name"].ToString();
				}
				if(bm.Name == "Purpose")
				{
					bm.Select();
					bm.Range.Text = "关爱健康、全人照顾";
				}
			}
			Name = ds.Tables["Patient"].Rows[0]["Name"].ToString();
			docWord.ActiveWindow.View.Type = Word.WdViewType.wdPrintView; //设置Word文档视图模式
			appWord.ActiveDocument.Protect(Word.WdProtectionType.wdAllowOnlyReading,ref strFalse,ref strPwd,ref strFalse, ref strTrue);//设置Word权限

			#region 保存生成的Word文档
			//设置或获取文档对象保存路径
			object path = @"C:\体检报告\"+ DateTime.Now.Date.ToShortDateString()+ds.Tables["Patient"].Rows[0]["Name"].ToString()+"体检报告.doc";
			docPath = path.ToString();
			if (File.Exists(path.ToString()))//判断文件是否存在
			{
				File.Delete(path.ToString());//若文件存在就删除这个文件
			}
			//保存当前Word文档对象到物理路径
			docWord.SaveAs(ref path, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing , ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
			//关闭当前Word文档对象
			docWord.Close(ref Nothing, ref Nothing, ref Nothing);
			//关闭当前Word应用程序
			appWord.Quit(ref Nothing, ref Nothing, ref Nothing);
			//关闭WinWord进程
			System.Diagnostics.Process[] process = System.Diagnostics.Process.GetProcesses();
			foreach(System.Diagnostics.Process  prc in process)
			{
				if(prc.ProcessName == "WINWORD")
				{
					prc.Kill();
				}
			}
			return true;
			
			#endregion
		}
		#endregion
	}
}
原文地址:https://www.cnblogs.com/junjie94wan/p/3259091.html