C#操作Word的辅助类(word2003) 修改完善版
转自:http://blog.csdn.net/jiutao_tang/article/details/6567608
该类在他人编写的几个类基础上扩展完善而来,主要功能有:
(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模版
利用模版新建文档,书签的使用,利用书签定位,表格的操作
C#操作Word的辅助类(word2003) 修改完善版的更多相关文章
- 操作Word的辅助类(word2003)
该类在他人编写的几个类基础上扩展完善而来,主要功能有: (1)插入文本 (2)插入图片 (3)插入表格 (4)载入模版 (5)编辑模版,利用标签等 (6)插入页眉页脚 /*************** ...
- C# /VB.NET操作Word批注(一)—— 插入、修改、删除Word批注
批注内容可以是对某段文字或内容的注释,也可以是对文段中心思想的概括提要,或者是对文章内容的评判.疑问,以及在阅读时给自己或他人起到提示作用.本篇文章中将介绍如何在C#中操作Word批注,主要包含以下要 ...
- VC+++ 操作word
最近完成了一个使用VC++ 操作word生成扫描报告的功能,在这里将过程记录下来,开发环境为visual studio 2008 导入接口 首先在创建的MFC项目中引入word相关组件 右键点击 项目 ...
- C#操作Word的超详细总结
本文中用C#来操作Word,包括: 创建Word: 插入文字,选择文字,编辑文字的字号.粗细.颜色.下划线等: 设置段落的首行缩进.行距: 设置页面页边距和纸张大小: 设置页眉.页码: 插入图片,设置 ...
- OpenXML操作word
OpenXML概述 项目中经常需要操作word,之前的方式是采用COM接口,这个接口很不稳定,经常报错.现在开始采用OpenXML.OpenXML(OOXML)是微软在Office 2007中提出的一 ...
- OpenXml操作Word的一些操作总结.
OpenXml操作Word的一些操作总结. OpenXml相对于用MS提供的COM组件来生成WORD,有如下优势: 1.相对于MS 的COM组件,因为版本带来的不兼容问题,及各种会生成WORD半途会崩 ...
- C# 操作Word 中的OLE——插入、编辑、读取 OLE
概述 OLE,Object Linking and Embedding,即对象连接与嵌入.我们在设计程序时,OLE可以用来创建复合文档,把文字.声音.图像.表格.应用程序等类型的信息组合在一起,在Wo ...
- C#/VB.NET 操作Word批注(二)——如何插入图片、读取、回复Word批注内容
序 在前面的文章C# 如何插入.修改.删除Word批注一文中介绍了如何操作Word批注的一些方法,在本篇文章中继续介绍操作Word批注的方法.分以下三种情况来介绍: 1. 插入图片到Word批注 2. ...
- 转发:VB程序操作word表格(文字、图片)
很多人都知道,用vb操作excel的表格非常简单,但是偏偏项目中碰到了VB操作word表格的部分,google.baidu搜爆了,都没有找到我需要的东西.到是搜索到了很多问这个问题的记录.没办法,索性 ...
随机推荐
- 浏览器html页面乱码问题分析
直接访问某html文件,浏览器显示编码是正常的,页面通过<meta charset="UTF-8">指定了编码方式,该文件存储编码也是utf8. 通过配置的org.sp ...
- 初学jquery,自己写的一个jquery幻灯片,代码有些笨拙,希望有大神可以指点一二,精简一下代码
html代码 <div class="picCon"> <div class="bigPic"> <ul> <li c ...
- 查看Msi文件内容
1通过msiexec命令解压msi包 msiexec.exe /a c:\msi\installer.msi /qb targetdir=d:\msi\installer 2 使用Orca查看. Or ...
- mysql索引的使用[下]
接着上篇,我们继续来探究索引.这次我们主要来探究关于联合索引的使用和联合.多表查询的规范. 继续看一下数据: mysql> select * from student order by ID d ...
- mysql索引的使用[上]
数据库的explain关键字和联合索引优化: 本篇文章简单的说一下mysql查询的优化以及explain语句的使用.(新手向) 因为这篇文章是面向查询的,直观一点,首先我们创建一个表:student ...
- C#微信公众号接口开发实例-高级接口-申请带参数的二维码
最近公司涉及到微信绑定用户,做了高级接口-申请带参数的二维码,总结了下微信开发接口.微信接口开发都是除了消息用的xml 回复基本上都是用json的形式传递信息(post/get),开发的方法基本都是一 ...
- 转: jdbc连接数据库需要注意和出错的地方
* 1.数据库登录模式不能只使用windows登录模式,要采取混合模式登录, * 并记住相应的密码和账户: * 2.连接数据库后一定要记得关闭资源,否则就会造成资源浪费. * 关闭的时候也要注意顺序, ...
- OC基础--类
都是注释哈 类的组成: C语言中函数分为声明和实现 OC中定义一个类也分为声明和实现, 也就是说以后我们在OC中定义类, 就是在写类的声明和实现 编写类的声明和实现: 声明 .h: /* 行为方法: ...
- CsvReader,CsvWriter的使用以及解决中文乱码
public void Csv(){ try { String[] stringList; String sourceFilePath = "D:\\111\\前海自身.csv"; ...
- 微信第三方 授权方账户信息 API文档错误
获取授权方的账户信息 该API用于获取授权方的公众号基本信息,包括头像.昵称.帐号类型.认证类型.微信号.原始ID和二维码图片URL. 需要特别记录授权方的帐号类型,在消息及事件推送时,对于不具备客服 ...