/// <summary> /// 插入测试列表的方法。 /// </summary> /// <param name="tableindex"></param> /// <param name="dtable"></param> public void insertTableForTest(string parLableName, System.Data.DataTable dtable) { object lableName = parLableName; Bookmark bm = objDocLast.Bookmarks.get_Item(ref lableName); //返回标签 Word.Table wtable = bm.Range.Tables[1]; // object wrow = wtable.Rows.Last; wtable.Rows.Last.Select(); object rownum = dtable.Rows.Count; objApp.Selection.InsertRowsBelow(ref rownum); for (int i = 0; i < dtable.Rows.Count; i++) { //超链接(编号) object range = wtable.Cell(i + 2, 1).Range; object unitno = dtable.Rows[i][0].ToString(); object address = unitDOCpath + "\\" + unitno + ".doc"; objApp.Selection.Hyperlinks.Add(range, ref address, ref objMissing, ref objMissing, ref unitno, ref objMissing); wtable.Cell(i + 2, 2).Range.Text = dtable.Rows[i][1].ToString(); //等级 wtable.Cell(i + 2, 3).Range.Text = dtable.Rows[i][2].ToString(); //名称 wtable.Cell(i + 2, 4).Range.Text = dtable.Rows[i][3].ToString(); //title } }
/// <summary> /// 插入更新包列表的方法 /// </summary> /// <param name="tableindex"></param> /// <param name="dtable"></param> public void insertTableForPack(string parLableName, System.Data.DataTable dtable) { object lableName = parLableName; Bookmark bm = objDocLast.Bookmarks.get_Item(ref lableName); //返回标签 Word.Table wtable = bm.Range.Tables[1]; object wrow = wtable.Rows.Last; wtable.Rows.Last.Select(); object rownum = dtable.Rows.Count; objApp.Selection.InsertRowsBelow(ref rownum); for (int i = 0; i < dtable.Rows.Count; i++) { wtable.Cell(i + 2, 1).Range.Text = (i + 1).ToString(); wtable.Cell(i + 2, 2).Range.Text = dtable.Rows[i][0].ToString(); wtable.Cell(i + 2, 3).Range.Text = dtable.Rows[i][1].ToString(); wtable.Cell(i + 2, 4).Range.Text = dtable.Rows[i][2].ToString(); // object range =wtable.Cell(i+2, 2).Range; // object adr = @"\\192.10.110.206\e\测试文档\DOC\BUG2011080901.doc"; // object adr1 = @"\\192.10.110.206\"; // objApp.Selection.Hyperlinks.Add(range,ref adr,ref objMissing,ref objMissing, // ref adr1,ref objMissing ); } }
public override void process(DataRow[] drTables, DataSet dsTableColumns, DataSet dsTablePrimaryKeys) { try { base.process(drTables, dsTableColumns, dsTablePrimaryKeys); OutPut = FilePathHelper.ExportDBDocPath; FileHelper.CreateDirectory(OutPut); setEnable(false); string msg = string.Empty; Stopwatch sw = Stopwatch.StartNew(); FileHelper.DeleteDirectory(FilePathHelper.ExportDBDocPath); if (drTables != null && dsTableColumns != null && dsTablePrimaryKeys != null) { setStatusBar(string.Format("正在生成{0}数据库文档", dbName)); setProgreesEditValue(0); setProgress(0); setProgressMax(drTables.Length); string path = FilePathHelper.ExportDBDocPath + dbName + "_DB_{0}_{1}" + ".doc"; FileHelper.CreateDirectory(path); Object Nothing = System.Reflection.Missing.Value; Word.Application dbWordApp = new Word.Application(); dbWordApp.NormalTemplate.Saved = true; Word.Document dbDoc = dbWordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing); dbWordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView; dbWordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader; dbWordApp.ActiveWindow.ActivePane.Selection.InsertAfter(string.Format("{0} {1}数据库文档", dbName, dbType)); dbWordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft; dbWordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument; string tableName = ""; int begin = 0; int end = 0; #region try { for (int j = 0; j < drTables.Length; j++) { try { if ((j + 1) % num == 0 || (j + 1).Equals(drTables.Length)) { begin = end; end = j; } #region 样式 DataRow drTable = drTables[j]; DataRow[] drTableColumns = dsTableColumns.Tables[dbName + DBtablesColumns].Select( "TABLE_NAME = '" + drTable["name"].ToString() + "'", "COLUMN_ID ASC"); tableName = drTable["name"] as string; //表名 string tableComments = drTable["comments"] as string; //表说明 setStatusBar(string.Format("正在生成{0}中{1}表信息,共{2}张表,已生成了{3}张表", dbName, tableName, drTables.Length, j)); //Word段落 Word.Paragraph p; p = dbDoc.Content.Paragraphs.Add(ref Nothing); //设置段落中的内容文本 p.Range.Text = "表" + Convert.ToString(j + 1) + ":" + tableName + "(" + tableComments + ")"; //设置为一号标题 object style = Word.WdBuiltinStyle.wdStyleHeading1; p.set_Style(ref style); //添加到末尾 p.Range.InsertParagraphAfter(); style = Word.WdBuiltinStyle.wdStyleBodyText; p.set_Style(ref style); //dbWordApp.Selection.TypeText("表" + Convert.ToString(j + 1) + ":" + tableName + "(" + // tableComments + ")"); dbWordApp.Selection.ParagraphFormat.LineSpacing = 15f; object count = 20; object WdLine = Word.WdUnits.wdLine; dbWordApp.Selection.MoveDown(ref WdLine, ref count, ref Nothing); Word.Table newTable = dbDoc.Tables.Add(dbWordApp.Selection.Range, drTableColumns.Length + 2, 7, ref Nothing, ref Nothing); newTable.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleSingle; newTable.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle; newTable.Columns[1].Width = 110f; //列明 newTable.Columns[2].Width = 90f; //数据类型 newTable.Columns[3].Width = 50f; //是否NULL newTable.Columns[4].Width = 40f; //默认值 newTable.Columns[5].Width = 30f; //主键 newTable.Columns[6].Width = 30f; //外键 newTable.Columns[7].Width = 130f; //列说明 #endregion #region 填充表名称(表格内) newTable.Cell(1, 1).Range.Text = "表名:" + tableName + "(" + tableComments + ")"; newTable.Cell(1, 1).Range.Bold = 2; //设置单元格中字体为粗体 //合并单元格 newTable.Cell(1, 1).Merge(newTable.Cell(1, 7)); newTable.Cell(1, 1).Range.Font.Color = Word.WdColor.wdColorRed; //设置单元格内字体颜色 newTable.Cell(1, 1).Select(); //选中 dbWordApp.Selection.Cells.VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter; //垂直居中 dbWordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter; //水平居中 #endregion #region 填充列标题 newTable.Cell(2, 1).Range.Text = "列名"; newTable.Cell(2, 1).Range.Font.Color = Word.WdColor.wdColorBlack; //设置单元格内字体颜色 newTable.Cell(2, 2).Range.Font.Bold = 2; newTable.Cell(2, 2).Range.Font.Size = 9; newTable.Cell(2, 2).Select(); //选中 newTable.Cell(2, 2).VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter; dbWordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter; //水平居中 newTable.Cell(2, 2).Range.Text = "数据类型"; newTable.Cell(2, 2).Range.Font.Color = Word.WdColor.wdColorBlack; //设置单元格内字体颜色 newTable.Cell(2, 2).Range.Font.Bold = 2; newTable.Cell(2, 2).Range.Font.Size = 9; newTable.Cell(2, 2).Select(); //选中 newTable.Cell(2, 2).VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter; dbWordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter; //水平居中 newTable.Cell(2, 3).Range.Text = "是否NULL"; newTable.Cell(2, 3).Range.Font.Color = Word.WdColor.wdColorBlack; //设置单元格内字体颜色 newTable.Cell(2, 3).Range.Font.Bold = 2; newTable.Cell(2, 3).Range.Font.Size = 9; newTable.Cell(2, 3).VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter; newTable.Cell(2, 3).Select(); //选中 dbWordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter; //水平居中 newTable.Cell(2, 4).Range.Text = "默认值"; newTable.Cell(2, 4).Range.Font.Color = Word.WdColor.wdColorBlack; //设置单元格内字体颜色 newTable.Cell(2, 4).VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter; newTable.Cell(2, 4).Range.Font.Bold = 2; newTable.Cell(2, 4).Range.Font.Size = 9; newTable.Cell(2, 4).Select(); //选中 dbWordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter; //水平居中 newTable.Cell(2, 5).Range.Text = "主键"; newTable.Cell(2, 5).Range.Font.Color = Word.WdColor.wdColorBlack; //设置单元格内字体颜色 newTable.Cell(2, 5).VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter; newTable.Cell(2, 5).Range.Font.Bold = 2; newTable.Cell(2, 5).Range.Font.Size = 9; newTable.Cell(2, 5).Select(); //选中 dbWordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter; //水平居中 newTable.Cell(2, 6).Range.Text = "外键"; newTable.Cell(2, 6).Range.Font.Color = Word.WdColor.wdColorBlack; //设置单元格内字体颜色 newTable.Cell(2, 6).VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter; newTable.Cell(2, 6).Range.Font.Bold = 2; newTable.Cell(2, 6).Range.Font.Size = 9; newTable.Cell(2, 6).Select(); //选中 dbWordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter; //水平居中 newTable.Cell(2, 7).Range.Text = "备注"; newTable.Cell(2, 7).Range.Font.Color = Word.WdColor.wdColorBlack; //设置单元格内字体颜色 newTable.Cell(2, 7).VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter; newTable.Cell(2, 7).Range.Font.Bold = 2; newTable.Cell(2, 7).Range.Font.Size = 9; newTable.Cell(2, 7).Select(); //选中 dbWordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter; //水平居中 #endregion #region 填充表格内容 for (int i = 0; i < drTableColumns.Length; i++) { string tableColumnName = drTableColumns[i]["COLUMN_NAME"] as string; //列名 string tableColumnComments = drTableColumns[i]["COMMENTS"] as string; //列说明 string tableColumnDataType = drTableColumns[i]["DATA_TYPE"] as string; //列类型 string tableColumnDataScale = drTableColumns[i]["DATA_SCALE"] + ""; //列精度 string tableColumnDataLength = drTableColumns[i]["DATA_LENGTH"] + ""; //列长度 string tableColumnDataNullAble = drTableColumns[i]["NULLABLE"] as string; //可空 string tableColumnDataDefault = drTableColumns[i]["DATA_DEFAULT"] as string; //默认值 string tableColumnDataPrecision = drTableColumns[i]["Data_Precision"] + ""; if (string.IsNullOrEmpty(tableColumnDataPrecision)) //字符串、日期等 { if ("DATE".Equals(tableColumnDataType)) { tableColumnDataType = tableColumnDataType + "()"; } else { tableColumnDataType = tableColumnDataType + "(" + tableColumnDataLength + ")"; } } else //数字类型等 { if ("0".Equals(tableColumnDataScale)) { tableColumnDataType = tableColumnDataType + "(" + tableColumnDataPrecision + ")"; } else { tableColumnDataType = tableColumnDataType + "(" + tableColumnDataPrecision + "," + tableColumnDataScale + ")"; } } //列名 newTable.Cell(i + 3, 1).Range.Text = tableColumnName; newTable.Cell(i + 3, 1).VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter; //数据类型 newTable.Cell(i + 3, 2).Range.Text = tableColumnDataType; newTable.Cell(i + 3, 2).VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter; //是否Null newTable.Cell(i + 3, 3).Range.Text = tableColumnDataNullAble; newTable.Cell(i + 3, 3).VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter; newTable.Cell(i + 3, 3).Select(); dbWordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter; //水平居中 //默认值 newTable.Cell(i + 3, 4).Range.Text = tableColumnDataDefault; newTable.Cell(i + 3, 4).VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter; newTable.Cell(i + 3, 4).Select(); dbWordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter; //水平居中 //主键 newTable.Cell(i + 3, 5).Range.Text = ""; DataRow[] dr = dsTablePrimaryKeys.Tables[dbName + DBtablesPrimaryKeys].Select( "TABLE_NAME = '" + tableName + "' AND COLUMN_NAME ='" + tableColumnName + "' and constraint_type='P'"); if (dr != null && dr.Length > 0) { newTable.Cell(i + 3, 5).Range.Text = "Y"; } newTable.Cell(i + 3, 5).VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter; newTable.Cell(i + 3, 5).Select(); dbWordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter; //水平居中 //外键 newTable.Cell(i + 3, 6).Range.Text = ""; DataRow[] dr2 = dsTablePrimaryKeys.Tables[dbName + DBtablesPrimaryKeys].Select( "TABLE_NAME = '" + tableName + "' AND COLUMN_NAME ='" + tableColumnName + "' and constraint_type='R'"); if (dr2 != null && dr2.Length > 0) { newTable.Cell(i + 3, 6).Range.Text = "Y"; } newTable.Cell(i + 3, 6).VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter; newTable.Cell(i + 3, 6).Select(); dbWordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter; //水平居中 //说明 newTable.Cell(i + 3, 7).Range.Text = tableColumnComments; newTable.Cell(i + 3, 7).VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter; } #endregion setProgress(1); } catch (Exception ex) { msg = ex.Message; string str = (string.Format("{0}生成数据库文档丢失{1}表", dbName, tableName)); setStatusBar(str); LogHelper.Error(ex); LogHelper.Debug(str); } finally { #region 释放资源 if ((j + 1) % num == 0 || (j + 1).Equals(drTables.Length)) { try { object fileName = string.Format(path, begin + 1, end + 1); dbDoc.SaveAs(ref fileName, 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); } catch (Exception ex) { msg = ex.Message; LogHelper.Error(ex); } try { dbDoc.Close(ref Nothing, ref Nothing, ref Nothing); } catch { } if (dbDoc != null) { try { System.Runtime.InteropServices.Marshal.ReleaseComObject(dbDoc); } catch { } dbDoc = null; } GC.Collect(); if (!(j + 1).Equals(drTables.Length)) { dbDoc = dbWordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing); } } #endregion } } } catch (Exception ex) { msg = ex.Message; LogHelper.Error(ex); } finally { #region 释放资源 try { dbWordApp.Quit(ref Nothing, ref Nothing, ref Nothing); } catch { } if (dbWordApp != null) { try { System.Runtime.InteropServices.Marshal.ReleaseComObject(dbWordApp); } catch { } dbWordApp = null; } GC.Collect(); #endregion } #endregion } sw.Stop(); if (string.IsNullOrEmpty(msg)) { setStatusBar(string.Format("{0}数据库文档生成成功", dbName)); openDialog(); } else { setStatusBar(string.Format("{0}数据库文档生成失败[{1}]", dbName, msg)); } setEnable(true); } catch (Exception ex) { setStatusBar(string.Format("{0}数据库文档生成失败[{1}]", dbName, ex.Message)); } }
/// <summary> /// 打印病历 /// </summary> public void Print(HIS.EMR_BLL.EmrRecord emrRecord) { XmlDocument xmlDoc = new HIS.EMR_BLL.OperaEmrRecord().GetPatInfo(emrRecord.PatListId, emrRecord.RecordCreateDate); object oMissing = System.Reflection.Missing.Value; object oEndOfDoc = "\\endofdoc"; Word.Application wordApp = new Word.Application(); base.InitWordApplication(wordApp, xmlDoc, Public.EMRType.手术记录); int columnNum = xmlDoc.SelectSingleNode("病人信息/基本信息").ChildNodes.Count; Word.Table table = wordApp.ActiveDocument.Tables.Add(wordApp.Selection.Range, columnNum, 1, ref oMissing, ref oMissing); wordApp.Selection.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleNone; wordApp.Selection.Borders.InsideLineStyle = WdLineStyle.wdLineStyleNone; int columnNo = 1; object step = 1; object unit = Word.WdUnits.wdCharacter; object extend = Word.WdMovementType.wdExtend; foreach (XmlNode node in xmlDoc.SelectSingleNode("病人信息/基本信息").ChildNodes) { wordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft; wordApp.Selection.Font.Size = Public.StaticVariable.BodyTitleFontSize; wordApp.Selection.Font.Bold = Public.StaticVariable.BodyTitleFontBold; wordApp.Selection.Font.Name = Public.StaticVariable.BodyTitleFontName; wordApp.Selection.TypeText(" " + node.InnerText.Split(':')[0] + ":"); wordApp.Selection.Font.Size = Public.StaticVariable.BodyFontSize; wordApp.Selection.Font.Bold = Public.StaticVariable.BodyFontBold; wordApp.Selection.Font.Name = Public.StaticVariable.BodyFontName; wordApp.Selection.TypeText(node.InnerText.Split(':')[1]); columnNo++; //if (columnNo <= columnNum) //{ // wordApp.Selection.Move(ref unit, ref step); //} //else //{ unit = Word.WdUnits.wdLine; wordApp.Selection.MoveDown(ref unit, ref step, ref oMissing); // break; //} } XElement xe = XElement.Parse(this.GetValue().InnerXml); var nodes = from e in xe.Descendants() orderby e.Attribute("Code").Value select e; foreach (XElement node in nodes) { if (node.Value.ToString().Trim().Length > 0) { wordApp.Selection.TypeParagraph(); wordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft; wordApp.Selection.Font.Size = Public.StaticVariable.BodyTitleFontSize; wordApp.Selection.Font.Bold = Public.StaticVariable.BodyTitleFontBold; wordApp.Selection.Font.Name = Public.StaticVariable.BodyTitleFontName; wordApp.Selection.TypeText(" " + node.Name.ToString().Trim() + ":"); wordApp.Selection.Font.Size = Public.StaticVariable.BodyFontSize; wordApp.Selection.Font.Bold = Public.StaticVariable.BodyFontBold; wordApp.Selection.Font.Name = Public.StaticVariable.BodyFontName; wordApp.Selection.TypeText(node.Value.ToString().Trim()); } } //医生签名 wordApp.Selection.TypeParagraph(); wordApp.Selection.TypeParagraph(); wordApp.Selection.Font.Size = Public.StaticVariable.BodyFontSize; wordApp.Selection.Font.Bold = 0; wordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphRight; wordApp.Selection.TypeText("手术医师:____________________"); wordApp.Selection.TypeParagraph(); wordApp.Selection.TypeText("主治医师:____________________"); wordApp.Selection.TypeParagraph(); wordApp.Selection.TypeText("科主任或副主任医师:__________"); wordApp.Selection.TypeParagraph(); wordApp.Selection.TypeText(" 记录时间:" + emrRecord.RecordCreateDate.Year + "." + emrRecord.RecordCreateDate.Month + "." + emrRecord.RecordCreateDate.Day + "." + emrRecord.RecordCreateDate.Hour + ":" + emrRecord.RecordCreateDate.Minute); }
static void Main(string[] args) { object oMissing = Type.Missing; //System.Reflection.Missing.Value; Word.Application WordApp = null; Word.Document Document = null; Word.Table Table = null; string CurrentDirectory = System.IO.Directory.GetCurrentDirectory(), tmpString; CurrentDirectory = CurrentDirectory.Substring(0, CurrentDirectory.LastIndexOf("bin", CurrentDirectory.Length - 1)); object InputFileName, OutputFileName, tmpObject; try { try { try { WordApp = (Word.Application)Marshal.GetActiveObject("Word.Application"); } catch (COMException eException) { if (eException.ErrorCode == -2147221021) { WordApp = new Word.Application(); } } WordApp.Visible = true; #if TEST_BOOKMARK InputFileName = CurrentDirectory + "Word_tst.doc"; Document = WordApp.Documents.Open(ref InputFileName, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing); if (Document.Bookmarks.Count > 0) { tmpString = "MyBookmark"; if (Document.Bookmarks.Exists(tmpString)) { tmpObject = tmpString; #if TEST_BOOKMARK_BY_BOOKMARKS_COLLECTION Document.Bookmarks.Item(ref tmpObject).Select(); #else object What = Word.WdGoToItem.wdGoToBookmark; WordApp.Selection.GoTo(/*What*/ ref What, /*Which*/ ref oMissing, /*Count*/ ref oMissing, /*Name*/ ref tmpObject); #endif WordApp.Selection.Text = "1234567890"; } } OutputFileName = CurrentDirectory + "Word_tst_out.doc"; if (File.Exists(OutputFileName.ToString())) { File.Delete(OutputFileName.ToString()); } Document.SaveAs(ref OutputFileName, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing); Document.Close(ref oMissing, ref oMissing, ref oMissing); #endif Document = WordApp.Documents.Add(ref oMissing, ref oMissing); int index = 0, rows = 3, columns = 2; Table = Document.Tables.Add(WordApp.Selection.Range, rows + 1, columns); Table.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter; Table.Range.Bold = 1; for (int i = 0; i < columns; i++) { Table.Cell(0, i + 1).Range.InsertAfter(i.ToString()); } for (int j = 1; j <= rows; j++) { Table.Rows.Item(j + 1).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft; Table.Rows.Item(j + 1).Range.Bold = 0; for (int i = 0; i < columns; i++) { Table.Cell(j + 1, i + 1).Range.InsertAfter(index.ToString()); index++; } } OutputFileName = CurrentDirectory + "test_out.doc"; if (File.Exists(OutputFileName.ToString())) { File.Delete(OutputFileName.ToString()); } Document.SaveAs(ref OutputFileName, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing); Document.Close(ref oMissing, ref oMissing, ref oMissing); } catch (COMException eException) { string tmp = eException.GetType().FullName + Environment.NewLine + "ErrorCode: " + eException.ErrorCode + Environment.NewLine + "Message: " + eException.Message + Environment.NewLine + "StackTrace:" + Environment.NewLine + eException.StackTrace; Console.WriteLine(tmp); } catch (ArgumentException eException) { string tmp = eException.GetType().FullName + Environment.NewLine + "ParamName: " + eException.ParamName + Environment.NewLine + "Message: " + eException.Message + Environment.NewLine + "StackTrace:" + Environment.NewLine + eException.StackTrace; Console.WriteLine(tmp); } catch (Exception eException) { string tmp = eException.GetType().FullName + Environment.NewLine + "Message: " + eException.Message + Environment.NewLine + "StackTrace:" + Environment.NewLine + eException.StackTrace; Console.WriteLine(tmp); } } finally { if (Table != null) { Marshal.ReleaseComObject(Table); Table = null; } if (Document != null) { Marshal.ReleaseComObject(Document); Document = null; } if (WordApp != null) { WordApp.Quit(ref oMissing, ref oMissing, ref oMissing); Marshal.ReleaseComObject(WordApp); WordApp = null; } GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); GC.GetTotalMemory(true); //GC.SuppressFinalize(this); } }
/// <summary> /// 初始化打印控件 /// </summary> /// <param name="wordApp"></param> /// <param name="xmlDoc"></param> /// <param name="type"></param> protected void InitWordApplication(Word.Application wordApp, XmlDocument xmlDoc, Public.EMRType type) { object oMissing = Missing.Value; wordApp.Visible = true; wordApp.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing); //设置页边距 wordApp.ActiveDocument.PageSetup.TopMargin = wordApp.CentimetersToPoints(float.Parse("1")); //上页边距 wordApp.ActiveDocument.PageSetup.BottomMargin = wordApp.CentimetersToPoints(float.Parse("1.5")); //上页边距 wordApp.ActiveDocument.PageSetup.LeftMargin = wordApp.CentimetersToPoints(float.Parse("2.5")); //上页边距 wordApp.ActiveDocument.PageSetup.RightMargin = wordApp.CentimetersToPoints(float.Parse("2.5")); //上页边距 //设置边线 wordApp.ActiveDocument.Sections[1].Borders.DistanceFrom = WdBorderDistanceFrom.wdBorderDistanceFromText; wordApp.ActiveDocument.Sections[1].Borders[WdBorderType.wdBorderTop].LineStyle = WdLineStyle.wdLineStyleNone; wordApp.ActiveDocument.Sections[1].Borders[WdBorderType.wdBorderBottom].LineStyle = WdLineStyle.wdLineStyleSingle; wordApp.ActiveDocument.Sections[1].Borders[WdBorderType.wdBorderLeft].LineStyle = WdLineStyle.wdLineStyleSingle; wordApp.ActiveDocument.Sections[1].Borders[WdBorderType.wdBorderRight].LineStyle = WdLineStyle.wdLineStyleNone; // 添加页眉 wordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView; wordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader; object styleType = "正文"; object style = wordApp.ActiveDocument.Styles.get_Item(ref styleType); wordApp.Selection.set_Style(ref style); //医院名称 wordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter; wordApp.Selection.Font.Size = 16;//字体加大 wordApp.Selection.Font.Bold = 1; wordApp.Selection.TypeText(HIS.SYSTEM.BussinessLogicLayer.Classes.BaseData.WorkName); //标题 wordApp.Selection.TypeParagraph(); wordApp.Selection.Font.Size = 22; wordApp.Selection.Font.Bold = 1; wordApp.Selection.TypeText("病 历 记 录"); wordApp.Selection.TypeParagraph(); //wordApp.Selection.TypeText(type == HIS_EMRManager.Public.EMRType.病程记录 ? "病 程 记 录" : "病 历 记 录"); //病人基本信息 wordApp.Selection.Font.Size = 12; Word.Table table = wordApp.ActiveDocument.Tables.Add(wordApp.Selection.Range, 1, 3, ref oMissing, ref oMissing); wordApp.Selection.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleNone; wordApp.Selection.Borders.InsideLineStyle = WdLineStyle.wdLineStyleNone; int columnNo = 1; object step = 1; object unit = Word.WdUnits.wdCharacter; object extend = Word.WdMovementType.wdExtend; foreach (XmlNode node in xmlDoc.SelectSingleNode("病人信息/基本信息").ChildNodes) { wordApp.Selection.Font.Size = 12; wordApp.Selection.Font.Bold = 0; wordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft; wordApp.Selection.TypeText(" " + (columnNo == 2?"(第 页)":node.InnerText)); columnNo++; if (columnNo <= 3) { wordApp.Selection.Move(ref unit, ref step); } else { unit = Word.WdUnits.wdLine; wordApp.Selection.MoveDown(ref unit, ref step, ref oMissing); break; } } //画分隔线 //wordApp.ActiveDocument.Shapes.AddLine(70, 125, 530, 125, ref oMissing); wordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument; // 跳出页眉设置 wordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter; wordApp.Selection.ParagraphFormat.Borders[WdBorderType.wdBorderTop].LineStyle = WdLineStyle.wdLineStyleSingle; wordApp.Selection.ParagraphFormat.Borders[WdBorderType.wdBorderBottom].LineStyle = WdLineStyle.wdLineStyleNone; wordApp.Selection.ParagraphFormat.Borders[WdBorderType.wdBorderLeft].LineStyle = WdLineStyle.wdLineStyleNone; wordApp.Selection.ParagraphFormat.Borders[WdBorderType.wdBorderRight].LineStyle = WdLineStyle.wdLineStyleNone; //标题 //wordApp.Selection.TypeParagraph(); wordApp.Selection.Font.Size = 14; wordApp.Selection.Font.Bold = 1; wordApp.Selection.TypeText(type.ToString()); wordApp.Selection.ParagraphFormat.LineSpacing = 20; }