using System; using System.Collections.Generic; using System.Text; using Word = Microsoft.Office.Interop.Word; namespace Com.Jpsoft.Hospital.ReportClass.WordReports { /// /// Word公共操作类 /// public class WordOperation { private Word._Application oWordApplic;//a reference to Word application private Word._Document oDoc;//a reference to the document; public WordOperation() { //activate the interface with the COM object of Microsoft Word //实例化Word COM对象 oWordApplic = new Microsoft.Office.Interop.Word.ApplicationClass(); } //Open a file (the file must exists) and activate it /// /// 根据模板打开Word文档 /// /// 模板的物理路径 public void Open(string strFileName) { object filename = strFileName; object readOnly = false; object isVisible = true; object missing = System.Reflection.Missing.Value; try { oDoc = oWordApplic.Documents.Open(ref filename, 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); oDoc.Activate(); } catch (Exception ex) { string errStr = ex.Message; Quit(); throw ex; } } //Open a new document /// /// 打开一个新的文档 /// public void Open() { object missing = System.Reflection.Missing.Value; try { oDoc = oWordApplic.Documents.Add(ref missing, ref missing, ref missing, ref missing); oDoc.Activate(); } catch (Exception ex) { string errStr = ex.Message; Quit(); throw ex; } } /// /// 退出Word /// public void Quit() { object missing = System.Reflection.Missing.Value; if (oDoc != null) { oDoc.Close(ref missing, ref missing, ref missing); System.Runtime.InteropServices.Marshal.ReleaseComObject(oDoc); oDoc = null; } if (oWordApplic != null) { oWordApplic.Quit(ref missing, ref missing, ref missing); System.Runtime.InteropServices.Marshal.ReleaseComObject(oWordApplic); oWordApplic = null; } GC.Collect(); } /// /// 保存文档 /// public void Save() { try { oDoc.Save(); } catch (Exception ex) { string errStr = ex.Message; Quit(); throw ex; } } /// /// 把创建的文档保存到指定路径 /// /// 要保存的物理路径 public void SaveAs(string strFileName) { object missing = System.Reflection.Missing.Value; object filename = strFileName; try { oDoc.SaveAs(ref filename, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing); } catch (Exception ex) { string errStr = ex.Message; Quit(); throw ex; } } /// /// 另存为HTML格式 /// /// 保存的物理路径 public void SaveAsHtml(string strFileName) { object missing = System.Reflection.Missing.Value; object filename = strFileName; object Format = (int)Word.WdSaveFormat.wdFormatHTML; try { oDoc.SaveAs(ref filename, ref Format, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing); } catch (Exception ex) { string errStr = ex.Message; Quit(); throw ex; } } /// /// 在光标处插入文本 /// /// 要插入的文本 public void InsertText(string strText) { try { oWordApplic.Selection.TypeText(strText); } catch (Exception ex) { string errStr = ex.Message; Quit(); throw ex; } } /// /// 插入行 /// public void InsertLineBreak() { try { oWordApplic.Selection.TypeParagraph(); } catch (Exception ex) { string errStr = ex.Message; Quit(); throw ex; } } /// /// 插入多行 /// /// 行数 public void InsertLineBreak(int nline) { try { for (int i = 0; i < nline; i++) { oWordApplic.Selection.TypeParagraph(); } } catch (Exception ex) { string errStr = ex.Message; Quit(); throw ex; } } //Change the paragragh alignment /// /// 设置对齐格式 /// /// 要设置的格式"Center,Left,Right,Justify" public void SetAlignment(string strType) { try { switch (strType) { case "Center": oWordApplic.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter; break; case "Left": oWordApplic.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft; break; case "Right": oWordApplic.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight; break; case "Justify": oWordApplic.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphJustify; break; } } catch (Exception ex) { string errStr = ex.Message; Quit(); throw ex; } } //if you use this function to change the font you should call it again with //no parameter in order to set the font without a particular format /// /// 设置字体样式 /// /// 字体"Bold,Italic,Underlined" public void SetFont(string strType) { try { switch (strType) { case "Bold": oWordApplic.Selection.Font.Bold = 1; break; case "Italic": oWordApplic.Selection.Font.Italic = 1; break; case "Underlined": oWordApplic.Selection.Font.Subscript = 0; break; } } catch (Exception ex) { string errStr = ex.Message; Quit(); throw ex; } } //disable all the style /// /// 取消所有字体样式 /// public void SetFont() { try { oWordApplic.Selection.Font.Bold = 0; oWordApplic.Selection.Font.Italic = 0; oWordApplic.Selection.Font.Subscript = 0; } catch (Exception ex) { string errStr = ex.Message; Quit(); throw ex; } } /// /// 设置字体名 /// /// 指定字体名 public void SetFontName(string strType) { try { oWordApplic.Selection.Font.Name = strType; } catch (Exception ex) { string errStr = ex.Message; Quit(); throw ex; } } /// /// 设置字体大小 /// /// 字体大小 public void SetFontSize(int nsize) { try { oWordApplic.Selection.Font.Size = nsize; } catch (Exception ex) { string errStr = ex.Message; Quit(); throw ex; } } /// /// 插入分页符 /// public void InsertPagebreak() { try { object pBreak = (int)Word.WdBreakType.wdPageBreak; oWordApplic.Selection.InsertBreak(ref pBreak); } catch (Exception ex) { string errStr = ex.Message; Quit(); throw ex; } } //Go to a predefineed bookmark,if the bookmark doesn't exists the application will raise an error /// /// 光标移动到书签处 /// /// 书签名 public void GotoBookMark(string strBookMarkName) { try { object missing = System.Reflection.Missing.Value; object Bookmark = (int)Word.WdGoToItem.wdGoToBookmark; object NameBookMark = strBookMarkName; oWordApplic.Selection.GoTo(ref Bookmark, ref missing, ref missing, ref NameBookMark); } catch (Exception ex) { string errStr = ex.Message; Quit(); throw ex; } } /// /// 光标移动到结尾处 /// public void GoToTheEnd() { try { object missing = System.Reflection.Missing.Value; object unit; unit = Word.WdUnits.wdStory; oWordApplic.Selection.EndKey(ref unit, ref missing); } catch (Exception ex) { string errStr = ex.Message; Quit(); throw ex; } } /// /// 光标移动到开始处 /// public void GoToTheBeginning() { try { object missing = System.Reflection.Missing.Value; object unit; unit = Word.WdUnits.wdStory; oWordApplic.Selection.HomeKey(ref unit, ref missing); } catch (Exception ex) { string errStr = ex.Message; Quit(); throw ex; } } /// /// 光标移动到表格 /// /// public void GoToTheTable(int ntable) { try { object missing = System.Reflection.Missing.Value; object what; what = Word.WdUnits.wdTable; object which; which = Word.WdGoToDirection.wdGoToFirst; object count; count = 1; oWordApplic.Selection.GoTo(ref what, ref which, ref count, ref missing); oWordApplic.Selection.Find.ClearFormatting(); oWordApplic.Selection.Text = ""; } catch (Exception ex) { string errStr = ex.Message; Quit(); throw ex; } } /// /// 光标移动到右方单元格 /// public void GoToRightCell() { try { object missing = System.Reflection.Missing.Value; object direction; direction = Word.WdUnits.wdCell; oWordApplic.Selection.MoveRight(ref direction, ref missing, ref missing); } catch (Exception ex) { string errStr = ex.Message; Quit(); throw ex; } } /// /// 光标移动到左方单元格 /// public void GoToLeftCell() { try { object missing = System.Reflection.Missing.Value; object direction; direction = Word.WdUnits.wdCell; oWordApplic.Selection.MoveLeft(ref direction, ref missing, ref missing); } catch (Exception ex) { string errStr = ex.Message; Quit(); throw ex; } } /// /// 光标移动到下方单元格 /// public void GoToDownCell() { try { object missing = System.Reflection.Missing.Value; object direction; direction = Word.WdUnits.wdCell; oWordApplic.Selection.MoveDown(ref direction, ref missing, ref missing); } catch (Exception ex) { string errStr = ex.Message; Quit(); throw ex; } } /// /// 光标移动到上方单元格 /// public void GoToUpCell() { try { object missing = System.Reflection.Missing.Value; object direction; direction = Word.WdUnits.wdCell; oWordApplic.Selection.MoveUp(ref direction, ref missing, ref missing); } catch (Exception ex) { string errStr = ex.Message; Quit(); throw ex; } } } }