操作Word的辅助类(word2003)
该类在他人编写的几个类基础上扩展完善而来,主要功能有:
(1)插入文本
(2)插入图片
(3)插入表格
(4)载入模版
(5)编辑模版,利用标签等
(6)插入页眉页脚
- /***************************************************************************
- * word辅助类
- * 作者:chengfellow
- * 日期:2008.8.18
- * 注意事项:
- * 1、开发环境居于office 2003;
- * 2、需要添加Com引用:Microsoft Office 11.0 Object Library和
- * Microsoft Word 11.0 Object Library。
- *
- ****************************************************************************/
- using
- System;
- using System.Collections.Generic;
- using System.Text;
- using System.Drawing;
- using System.Windows.Forms;
- using System.IO;
- using System.Data;
- namespace WordAddinSample
- {
- public class WordHelp
- {
- #region - 属性 -
- private Microsoft.Office.Interop.Word.ApplicationClass oWord;
- // a reference to Word application,应用程序
- private Microsoft.Office.Interop.Word.Document oDoc;
- // a reference to the document,具体文档
- object missing = System.Reflection.Missing.Value;
- public Microsoft.Office.Interop.Word.ApplicationClass
- WordApplication
- {
- get { return oWord; }
- }
- public string ActiveWindowCaption {
- get {
- return oWord.ActiveWindow.Caption;
- }
- set {
- oWord.ActiveWindow.Caption = value;
- }
- }
- public enum OwdWrapType
- {
- 嵌入型, //wdWrapInline
- 四周型, //Square.
- 紧密型, //Tight.
- 衬于文字下方,//Behind text.
- 衬于文字上方 //Top and bottom.
- }
- #endregion
- #region - 创建关闭文档 -
- public WordHelp() //构造函数 1
- {
- // activate the interface with the COM object of Microsoft
- Word
- oWord = new
- Microsoft.Office.Interop.Word.ApplicationClass();
- }
- public WordHelp(Microsoft.Office.Interop.Word.ApplicationClass
- wordapp) //构造函数 2
- {
- oWord = wordapp;
- }
- // Open a file (the file must exists) and activate it,打开已存在
- public void Open(string strFileName)
- {
- object fileName = strFileName;
- object readOnly = false;
- object isVisible = true;
- oDoc = oWord.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();
- }
- // Open a new document,创建新文档
- public void Open()
- {
- oDoc = oWord.Documents.Add(ref missing, ref missing, ref
- missing, ref missing);
- oDoc.Activate();
- }
- public void Quit()
- {
- oDoc.Close(ref missing, ref missing, ref missing);
- if (oDoc != null)
- {
- System.Runtime.InteropServices.Marshal.ReleaseComObject(oDoc);
- oDoc = null;
- }
- // oWord.Application.Quit(ref missing, ref missing, ref
- missing); tjt
- oWord.Quit(ref missing, ref missing, ref missing);
- if (oWord != null)
- {
- System.Runtime.InteropServices.Marshal.ReleaseComObject(oWord);
- oWord = null;
- }
- //释放word进程
- GC.Collect();
- }
- /// <summary>
- /// 从模板创建新的Word文档,
- /// </summary>
- /// <param name="templateName">模板文件名</param>
- /// <returns></returns>
- public bool LoadDotFile(string templateName)
- {
- if (!string.IsNullOrEmpty(templateName))
- {
- oWord.Visible = false;
- oWord.Caption = "";
- oWord.Options.CheckSpellingAsYouType = false;
- oWord.Options.CheckGrammarAsYouType = false;
- Object Template = templateName;// Optional Object. The
- name of the template to be used for the new document. If this argument
- is omitted, the Normal template is used.
- Object NewTemplate = false;// Optional Object. True to
- open the document as a template. The default value is False.
- Object DocumentType =
- Microsoft.Office.Interop.Word.WdNewDocumentType.wdNewBlankDocument; //
- Optional Object. Can be one of the following WdNewDocumentType
- constants: wdNewBlankDocument, wdNewEmailMessage, wdNewFrameset, or
- wdNewWebPage. The default constant is wdNewBlankDocument.
- Object Visible = true;//Optional Object. True to open
- the document in a visible window. If this value is False, Microsoft Word
- opens the document but sets the Visible property of the document window
- to False. The default value is True.
- try
- {
- oDoc = oWord.Documents.Add(ref Template, ref
- NewTemplate, ref DocumentType, ref Visible);
- return true;
- }
- catch (Exception ex)
- {
- string err = string.Format("创建Word文档出错,错误原因:{0}",
- ex.Message);
- throw new Exception(err, ex);
- }
- }
- return false;
- }
- ///
- /// 打开Word文档,并且返回对象oDoc
- /// 完整Word文件路径+名称
- /// 返回的Word.Document oDoc对象
- public Microsoft.Office.Interop.Word.Document
- CreateWordDocument(string FileName, bool HideWin)
- {
- if (FileName == "") return null;
- oWord.Visible = HideWin;
- oWord.Caption = "";
- oWord.Options.CheckSpellingAsYouType = false;
- oWord.Options.CheckGrammarAsYouType = false;
- Object filename = FileName;
- Object ConfirmConversions = false;
- Object ReadOnly = true;
- Object AddToRecentFiles = false;
- Object PasswordDocument = System.Type.Missing;
- Object PasswordTemplate = System.Type.Missing;
- Object Revert = System.Type.Missing;
- Object WritePasswordDocument = System.Type.Missing;
- Object WritePasswordTemplate = System.Type.Missing;
- Object Format = System.Type.Missing;
- Object Encoding = System.Type.Missing;
- Object Visible = System.Type.Missing;
- Object OpenAndRepair = System.Type.Missing;
- Object DocumentDirection = System.Type.Missing;
- Object NoEncodingDialog = System.Type.Missing;
- Object XMLTransform = System.Type.Missing;
- try
- {
- Microsoft.Office.Interop.Word.Document wordDoc =
- oWord.Documents.Open(ref filename, ref ConfirmConversions,
- ref ReadOnly, ref AddToRecentFiles, ref
- PasswordDocument, ref PasswordTemplate,
- ref Revert, ref WritePasswordDocument, ref
- WritePasswordTemplate, ref Format,
- ref Encoding, ref Visible, ref OpenAndRepair, ref
- DocumentDirection,
- ref NoEncodingDialog, ref XMLTransform);
- return wordDoc;
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- return null;
- }
- }
- public void SaveAs(Microsoft.Office.Interop.Word.Document oDoc,
- string strFileName)
- {
- object fileName = strFileName;
- if (File.Exists(strFileName))
- {
- if (MessageBox.Show("文件'" + strFileName +
- "'已经存在,选确定覆盖原文件,选取消退出操作!", "警告", MessageBoxButtons.OKCancel) ==
- DialogResult.OK)
- {
- 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);
- }
- else
- {
- Clipboard.Clear();
- }
- }
- else
- {
- 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);
- }
- }
- public void SaveAsHtml(Microsoft.Office.Interop.Word.Document
- oDoc, string strFileName)
- {
- object fileName = strFileName;
- //wdFormatWebArchive保存为单个网页文件
- //wdFormatFilteredHTML保存为过滤掉word标签的htm文件,缺点是有图片的话会产生网页文件夹
- if (File.Exists(strFileName))
- {
- if (MessageBox.Show("文件'" + strFileName +
- "'已经存在,选确定覆盖原文件,选取消退出操作!", "警告", MessageBoxButtons.OKCancel) ==
- DialogResult.OK)
- {
- object Format =
- (int)Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatWebArchive;
- 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);
- }
- else
- {
- Clipboard.Clear();
- }
- }
- else
- {
- object Format =
- (int)Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatWebArchive;
- 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);
- }
- }
- public void Save()
- {
- oDoc.Save();
- }
- public void SaveAs(string strFileName)
- {
- object fileName = strFileName;
- 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);
- }
- // Save the document in HTML format
- public void SaveAsHtml(string strFileName)
- {
- object fileName = strFileName;
- object Format =
- (int)Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatHTML;
- 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);
- }
- #endregion
- #region 添加菜单(工具栏)项
- //添加单独的菜单项
- public void AddMenu(Microsoft.Office.Core.CommandBarPopup
- popuBar)
- {
- Microsoft.Office.Core.CommandBar menuBar = null;
- menuBar = this.oWord.CommandBars["Menu Bar"];
- popuBar =
- (Microsoft.Office.Core.CommandBarPopup)this.oWord.CommandBars.FindControl(Microsoft.Office.Core.MsoControlType.msoControlPopup,
- missing, popuBar.Tag, true);
- if (popuBar == null)
- {
- popuBar =
- (Microsoft.Office.Core.CommandBarPopup)menuBar.Controls.Add(Microsoft.Office.Core.MsoControlType.msoControlPopup,
- missing, missing, missing, missing);
- }
- }
- //添加单独工具栏
- public void AddToolItem(string strBarName, string strBtnName)
- {
- Microsoft.Office.Core.CommandBar toolBar = null;
- toolBar =
- (Microsoft.Office.Core.CommandBar)this.oWord.CommandBars.FindControl(Microsoft.Office.Core.MsoControlType.msoControlButton,
- missing, strBarName, true);
- if (toolBar == null)
- {
- toolBar =
- (Microsoft.Office.Core.CommandBar)this.oWord.CommandBars.Add(
- Microsoft.Office.Core.MsoControlType.msoControlButton,
- missing, missing, missing);
- toolBar.Name = strBtnName;
- toolBar.Visible = true;
- }
- }
- #endregion
- #region 移动光标位置
- // Go to a predefined bookmark, if the bookmark doesn't exists
- the application will raise an error
- public void GotoBookMark(string strBookMarkName)
- {
- // VB : Selection.GoTo What:=wdGoToBookmark, Name:="nome"
- object Bookmark =
- (int)Microsoft.Office.Interop.Word.WdGoToItem.wdGoToBookmark;
- object NameBookMark = strBookMarkName;
- oWord.Selection.GoTo(ref Bookmark, ref missing, ref missing,
- ref NameBookMark);
- }
- public void GoToTheEnd()
- {
- // VB : Selection.EndKey Unit:=wdStory
- object unit;
- unit = Microsoft.Office.Interop.Word.WdUnits.wdStory;
- oWord.Selection.EndKey(ref unit, ref missing);
- }
- public void GoToLineEnd()
- {
- object unit = Microsoft.Office.Interop.Word.WdUnits.wdLine;
- object ext =
- Microsoft.Office.Interop.Word.WdMovementType.wdExtend;
- oWord.Selection.EndKey(ref unit, ref ext);
- }
- public void GoToTheBeginning()
- {
- // VB : Selection.HomeKey Unit:=wdStory
- object unit;
- unit = Microsoft.Office.Interop.Word.WdUnits.wdStory;
- oWord.Selection.HomeKey(ref unit, ref missing);
- }
- public void GoToTheTable(int ntable)
- {
- // Selection.GoTo What:=wdGoToTable, Which:=wdGoToFirst,
- Count:=, Name:=""
- // Selection.Find.ClearFormatting
- // With Selection.Find
- // .Text = ""
- // .Replacement.Text = ""
- // .Forward = True
- // .Wrap = wdFindContinue
- // .Format = False
- // .MatchCase = False
- // .MatchWholeWord = False
- // .MatchWildcards = False
- // .MatchSoundsLike = False
- // .MatchAllWordForms = False
- // End With
- object what;
- what = Microsoft.Office.Interop.Word.WdUnits.wdTable;
- object which;
- which =
- Microsoft.Office.Interop.Word.WdGoToDirection.wdGoToFirst;
- object count;
- count = ;
- oWord.Selection.GoTo(ref what, ref which, ref count, ref
- missing);
- oWord.Selection.Find.ClearFormatting();
- oWord.Selection.Text = "";
- }
- public void GoToRightCell()
- {
- // Selection.MoveRight Unit:=wdCell
- object direction;
- direction = Microsoft.Office.Interop.Word.WdUnits.wdCell;
- oWord.Selection.MoveRight(ref direction, ref missing, ref
- missing);
- }
- public void GoToLeftCell()
- {
- // Selection.MoveRight Unit:=wdCell
- object direction;
- direction = Microsoft.Office.Interop.Word.WdUnits.wdCell;
- oWord.Selection.MoveLeft(ref direction, ref missing, ref
- missing);
- }
- public void GoToDownCell()
- {
- // Selection.MoveRight Unit:=wdCell
- object direction;
- direction = Microsoft.Office.Interop.Word.WdUnits.wdLine;
- oWord.Selection.MoveDown(ref direction, ref missing, ref
- missing);
- }
- public void GoToUpCell()
- {
- // Selection.MoveRight Unit:=wdCell
- object direction;
- direction = Microsoft.Office.Interop.Word.WdUnits.wdLine;
- oWord.Selection.MoveUp(ref direction, ref missing, ref
- missing);
- }
- #endregion
- #region - 插入操作 -
- public void InsertText(string strText) //插入文本
- {
- oWord.Selection.TypeText(strText);
- }
- public void InsertLineBreak() //插入换行符
- {
- oWord.Selection.TypeParagraph();
- }
- /// <summary>
- /// 插入多个空行
- /// </summary>
- /// <param name="nline"></param>
- public void InsertLineBreak(int nline)
- {
- for (int i = ; i < nline; i++)
- oWord.Selection.TypeParagraph();
- }
- public void InsertPagebreak() //插入分页符
- {
- // VB : Selection.InsertBreak Type:=wdPageBreak
- object pBreak =
- (int)Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak;
- oWord.Selection.InsertBreak(ref pBreak);
- }
- // 插入页码
- public void InsertPageNumber() //在正文中插入页码
- {
- object wdFieldPage =
- Microsoft.Office.Interop.Word.WdFieldType.wdFieldPage;
- object preserveFormatting = true;
- oWord.Selection.Fields.Add(oWord.Selection.Range, ref
- wdFieldPage, ref missing, ref preserveFormatting);
- }
- // 插入页码
- public void InsertPageNumber(string strAlign)
- {
- object wdFieldPage =
- Microsoft.Office.Interop.Word.WdFieldType.wdFieldPage;
- object preserveFormatting = true;
- oWord.Selection.Fields.Add(oWord.Selection.Range, ref
- wdFieldPage, ref missing, ref preserveFormatting);
- SetAlignment(strAlign);
- }
- #region - 插入页脚 -
- public bool InsertPageFooter(string text)
- {
- try
- {
- oWord.ActiveWindow.View.SeekView =
- Microsoft.Office.Interop.Word.WdSeekView.wdSeekCurrentPageFooter;//页脚
- oWord.Selection.InsertAfter(text); //.InsertAfter(text);
- return true;
- }
- catch (Exception)
- {
- return false;
- }
- }
- public bool InsertPageHeader(string text)
- {
- try
- {
- oWord.ActiveWindow.View.SeekView =
- Microsoft.Office.Interop.Word.WdSeekView.wdSeekCurrentPageHeader;//页眉
- oWord.Selection.InsertAfter(text);
- return true;
- }
- catch (Exception)
- {
- return false;
- }
- }
- public bool InsertPageFooterNumber()
- {
- try
- {
- oWord.ActiveWindow.View.SeekView =
- Microsoft.Office.Interop.Word.WdSeekView.wdSeekCurrentPageHeader; //页眉
- oWord.Selection.WholeStory();
- oWord.Selection.ParagraphFormat.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderBottom].LineStyle
- = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleNone; //取消页眉的下划线
- oWord.ActiveWindow.View.SeekView =
- Microsoft.Office.Interop.Word.WdSeekView.wdSeekMainDocument; //转到正文
- oWord.ActiveWindow.View.SeekView =
- Microsoft.Office.Interop.Word.WdSeekView.wdSeekCurrentPageFooter;//页脚
- oWord.Selection.TypeText("第");
- object page =
- Microsoft.Office.Interop.Word.WdFieldType.wdFieldPage; //当前页码
- oWord.Selection.Fields.Add(oWord.Selection.Range, ref
- page, ref missing, ref missing);
- oWord.Selection.TypeText("页/共");
- object pages =
- Microsoft.Office.Interop.Word.WdFieldType.wdFieldNumPages; //总页码
- oWord.Selection.Fields.Add(oWord.Selection.Range, ref
- pages, ref missing, ref missing);
- oWord.Selection.TypeText("页");
- oWord.ActiveWindow.View.SeekView =
- Microsoft.Office.Interop.Word.WdSeekView.wdSeekMainDocument;
- return true;
- }
- catch (Exception)
- {
- return false;
- }
- }
- #endregion
- public void InsertLine(float left, float top, float width, float
- weight, int r, int g, int b)
- {
- //SetFontColor("red");
- //SetAlignment("Center");
- object Anchor = oWord.Selection.Range;
- //int pLeft = 0, pTop = 0, pWidth = 0, pHeight = 0;
- //oWord.ActiveWindow.GetPoint(out pLeft, out pTop, out
- pWidth, out pHeight,missing);
- //MessageBox.Show(pLeft + "," + pTop + "," + pWidth + "," +
- pHeight);
- object rep = false;
- //left += oWord.ActiveDocument.PageSetup.LeftMargin;
- left = oWord.CentimetersToPoints(left);
- top = oWord.CentimetersToPoints(top);
- width = oWord.CentimetersToPoints(width);
- Microsoft.Office.Interop.Word.Shape s =
- oWord.ActiveDocument.Shapes.AddLine(, top, width, top, ref Anchor);
- s.Line.ForeColor.RGB = RGB(r, g, b);
- s.Line.Visible = Microsoft.Office.Core.MsoTriState.msoTrue;
- s.Line.Style =
- Microsoft.Office.Core.MsoLineStyle.msoLineSingle;
- s.Line.Weight = weight;
- }
- #endregion
- #region - 插入图片 -
- public void InsertImage(string strPicPath, float picWidth, float
- picHeight)
- {
- string FileName = strPicPath;
- object LinkToFile = false;
- object SaveWithDocument = true;
- object Anchor = oWord.Selection.Range;
- oWord.ActiveDocument.InlineShapes.AddPicture(FileName, ref
- LinkToFile, ref SaveWithDocument, ref Anchor).Select();
- oWord.Selection.InlineShapes[].Width = picWidth; // 图片宽度
- oWord.Selection.InlineShapes[].Height = picHeight; // 图片高度
- }
- //public void InsertImage(string strPicPath, float picWidth,
- float picHeight, OwdWrapType owdWrapType)
- //{
- // string FileName = strPicPath;
- // object LinkToFile = false;
- // object SaveWithDocument = true;
- // object Anchor = oWord.Selection.Range;
- // oWord.ActiveDocument.InlineShapes.AddPicture(FileName, ref
- LinkToFile, ref SaveWithDocument, ref Anchor).Select();
- // oWord.Selection.InlineShapes[1].Width = picWidth; // 图片宽度
- // oWord.Selection.InlineShapes[1].Height = picHeight; //
- 图片高度
- // // 将图片设置为四面环绕型
- // // Microsoft.Office.Interop.Word.Shape s =
- oWord.Selection.InlineShapes[].ConvertToShape();
- // // s.WrapFormat.Type =
- Microsoft.Office.Interop.Word.WdWrapType.wdWrapNone; //wdWrapSquare
- 四周环绕型
- //}
- #endregion
- #region - 插入表格 -
- public bool InsertTable(DataTable dt, bool haveBorder, double[]
- colWidths)
- {
- try
- {
- object Nothing = System.Reflection.Missing.Value;
- int lenght = oDoc.Characters.Count - ;
- object start = lenght;
- object end = lenght;
- //表格起始坐标
- Microsoft.Office.Interop.Word.Range tableLocation =
- oDoc.Range(ref start, ref end);
- //添加Word表格
- Microsoft.Office.Interop.Word.Table table =
- oDoc.Tables.Add(tableLocation, dt.Rows.Count, dt.Columns.Count, ref
- Nothing, ref Nothing);
- if (colWidths != null)
- {
- for (int i = ; i < colWidths.Length; i++)
- {
- table.Columns[i + ].Width = (float)(28.5F *
- colWidths[i]);
- }
- }
- ///设置TABLE的样式
- table.Rows.HeightRule =
- Microsoft.Office.Interop.Word.WdRowHeightRule.wdRowHeightAtLeast;
- table.Rows.Height =
- oWord.CentimetersToPoints(float.Parse("0.8"));
- table.Range.Font.Size = 10.5F;
- table.Range.Font.Name = "宋体";
- table.Range.Font.Bold = ;
- table.Range.ParagraphFormat.Alignment =
- Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
- table.Range.Cells.VerticalAlignment =
- Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
- if (haveBorder == true)
- {
- //设置外框样式
- table.Borders.OutsideLineStyle =
- Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;
- table.Borders.InsideLineStyle =
- Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;
- //样式设置结束
- }
- for (int row = ; row < dt.Rows.Count; row++)
- {
- for (int col = ; col < dt.Columns.Count; col++)
- {
- table.Cell(row + , col + ).Range.Text =
- dt.Rows[row][col].ToString();
- }
- }
- return true;
- }
- catch (Exception e)
- {
- MessageBox.Show(e.ToString(), "错误提示",
- MessageBoxButtons.OK, MessageBoxIcon.Error);
- return false;
- }
- finally
- {
- }
- }
- public bool InsertTable(DataTable dt, bool haveBorder)
- {
- return InsertTable(dt, haveBorder, null);
- }
- public bool InsertTable(DataTable dt)
- {
- return InsertTable(dt, false, null);
- }
- //插入表格结束
- #endregion
- #region 设置样式
- /// <summary>
- /// Change the paragraph alignement
- /// </summary>
- /// <param name="strType"></param>
- public void SetAlignment(string strType)
- {
- switch (strType.ToLower())
- {
- case "center":
- oWord.Selection.ParagraphFormat.Alignment =
- Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
- break;
- case "left":
- oWord.Selection.ParagraphFormat.Alignment =
- Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;
- break;
- case "right":
- oWord.Selection.ParagraphFormat.Alignment =
- Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;
- break;
- case "justify":
- oWord.Selection.ParagraphFormat.Alignment =
- Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphJustify;
- break;
- }
- }
- // if you use thif function to change the font you should call
- it again with
- // no parameter in order to set the font without a particular
- format
- public void SetFont(string strType)
- {
- switch (strType)
- {
- case "Bold":
- oWord.Selection.Font.Bold = ;
- break;
- case "Italic":
- oWord.Selection.Font.Italic = ;
- break;
- case "Underlined":
- oWord.Selection.Font.Subscript = ;
- break;
- }
- }
- // disable all the style
- public void SetFont()
- {
- oWord.Selection.Font.Bold = ;
- oWord.Selection.Font.Italic = ;
- oWord.Selection.Font.Subscript = ;
- SetFontName("宋体"); //默认宋体,tjt
- SetFontSize(10.5f); //默认五号字体,tjt
- }
- public void SetFontName(string strType)
- {
- oWord.Selection.Font.Name = strType;
- }
- public void SetFontSize(float nSize)
- {
- SetFontSize(nSize, );
- }
- public void SetFontSize(float nSize, int scaling)
- {
- if (nSize > 0f)
- oWord.Selection.Font.Size = nSize;
- if (scaling > )
- oWord.Selection.Font.Scaling = scaling;
- }
- public void SetFontColor(string strFontColor)
- {
- switch (strFontColor.ToLower())
- {
- case "blue":
- oWord.Selection.Font.Color =
- Microsoft.Office.Interop.Word.WdColor.wdColorBlue;
- break;
- case "gold":
- oWord.Selection.Font.Color =
- Microsoft.Office.Interop.Word.WdColor.wdColorGold;
- break;
- case "gray":
- oWord.Selection.Font.Color =
- Microsoft.Office.Interop.Word.WdColor.wdColorGray875;
- break;
- case "green":
- oWord.Selection.Font.Color =
- Microsoft.Office.Interop.Word.WdColor.wdColorGreen;
- break;
- case "lightblue":
- oWord.Selection.Font.Color =
- Microsoft.Office.Interop.Word.WdColor.wdColorLightBlue;
- break;
- case "orange":
- oWord.Selection.Font.Color =
- Microsoft.Office.Interop.Word.WdColor.wdColorOrange;
- break;
- case "pink":
- oWord.Selection.Font.Color =
- Microsoft.Office.Interop.Word.WdColor.wdColorPink;
- break;
- case "red":
- oWord.Selection.Font.Color =
- Microsoft.Office.Interop.Word.WdColor.wdColorRed;
- break;
- case "yellow":
- oWord.Selection.Font.Color =
- Microsoft.Office.Interop.Word.WdColor.wdColorYellow;
- break;
- }
- }
- public void SetPageNumberAlign(string strType, bool bHeader)
- {
- object alignment;
- object bFirstPage = false;
- object bF = true;
- //if (bHeader == true)
- //WordApplic.Selection.HeaderFooter.PageNumbers.ShowFirstPageNumber =
- bF;
- switch (strType)
- {
- case "Center":
- alignment =
- Microsoft.Office.Interop.Word.WdPageNumberAlignment.wdAlignPageNumberCenter;
- //WordApplic.Selection.HeaderFooter.PageNumbers.Add(ref alignment,ref
- bFirstPage);
- //Microsoft.Office.Interop.Word.Selection
- objSelection = WordApplic.pSelection;
- oWord.Selection.HeaderFooter.PageNumbers[].Alignment =
- Microsoft.Office.Interop.Word.WdPageNumberAlignment.wdAlignPageNumberCenter;
- break;
- case "Right":
- alignment =
- Microsoft.Office.Interop.Word.WdPageNumberAlignment.wdAlignPageNumberRight;
- oWord.Selection.HeaderFooter.PageNumbers[].Alignment =
- Microsoft.Office.Interop.Word.WdPageNumberAlignment.wdAlignPageNumberRight;
- break;
- case "Left":
- alignment =
- Microsoft.Office.Interop.Word.WdPageNumberAlignment.wdAlignPageNumberLeft;
- oWord.Selection.HeaderFooter.PageNumbers.Add(ref
- alignment, ref bFirstPage);
- break;
- }
- }
- /// <summary>
- /// 设置页面为标准A4公文样式
- /// </summary>
- private void SetA4PageSetup()
- {
- oWord.ActiveDocument.PageSetup.TopMargin =
- oWord.CentimetersToPoints(3.7f);
- //oWord.ActiveDocument.PageSetup.BottomMargin =
- oWord.CentimetersToPoints(1f);
- oWord.ActiveDocument.PageSetup.LeftMargin =
- oWord.CentimetersToPoints(2.8f);
- oWord.ActiveDocument.PageSetup.RightMargin =
- oWord.CentimetersToPoints(2.6f);
- //oWord.ActiveDocument.PageSetup.HeaderDistance =
- oWord.CentimetersToPoints(2.5f);
- //oWord.ActiveDocument.PageSetup.FooterDistance =
- oWord.CentimetersToPoints(1f);
- oWord.ActiveDocument.PageSetup.PageWidth =
- oWord.CentimetersToPoints(21f);
- oWord.ActiveDocument.PageSetup.PageHeight =
- oWord.CentimetersToPoints(29.7f);
- }
- #endregion
- #region 替换
- ///<summary>
- /// 在word 中查找一个字符串直接替换所需要的文本
- /// </summary>
- /// <param name="strOldText">原文本</param>
- /// <param name="strNewText">新文本</param>
- /// <returns></returns>
- public bool Replace(string strOldText, string strNewText)
- {
- if (oDoc == null)
- oDoc = oWord.ActiveDocument;
- this.oDoc.Content.Find.Text = strOldText;
- object FindText, ReplaceWith, Replace;//
- FindText = strOldText;//要查找的文本
- ReplaceWith = strNewText;//替换文本
- Replace =
- Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;/**//*wdReplaceAll -
- 替换找到的所有项。
- * wdReplaceNone -
- 不替换找到的任何项。
- * wdReplaceOne -
- 替换找到的第一项。
- * */
- oDoc.Content.Find.ClearFormatting();//移除Find的搜索文本和段落格式设置
- if (oDoc.Content.Find.Execute(
- ref FindText, ref missing,
- ref missing, ref missing,
- ref missing, ref missing,
- ref missing, ref missing, ref missing,
- ref ReplaceWith, ref Replace,
- ref missing, ref missing,
- ref missing, ref missing))
- {
- return true;
- }
- return false;
- }
- public bool SearchReplace(string strOldText, string strNewText)
- {
- object replaceAll =
- Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;
- //首先清除任何现有的格式设置选项,然后设置搜索字符串 strOldText。
- oWord.Selection.Find.ClearFormatting();
- oWord.Selection.Find.Text = strOldText;
- oWord.Selection.Find.Replacement.ClearFormatting();
- oWord.Selection.Find.Replacement.Text = strNewText;
- if (oWord.Selection.Find.Execute(
- ref missing, ref missing, ref missing, ref missing, ref
- missing,
- ref missing, ref missing, ref missing, ref missing, ref
- missing,
- ref replaceAll, ref missing, ref missing, ref missing,
- ref missing))
- {
- return true;
- }
- return false;
- }
- #endregion
- #region - 表格操作 -
- public bool FindTable(string bookmarkTable)
- {
- try
- {
- object bkObj = bookmarkTable;
- if (oWord.ActiveDocument.Bookmarks.Exists(bookmarkTable)
- == true)
- {
- oWord.ActiveDocument.Bookmarks.get_Item(ref
- bkObj).Select();
- return true;
- }
- else
- return false;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public void MoveNextCell()
- {
- try
- {
- Object unit =
- Microsoft.Office.Interop.Word.WdUnits.wdCell;
- Object count = ;
- oWord.Selection.Move(ref unit, ref count);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public void SetCellValue(string value)
- {
- try
- {
- oWord.Selection.TypeText(value);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public void MoveNextRow()
- {
- try
- {
- Object extend =
- Microsoft.Office.Interop.Word.WdMovementType.wdExtend;
- Object unit =
- Microsoft.Office.Interop.Word.WdUnits.wdCell;
- Object count = ;
- oWord.Selection.MoveRight(ref unit, ref count, ref
- extend);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- //表格操作结束
- #endregion
- #region 填充书签
- /// <summary>
- /// 填充书签
- /// </summary>
- /// <param name="bookmark">书签</param>
- /// <param name="value">值</param>
- public void bookmarkReplace(string bookmark, string value)
- {
- try
- {
- object bkObj = bookmark;
- if (oWord.ActiveDocument.Bookmarks.Exists(bookmark) ==
- true)
- {
- oWord.ActiveDocument.Bookmarks.get_Item(ref
- bkObj).Select();
- }
- else return;
- oWord.Selection.TypeText(value);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- #endregion
- /// <summary>
- /// rgb转换函数
- /// </summary>
- /// <param name="r"></param>
- /// <param name="g"></param>
- /// <param name="b"></param>
- /// <returns></returns>
- int RGB(int r, int g, int b)
- {
- return ((b << ) | (ushort)(((ushort)g << ) |
- r));
- }
- Color RGBToColor(int color)
- {
- int r = 0xFF & color;
- int g = 0xFF00 & color;
- g >>= ;
- int b = 0xFF0000 & color;
- b >>= ;
- return Color.FromArgb(r, g, b);
- }
- }
- }
- /*
- (1) 插入图片后,如果后面不再插入内容,则图片会包含;如果继续插入内容,则图片会被程序删除。解决方法是:
- 插入图片后,执行跳转,光标转移到图片后面,再插入东西,就可以了。
- word.InsertImage("d://111.jpg",400.0f,300.0f); //插入图片
- word.GoToTheEnd();
- (2)
- oWord.ActiveWindow.View.SeekView =
- Microsoft.Office.Interop.Word.WdSeekView.wdSeekCurrentPageHeader; //页眉
- oWord.ActiveWindow.View.SeekView =
- Microsoft.Office.Interop.Word.WdSeekView.wdSeekCurrentPageFooter; //页脚
- oWord.ActiveWindow.View.SeekView =
- Microsoft.Office.Interop.Word.WdSeekView.wdSeekMainDocument; //转到正文
- object page = Microsoft.Office.Interop.Word.WdFieldType.wdFieldPage;
- //当前页码
- object pages =
- Microsoft.Office.Interop.Word.WdFieldType.wdFieldNumPages; //总页码
- *
- */
示例程序,由于用到了静态类等,直接拷贝可能不能运行:
引入命名空间:using WordAddinSample;
几段代码:
private void button1_Click(object sender, EventArgs e)
{
WordHelp word = new WordHelp();
word.Open();
// word.InsertPageNumber("center"); 插入页码,但注意只是插入到正文里,不是真正的页码位置
// word.LoadDotFile("d://ESTemplate.doc");
// word.CreateWordDocument("d://ab.doc", false); //打开已有文件
word.SetPageNumberAlign("center",true); //
word.InsertText("白龙矿反馈设计报告1111"); //插入文本
word.SetAlignment("center"); //居中
word.InsertLineBreak(); //换行,参数为行数,例为换5行
word.SetFont("bold"); //只有三个值bold, Italic Underlined
word.SetFontSize(); //大小
word.SetFontName("黑体"); //字体样式
word.InsertText("白龙矿反馈设计报告"); //插入文本 word.SetFont(); //清空字体格式,恢复默认
word.InsertLineBreak();
word.InsertText("美丽的矿大校园--字体已恢复默认");
word.InsertImage("d://111.jpg",400.0f,300.0f); //插入图片
word.InsertPagebreak(); //分页符
word.InsertText("分页测试2");
word.InsertLineBreak();
word.InsertText("插入表格");
word.InsertLineBreak();
DataTable storedt = new DataTable(); // Data 数据空间
storedt.Columns.Add("Book_ISBN");
storedt.Columns.Add("Book_Name");
storedt.Columns.Add("Store_Num");
storedt.Columns.Add("CanBorrow_Num");
storedt.Columns.Add("InShop_Num");
storedt.Columns.Add("OutShop_Num");
storedt.Rows.Add("", "", "", "", "", "");
storedt.Rows.Add("", "", "", "", "", "");
storedt.Rows.Add("", "", "", "", "", "");
storedt.Rows.Add("", "", "", "", "", "");
storedt.Rows.Add("", "", "", "", "", "");
storedt.Rows.Add("", "", "", "", "", "");
word.InsertTable(storedt);
word.InsertPageHeader("我是页眉"); //插入页眉
word.InsertPageFooter("我是页脚"); //插入页脚
word.InsertPageFooterNumber(); // 第*页/共*页
word.GoToTheEnd();
word.SaveAs("d://c.doc");
word.Quit();
}
private void button2_Click(object sender, EventArgs e) //加载模版
{
WordHelp word = new WordHelp();
// word.Open(); //先创建个对象
word.LoadDotFile("d://现代型报告.dot");
// word.LoadDotFile("d://现代型报告.dot"); //加载模版
word.InsertText("huhu");
word.SaveAs("d://temp.doc");
word.Quit();
}
private void button3_Click(object sender, EventArgs e) //打开Word
{
WordHelp word = new WordHelp();
word.CreateWordDocument("d://c.doc", false); //打开已有文件
word.GoToTheEnd();
word.InsertText("我是打开已有文档新添加的文本内容");
word.Save();
word.Quit();
}
private void button4_Click(object sender, EventArgs e)
{
//静态变量赋值,测试用
setParas();
// word.Open(); //先创建个对象
WordHelp word = new WordHelp();
//加载模版
word.LoadDotFile("d://HDTemplate.dot"); //首页
word.SetAlignment("center"); //居中
word.SetFont("bold"); //只有三个值bold, Italic Underlined
word.SetFontSize(26.25f); //大小 26.25对应 一号
word.SetFontName("黑体"); //字体样式
word.InsertLineBreak(); //换行,参数为行数,例为换5行
word.InsertText(ClassParas.MineName); //插入文本
word.InsertLineBreak();
word.InsertText("反馈设计报告");
word.InsertLineBreak();
word.SetFontSize();
word.InsertText(ClassParas.CompanyName);
word.InsertLineBreak();
word.InsertText("中国矿业大学");
word.InsertLineBreak();
word.InsertText(DateTime.Now.ToShortDateString());
word.InsertLineBreak(); //保存
word.SaveAs("d://temp.doc");
word.Quit();
}
private void setParas(){
ClassParas.MineName = "白龙矿";
ClassParas.CompanyName = "山东新汶矿业集团";
}
private void button5_Click(object sender, EventArgs e) //书签替换
{
// word.Open(); //先创建个对象
WordHelp word = new WordHelp();
//加载模版
word.LoadDotFile("d://Bookmark.dot");
word.GotoBookMark("矿名"); //光标移动到书签"矿名"处
word.bookmarkReplace("矿名","金属矿"); //书签替换
word.bookmarkReplace("公司名","我的公司");
if (word.FindTable("引用表"))
{
// 第1行数据
word.MoveNextRow();
word.SetCellValue("");
word.MoveNextCell();
word.SetCellValue("HP电脑");
word.MoveNextCell();
word.SetCellValue("台");
word.MoveNextCell();
word.SetCellValue("");
word.MoveNextCell();
word.SetCellValue("250,000");
// 第2行数据
word.MoveNextRow();
word.SetCellValue("");
word.MoveNextCell();
word.SetCellValue("DELL笔记本");
word.MoveNextCell();
word.SetCellValue("台");
word.MoveNextCell();
word.SetCellValue("");
word.MoveNextCell();
word.SetCellValue("40,000");
}
非常好的几篇参考文章:
功能全面的一个类,本文中的类即在该类的基础上修改而来
添加文本、表格、换页
用C#编程修改Word模版
利用模版新建文档,书签的使用,利用书签定位,表格的操作
操作Word的辅助类(word2003)的更多相关文章
- C#操作Word的辅助类(word2003) 修改完善版
转自:http://blog.csdn.net/jiutao_tang/article/details/6567608 该类在他人编写的几个类基础上扩展完善而来,主要功能有: (1)插入文本 (2)插 ...
- c# 操作Word总结(车)
在医疗管理系统中为保存患者的体检和治疗记录,方便以后的医生或其他人查看.当把数据保存到数据库中,需要新建很多的字段,而且操作很繁琐,于是想到网页的信息创建到一个word文本中,在显示的时,可以在线打开 ...
- c# 操作Word总结【转】
http://www.cnblogs.com/eye-like/p/4121219.html 在医疗管理系统中为保存患者的体检和治疗记录,方便以后的医生或其他人查看.当把数据保存到数据库中,需要新建很 ...
- VC+++ 操作word
最近完成了一个使用VC++ 操作word生成扫描报告的功能,在这里将过程记录下来,开发环境为visual studio 2008 导入接口 首先在创建的MFC项目中引入word相关组件 右键点击 项目 ...
- c# 操作Word总结
在医疗管理系统中为保存患者的体检和治疗记录,方便以后的医生或其他人查看.当把数据保存到数据库中,需要新建很多的字段,而且操作很繁琐,于是想到网页的信息创建到一个word文本中,在显示的时,可以在线打开 ...
- [转] c# 操作Word
来自 风过四季天 的原文 c# 操作Word总结 在医疗管理系统中为保存患者的体检和治疗记录,方便以后的医生或其他人查看.当把数据保存到数据库中,需要新建很多的字段,而且操作很繁琐,于是想 到网页的信 ...
- 最直观的poi的使用帮助(告诉你怎么使用poi的官网),操作word,excel,ppt
最直观的poi的使用帮助(告诉你怎么使用poi的官网),poi操作word,excel,ppt 写在最前面 其实poi的官网上面有poi的各种类和接口的使用说明,还有非常详细的样例,所以照着这些样例来 ...
- python操作word入门
1.安装pywin32 http://sourceforge.net/projects/pywin32 在files里去找适合你的python版本.截止此文,最新版本是pywin32-219快捷路径: ...
- C#中操作Word(1)—— word对象模型介绍
一.开发环境布置 C#中添加对Word的支持,只需添加对Microsoft.Office.Interop.Word的命名空间,如下图所示,右键点击“引用”,在弹出的“添加引用”对话框中选中COM标签页 ...
随机推荐
- android 2.3.3 配置github的两步骤
第一步:配置GitHub的总账号(Version Control) 第二步:配置具体的仓库(仓库名称你从GitHub网上添加)
- directshow 虚拟摄像头 实例 代码解读
directshow 虚拟摄像头 实例 代码解读 本文只介绍这个源码的大致构成以及怎么修改,因为其他的我也不会啊哈哈哈,我就是用QQ调用虚拟摄像头读取我自己的视频或者图片播放给别人让别人以为这就是实时 ...
- SVN用命令行更换本地副本IP地址
1.运行svn info现连的svn信息 2.运行svn switch --relocate https://原来的地址 https://改过后的地址. svn help switch 查看switc ...
- Unsupported compiler 'com.apple.compilers.llvmgcc42' selected for architecture 'armv7' Xcode5
刚刚将Xcode更新到Xcode5,一运行报如下错误: Unsupported compiler 'com.apple.compilers.llvmgcc42' selected for archit ...
- shell编程——sed用法
一.sed格式: sed 参数 '正则表达式' 文件名 演示文件的内容: [root@localhost ~]# cat test.sh #!/bin/bash 第一行 12345! 第二行 2345 ...
- MySQL内置功能之视图、触发器和存储过程
主要内容: 一.视图 二.触发器 三.存储过程 1️⃣ 视图 一.关于视图的理解 1.1.何谓视图? 视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名], 用户使 ...
- Python OrderedDict使用
一.最近最少使用实现: import collections class LRUDict(object): ''' 最近最少使用队列实现,最近使用的键值放后面 ''' def __init__(sel ...
- Linux实战教学笔记33:lvs+keepalived集群架构服务
一,LVS功能详解 1.1 LVS(Linux Virtual Server)介绍 LVS是Linux Virtual Server 的简写(也叫做IPVS),意即Linux虚拟服务器,是一个虚拟的服 ...
- IT 360服务器监控
- 在zookeeper集群的基础上,搭建伪solrCloud集群
伪集群的搭建:将solrCloud搭建到同一台机器上. 准备工作 1 将在window中部署的单机版solr上传到服务器(虚拟机)中 solr的简单部署:在tomcat中启动slor 的内容 这一次放 ...