123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514 |
- using System;
- using System.Collections.Generic;
- using System.Text;
- using Word = Microsoft.Office.Interop.Word;
- namespace Com.Jpsoft.Hospital.ReportClass.WordReports
- {
- /// <summary>
- /// Word公共操作类
- /// </summary>
- 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
- /// <summary>
- /// 根据模板打开Word文档
- /// </summary>
- /// <param name="strFileName">模板的物理路径</param>
- 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
- /// <summary>
- /// 打开一个新的文档
- /// </summary>
- 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;
- }
- }
- /// <summary>
- /// 退出Word
- /// </summary>
- 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();
- }
- /// <summary>
- /// 保存文档
- /// </summary>
- public void Save()
- {
- try
- {
- oDoc.Save();
- }
- catch (Exception ex)
- {
- string errStr = ex.Message;
- Quit();
- throw ex;
- }
- }
- /// <summary>
- /// 把创建的文档保存到指定路径
- /// </summary>
- /// <param name="strFileName">要保存的物理路径</param>
- 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;
- }
- }
- /// <summary>
- /// 另存为HTML格式
- /// </summary>
- /// <param name="strFileName">保存的物理路径</param>
- 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;
- }
- }
- /// <summary>
- /// 在光标处插入文本
- /// </summary>
- /// <param name="strText">要插入的文本</param>
- public void InsertText(string strText)
- {
- try
- {
- oWordApplic.Selection.TypeText(strText);
- }
- catch (Exception ex)
- {
- string errStr = ex.Message;
- Quit();
- throw ex;
- }
- }
- /// <summary>
- /// 插入行
- /// </summary>
- public void InsertLineBreak()
- {
- try
- {
- oWordApplic.Selection.TypeParagraph();
- }
- catch (Exception ex)
- {
- string errStr = ex.Message;
- Quit();
- throw ex;
- }
- }
- /// <summary>
- /// 插入多行
- /// </summary>
- /// <param name="nline">行数</param>
- 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
- /// <summary>
- /// 设置对齐格式
- /// </summary>
- /// <param name="strType">要设置的格式"Center,Left,Right,Justify"</param>
- 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
- /// <summary>
- /// 设置字体样式
- /// </summary>
- /// <param name="strType">字体"Bold,Italic,Underlined"</param>
- 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
- /// <summary>
- /// 取消所有字体样式
- /// </summary>
- 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;
- }
- }
- /// <summary>
- /// 设置字体名
- /// </summary>
- /// <param name="strType">指定字体名</param>
- public void SetFontName(string strType)
- {
- try
- {
- oWordApplic.Selection.Font.Name = strType;
- }
- catch (Exception ex)
- {
- string errStr = ex.Message;
- Quit();
- throw ex;
- }
- }
- /// <summary>
- /// 设置字体大小
- /// </summary>
- /// <param name="nsize">字体大小</param>
- public void SetFontSize(int nsize)
- {
- try
- {
- oWordApplic.Selection.Font.Size = nsize;
- }
- catch (Exception ex)
- {
- string errStr = ex.Message;
- Quit();
- throw ex;
- }
- }
- /// <summary>
- /// 插入分页符
- /// </summary>
- 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
- /// <summary>
- /// 光标移动到书签处
- /// </summary>
- /// <param name="strBookMarkName">书签名</param>
- 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;
- }
- }
- /// <summary>
- /// 光标移动到结尾处
- /// </summary>
- 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;
- }
- }
- /// <summary>
- /// 光标移动到开始处
- /// </summary>
- 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;
- }
- }
- /// <summary>
- /// 光标移动到表格
- /// </summary>
- /// <param name="ntable"></param>
- 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;
- }
- }
- /// <summary>
- /// 光标移动到右方单元格
- /// </summary>
- 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;
- }
- }
- /// <summary>
- /// 光标移动到左方单元格
- /// </summary>
- 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;
- }
- }
- /// <summary>
- /// 光标移动到下方单元格
- /// </summary>
- 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;
- }
- }
- /// <summary>
- /// 光标移动到上方单元格
- /// </summary>
- 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;
- }
- }
- }
- }
|