对Word进行各种操作
来源:互联网 发布:mac 10.8.2能玩魔兽 编辑:程序博客网 时间:2024/05/17 08:31
添加现有文件,DocManager.cs
在原文件的基础上新添加了:
1、设置单元格的边框样式
2、设置表格行、列的边框样
3、单元格内容垂直居中、靠上、靠下
4、按行合并单元格
5、按列合并单元格
---------------------------------------------------文件内容---------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Word;
using System.Data;
namespace FluidDesign
{
/// <summary>
/// IDocManager 接口。
/// </summary>
public interface IDocManager
{
/// <summary>
/// 返回WORD对象
/// </summary>
Word.Application Application
{
get;
}
/// <summary>
/// 返回DOC对象
/// </summary>
Word.Document Document
{
get;
}
/// <summary>
/// 新建空白文档
/// </summary>
void Add();
/// <summary>
/// 从模板文件中新建文档
/// </summary>
/// <param name="TemplateFileName">模板文件路径及文件名</param>
void Add(string templateFileName);
/// <summary>
/// 以只读的方式从模板文件中新建文档
/// </summary>
void AddByReadOnly(string TemplateFileName);
/// <summary>
/// 打开WORD文件
/// </summary>
/// <param name="strFileName">文件名</param>
void Open(string fileName);
/// <summary>
/// 以只读的方式打开WORD文件
/// </summary>
/// <param name="strFileName">文件名</param>
void Open(string fileName, bool isReadOnly);
/// <summary>
/// 当前文档章节数,包含空行了章节数,行数,页数,段落数,字数
/// </summary>
/// <returns>字符串数组</returns>
string[] GetFileInfo();
/// <summary>
/// 在文档空白的地方添加文字内容
/// </summary>
/// <param name="WordText">文本内容</param>
void AppendText(string wordText);
/// <summary>
/// 插入空行
/// </summary>
void InsertBlankLine();
/// <summary>
/// 插入指定行数的空行
/// </summary>
/// <param name="LineNumber">行数</param>
void InsertBlankLine(int lineNumber);
/// <summary>
/// 添加分页符
/// </summary>
void InsertPageBreak();
/// <summary>
/// 插入表格
/// </summary>
/// <param name="table">表格数据</param>
void InsertTable(ref System.Data.DataTable table);
/// <summary>
/// 设置表格的边界样式
/// </summary>
/// <param name="itable"></param>
/// <param name="LineStyle"></param>
void SetTableBorderStyle(int itable, Word.WdLineStyle LineStyle);
/// <summary>
/// 设置单元格边界样式
/// </summary>
/// <param name="LineStyle"></param>
void SetCellBorderStyle(int itable, int row, int col, Word.WdBorderType WdBorderType, Word.WdLineStyle LineStyle);
/// 设置表格行边界样式
/// </summary>
/// <param name="LineStyle"></param>
///
void SetRowBorderStyle(int itable, int row, Word.WdBorderType WdBorderType, Word.WdLineStyle LineStyle);
/// <summary>
/// 设置表格列边界样式
/// </summary>
/// <param name="LineStyle"></param>
void SetColBorderStyle(int itable, int col, Word.WdBorderType WdBorderType, Word.WdLineStyle LineStyle);
/// <summary>
/// 设置表格的字体大小
/// </summary>
/// <param name="itable"></param>
/// <param name="font"></param>
void SetTableFontSize(int itable, float Size);
/// <summary>
/// 设置单元格字体对齐方式,垂直,靠上
/// </summary>
/// <param name="LineStyle"></param>
void SetCellAlignmentStyle(int itable, int row, int col, Word.WdCellVerticalAlignment wdVerticalAlignment);
//合并单元格 从第RowIndex行 ,第ColumnIndex列的单元格开始,合并Length个单元格
void MergeColumCell(int itable, int RowIndex, int ColumnIndex, int Lenght);
//按行合并单元格 从第RowIndex行 ,第ColumnIndex列的单元格开始,合并Length个单元格
void MergeRowCell(int itable, int RowIndex, int ColumnIndex, int Lenght);
/// <summary>
/// 设置页眉
/// </summary>
/// <param name="text">放置的文本</param>
void SetPageHeader(string text);
/// <summary>
/// 设置页脚
/// </summary>
/// <param name="text">放置的文本</param>
void SetPageFooter(string text);
/// <summary>
/// 设定对齐
/// </summary>
/// <param name="AlignmentType">对齐方式</param>
void SetAlignment(string alignmentType);
/// <summary>
/// 设置字体名称
/// </summary>
/// <param name="FontName">字体名称</param>
void SetFontName(string fontName);
/// <summary>
/// 设置字体风格
/// </summary>
/// <param name="FontStyle">字体风格</param>
void SetFontStyle(string fontStyle);
/// <summary>
/// 取消字体风格
/// </summary>
void DisabledFontStyle();
/// <summary>
/// 设定字号
/// </summary>
/// <param name="Size">字号</param>
void SetFontSize(int size);
/// <summary>
/// 设置颜色
/// </summary>
/// <param name="Color"></param>
void SetFontColor(Word.WdColor color);
/// <summary>
/// 选择段落文本
/// </summary>
/// <param name="Para">段落</param>
void SelectRange(int para);
/// <summary>
/// 选择页眉
/// </summary>
void SelectPageHeader();
/// <summary>
/// 选择页脚
/// </summary>
void SelectPageFooter();
/// <summary>
/// 设置页面切换到主页面
/// </summary>
void SetDocSeekViewToMainDocument();
/// <summary>
/// 选择文本
/// </summary>
/// <param name="para">段落</param>
/// <param name="Sentences">句子</param>
void SelectRange(int para, int sentences);
/// <summary>
/// 选择文本
/// </summary>
/// <param name="start">开始处</param>
/// <param name="end">结束处</param>
void SelectRange(long start, long end);
/// <summary>
/// 转到书签
/// </summary>
/// <param name="BookMark">书签名称</param>
void FindBookMark(string bookMark);
/// <summary>
/// 判断书签是否存在
/// </summary>
/// <param name="BookMark">书签名称</param>
/// <returns>true Or false</returns>
bool BookMarkExists(string bookMark);
/// <summary>
/// 修改标签内容
/// </summary>
void setRangleText(Word.Bookmark bm, string text);
/// <summary>
/// 转到文档开头
/// </summary>
void GotoDocBegin();
/// <summary>
/// 转到文档结尾
/// </summary>
void GotoDocEnd();
/// <summary>
/// 转到表格
/// </summary>
/// <param name="ntable"></param>
void GoToTheTable(int ntable);
/// <summary>
/// 转到某个表格的某个单元格
/// </summary>
/// <param name="nTable">表格序号</param>
/// <param name="nRow">行号</param>
/// <param name="nColumn">列号</param>
void GotoTableCell(int nTable, int nRow, int nColumn);
/// <summary>
/// 某个表格的某个单元格的值
/// </summary>
/// <param name="nTable"></param>
/// <param name="nRow"></param>
/// <param name="nColumn"></param>
/// <returns></returns>
string GetTableCellValue(int nTable, int nRow, int nColumn);
/// <summary>
/// 转到右边单元格
/// </summary>
void GoToRightCell();
/// <summary>
/// 转到左边单元格
/// </summary>
void GoToLeftCell();
/// <summary>
/// 表格中转到下面的单元格
/// </summary>
void GoToDownCell();
/// <summary>
/// 表格中转到上面的单元格
/// </summary>
void GoToUpCell();
/// <summary>
/// 插入图片到WORD文档中
/// </summary>
/// <param name="FileName"></param>
void InsertPicture(string FileName);
/// <summary>
/// 插入WORD文件
/// </summary>
/// <param name="FileName">文件名称</param>
void InsertWordFile(string fileName);
/// <summary>
/// 填充单元格
/// </summary>
/// <param name="FileName">文件名称</param>
void FillRow(int itable, int Row, int Cell, string str);
/// <summary>
/// 添加单元格
/// </summary>
/// <param name="itable">表格索引</param>
/// /// <param name="irow">生成多少行</param>
void AddRow(int itable,int irow);
/// <summary>
/// 另存文件
/// </summary>
/// <param name="fileName"></param>
void SavaAs(object path);
/// <summary>
/// 删除文件
/// </summary>
/// <param name="AbsoluteFilePath">文件名</param>
void FileDelete(string absoluteFilePath);
/// <summary>
/// 杀除所有WINWORD进程
/// </summary>
void KillWinWord();
/// <summary>
/// 关闭文档
/// </summary>
void CloseWordAndDoc();
/// <summary>
/// 添加文体
/// </summary>
/// <param name="WordText">文本</param>
/// <param name="font">字体</param>
/// <param name="size">大小</param>
/// <param name="bold">风格</param>
void AppendText(string WordText, string font, int size, string bold);
/**/
/// <summary>
/// 段落的对齐方式
/// 例如:word.SetAlignment(Word.WdParagraphAlignment.wdAlignParagraphCenter)
/// </summary>
/// <param name="alignment"></param>
// void SetAlignment(Word.WdParagraphAlignment alignment);
/// <summary>
/// 首行缩进
/// </summary>
/// <param name="fltCount">float类型的数值</param>
void SetFirstLineIndent(float fltCount);
/// <summary>
/// 左缩进
/// </summary>
/// <param name="fltCount">float类型的数值</param>
void SetLeftIndent(float fltCount);
/// <summary>
/// 右缩进
/// </summary>
/// <param name="fltCount">float类型的数值</param>
void SetRightIndent(float fltCount);
/// <summary>
/// 行距
/// </summary>
/// <param name="spacePoint"></param>
void SetLineSpacingRule(int spacePoint);
/// <summary>
/// 段后据
/// </summary>
/// <param name="space"></param>
void SetSpaceAfter(float space);
/// <summary>
/// 段前据
/// </summary>
/// <param name="space"></param>
void SetSpaceBefore(float space);
/// <summary>
/// 标题级别
/// </summary>
/// <param name="level"> 标题级别</param>
void InsertParaTitle(int level);
/// <summary>
/// 更新目录
/// </summary>
void UpdateCatagory(float fontsize);
}
public class DocManager : IDocManager
{
/// <summary>
/// 页面方向
/// </summary>
///
#region 变量声明
public enum Orientation
{
Landscape,//横向
Portrait//纵向
}
public Word.Application _oWord;
public Word.Document _oDoc = null;
private object _oMissing = System.Reflection.Missing.Value;
private bool _IsDispose = false;
/// <summary>
/// 页面方式
/// </summary>
///
private Orientation _setorientation = Orientation.Portrait;
#endregion
#region 初始化
public DocManager()
{
try
{
_oWord = new Word.Application();
}
catch{
}
}
/// <summary>
/// 垃圾回收
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// 添对象
/// </summary>
/// <param name="o"></param>
private void NAR(object o)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(o);
}
catch {}
finally
{
o = null;
}
}
/// <summary>
/// 释放资源
/// </summary>
/// <param name="disposing"></param>
protected virtual void Dispose(bool disposing)
{
if(!this._IsDispose)
{
// If disposing equals true, dispose all managed
// and unmanaged resources.
if(disposing)
{
if(_oDoc != null)
_oDoc.Close(ref _oMissing,ref _oMissing,ref _oMissing);
if(_oWord != null)
_oWord.Application.Quit(ref _oMissing,ref _oMissing,ref _oMissing);
if(_oDoc != null)
NAR(_oDoc);
if(_oWord != null)
NAR(_oWord);
_oDoc = null;
_oWord = null;
GC.Collect();
}
}
_IsDispose = true;
}
~DocManager()
{
Dispose(false);
}
/// <summary>
/// 返回DOC对象
/// </summary>
public Word.Document Document
{
get
{
return this._oDoc;
}
}
/// <summary>
/// 返回WORD对象
/// </summary>
public Word.Application Application
{
get
{
return this._oWord;
}
}
#endregion
#region 新建和打开文件
/// <summary>
/// 新建空白文档
/// </summary>
public void Add()
{
Word.Documents _oDocs = _oWord.Documents;
_oDoc = _oDocs.Add(ref _oMissing, ref _oMissing, ref _oMissing, ref _oMissing);
if (Orientation.Landscape == _setorientation) //横向
{
_oDoc.PageSetup.Orientation = Word.WdOrientation.wdOrientLandscape;
}
NAR(_oDocs);
}
/// <summary>
/// 从模板文件中新建文档
/// </summary>
public void Add(string TemplateFileName)
{
Word.Documents _oDocs = _oWord.Documents;
object fileName = TemplateFileName;
_oDoc = _oDocs.Add(ref fileName, ref _oMissing, ref _oMissing, ref _oMissing);
_oDoc.Activate();
if(Orientation.Landscape == _setorientation) //横向
{
_oDoc.PageSetup.Orientation = Word.WdOrientation.wdOrientLandscape;
}
NAR(_oDocs);
_oWord.Visible = true;
}
/// <summary>
/// 以只读的方式从模板文件中新建文档
/// </summary>
public void AddByReadOnly(string TemplateFileName)
{
object fileName = TemplateFileName;
object readOnly = true;
object isVisible = true;
Word.Documents _oDocs = _oWord.Documents;
_oDoc = _oDocs.Open(ref fileName, ref _oMissing, ref readOnly, ref _oMissing,
ref _oMissing, ref _oMissing, ref _oMissing, ref _oMissing, ref _oMissing,
ref _oMissing, ref _oMissing, ref isVisible, ref _oMissing, ref _oMissing,
ref _oMissing, ref _oMissing);
_oDoc.Activate();
if (Orientation.Landscape == _setorientation) //横向
{
_oDoc.PageSetup.Orientation = Word.WdOrientation.wdOrientLandscape;
}
NAR(_oDocs);
_oWord.Visible = true;
}
/// <summary>
/// 打开WORD文件
/// </summary>
/// <param name="strFileName">文件名</param>
public void Open(string FileName)
{
object fileName = FileName;
object readOnly = false;
object isVisible = true;
try
{
Word.Documents _oDocs = _oWord.Documents;
//打开文档
_oDoc = _oDocs.Open(ref fileName, ref _oMissing, ref readOnly, ref _oMissing,
ref _oMissing, ref _oMissing, ref _oMissing, ref _oMissing, ref _oMissing,
ref _oMissing, ref _oMissing, ref isVisible, ref _oMissing, ref _oMissing,
ref _oMissing, ref _oMissing);
//激活文档
_oDoc.Activate();
NAR(_oDocs);
}
catch
{
//发生错误
}
finally
{
this.Dispose();
//_oWord.Application.Quit(ref _oMissing,ref _oMissing,ref _oMissing);
}
}
/// <summary>
/// 以只读的方式打开WORD文件
/// </summary>
/// <param name="strFileName">文件名</param>
public void Open(string FileName, bool IsReadOnly)
{
object fileName = FileName;
object readOnly = IsReadOnly;
object isVisible = true;
try
{
Word.Documents _oDocs = _oWord.Documents;
//打开文档
_oDoc = _oDocs.Open(ref fileName, ref _oMissing, ref readOnly, ref _oMissing,
ref _oMissing, ref _oMissing, ref _oMissing, ref _oMissing, ref _oMissing,
ref _oMissing, ref _oMissing, ref isVisible, ref _oMissing, ref _oMissing,
ref _oMissing, ref _oMissing);
//激活文档
_oDoc.Activate();
NAR(_oDocs);
}
catch
{
//发生错误
}
finally
{
this.Dispose();
//_oWord.Application.Quit(ref _oMissing,ref _oMissing,ref _oMissing);
}
}
#endregion
#region 内容操作
/// <summary>
/// 删除选择的内容
/// </summary>
public void Clear()
{
//_oDoc.Paragraphs.Last.Range.Text = WordText;
_oWord.Selection.Delete(ref _oMissing, ref _oMissing);
}
/// <summary>
/// 在文档空白的地方添加文字内容
/// </summary>
/// <param name="WordText">文本内容</param>
public void AppendText(string WordText,string font,int size,string bold)
{
//_oDoc.Paragraphs.Last.Range.Text = WordText;
SetFontSize(size);
SetFontStyle(bold);
SetFontName(font);
_oWord.Selection.TypeText(WordText);
DisabledFontStyle();
}
public void AppendText(string WordText)
{
//_oDoc.Paragraphs.Last.Range.Text = WordText;
_oWord.Selection.TypeText(WordText);
}
/// <summary>
/// 替换文本
/// </summary>
/// <param name="WordText"></param>
public void ReplaceText(string WordText)
{
//_oDoc.Paragraphs.Last.Range.Text = WordText;
_oWord.Options.ReplaceSelection = true;
_oWord.Selection.TypeText(WordText);
_oWord.Options.ReplaceSelection = false;
}
/// <summary>
/// 插入空行
/// </summary>
public void InsertBlankLine()
{
_oWord.Selection.TypeParagraph();
}
/// <summary>
/// 插入指定行数的空行
/// </summary>
/// <param name="LineNumber">行数</param>
public void InsertBlankLine(int LineNumber)
{
for (int i = 0; i < LineNumber; i++)
_oWord.Selection.TypeParagraph();
}
/// <summary>
/// 添加分页符
/// </summary>
public void InsertPageBreak()
{
object pageBreak = (int)Word.WdBreakType.wdPageBreak;
_oWord.Selection.InsertBreak(ref pageBreak);
}
/// <summary>
/// 修改标签内容
/// </summary>
public void setRangleText(Word.Bookmark bm, string text)
{
bm.Select();
bm.Range.Text = text=="-1"?"":text;
}
/// <summary>
/// 插入表格
/// </summary>
/// <param name="table">表格数据</param>
///
public void InsertTable(ref DataTable table)
{
Word.Table oTable;
int rowIndex = 1, colIndex = 0;
int rowsNumber, columnsNumber;
rowsNumber = table.Rows.Count + 1;
columnsNumber = table.Columns.Count;
oTable = _oDoc.Tables.Add(_oWord.Selection.Range, rowsNumber, columnsNumber, ref _oMissing, ref _oMissing);
//初始化列
foreach (DataColumn Col in table.Columns)
{
colIndex += 1;
oTable.Cell(1, colIndex).Range.InsertAfter(Col.ColumnName);
}
//添加行
foreach (DataRow Row in table.Rows)
{
rowIndex += 1;
colIndex = 0;
//按列添加
foreach (DataColumn Col in table.Columns)
{
colIndex += 1;
oTable.Cell(rowIndex, colIndex).Range.InsertAfter(Row[Col.ColumnName].ToString());
}
}
oTable.AllowAutoFit = true;
oTable.ApplyStyleFirstColumn = true;
oTable.ApplyStyleHeadingRows = true;
}
/// <summary>
/// 设置表格行边界样式
/// </summary>
/// <param name="LineStyle"></param>
public void SetRowBorderStyle(int itable, int row, Word.WdBorderType WdBorderType, Word.WdLineStyle LineStyle)
{
_oDoc.Tables[itable].Rows[row].Borders[WdBorderType].LineStyle = LineStyle;
}
/// <summary>
/// 设置表格列边界样式
/// </summary>
/// <param name="LineStyle"></param>
public void SetColBorderStyle(int itable, int col, Word.WdBorderType WdBorderType, Word.WdLineStyle LineStyle)
{
_oDoc.Tables[itable].Columns[col].Borders[WdBorderType].LineStyle = LineStyle;
}
/// <summary>
/// <summary>
/// 设置单元格边界样式
/// </summary>
/// <param name="LineStyle"></param>
public void SetCellBorderStyle(int itable,int row,int col,Word.WdBorderType WdBorderType, Word.WdLineStyle LineStyle)
{
_oDoc.Tables[itable].Rows[row].Cells[col].Borders[WdBorderType].LineStyle = LineStyle;
}
/// <summary>
/// 设置表格边界样式
/// </summary>
/// <param name="LineStyle"></param>
public void SetTableBorderStyle(int itable,Word.WdLineStyle LineStyle)
{
_oDoc.Tables[itable].Borders[Word.WdBorderType.wdBorderTop].LineStyle=LineStyle;
_oDoc.Tables[itable].Borders[Word.WdBorderType.wdBorderLeft ].LineStyle = LineStyle;
_oDoc.Tables[itable].Borders[Word.WdBorderType.wdBorderRight ].LineStyle = LineStyle;
_oDoc.Tables[itable].Borders[Word.WdBorderType.wdBorderBottom ].LineStyle = LineStyle;
_oDoc.Tables[itable].Borders[Word.WdBorderType.wdBorderHorizontal ].LineStyle = LineStyle;
_oDoc.Tables[itable].Borders[Word.WdBorderType.wdBorderVertical ].LineStyle = LineStyle;
}
/// <summary>
/// 设置单元格字体对齐方式,垂直,靠上
/// </summary>
/// <param name="LineStyle"></param>
public void SetCellAlignmentStyle(int itable, int row, int col, Word.WdCellVerticalAlignment wdVerticalAlignment)
{
_oDoc.Tables[itable].Rows[row].Cells[col].VerticalAlignment = wdVerticalAlignment;
}
/// <summary>
/// 设置表格的字体大小
/// </summary>
/// <param name="itable"></param>
/// <param name="font"></param>
public void SetTableFontSize(int itable, float Size)
{
_oDoc.Tables[itable].Range.Font.Size = Size;
}
//按列合并单元格 从第RowIndex行 ,第ColumnIndex列的单元格开始,合并Length个单元格
public void MergeColumCell(int itable,int RowIndex,int ColumnIndex, int Lenght)
{
//or(int index=1;index<=Lenght;index++)
{
_oDoc.Tables[itable].Cell(RowIndex, ColumnIndex).Merge(_oDoc.Tables[itable].Cell(RowIndex + Lenght, ColumnIndex));
}
}
//按行合并单元格 从第RowIndex行 ,第ColumnIndex列的单元格开始,合并Length个单元格
public void MergeRowCell(int itable, int RowIndex, int ColumnIndex, int Lenght)
{
// for (int index = 1; index <= Lenght; index++)
{
_oDoc.Tables[itable].Cell(RowIndex, ColumnIndex).Merge(_oDoc.Tables[itable].Cell(RowIndex, ColumnIndex + Lenght));
}
}
/// <summary>
/// 填充单元格
/// </summary>
/// <param name="Row">表格行数</param>
/// <param name="Cell">第一行单元格数目</param>
/// <param name="itable">文档中的表格索引号</param>
public void FillRow(int itable,int Row, int Cell,string str)
{
_oDoc.Tables[itable].Cell(Row, Cell).Range.InsertAfter(str);
}
/// <summary>
/// 添加单元格
/// </summary>
/// <param name="itable">文档中的表格索引号</param>
public void AddRow(int itable,int irow)
{
for (int i = 1; i <= irow; i++)
{
_oDoc.Tables[itable].Rows.Add(ref _oMissing);
}
}
#endregion
#region 得到当前文件信息
/// <summary>
/// 当前文档章节数,包含空行了章节数,行数,页数,段落数,字数
/// </summary>
/// <returns>字符串数组</returns>
public string[] GetFileInfo()
{
string[] FileInfo = new string[6];//章节数,包含空行了章节数,行数,页数,段落数,字数;
object IncludeFootnotesAndEndnotes = false;
FileInfo[0] = _oDoc.ComputeStatistics(Word.WdStatistic.wdStatisticCharacters, ref IncludeFootnotesAndEndnotes).ToString();
FileInfo[1] = _oDoc.ComputeStatistics(Word.WdStatistic.wdStatisticCharactersWithSpaces, ref IncludeFootnotesAndEndnotes).ToString();
FileInfo[2] = _oDoc.ComputeStatistics(Word.WdStatistic.wdStatisticLines, ref IncludeFootnotesAndEndnotes).ToString();
FileInfo[3] = _oDoc.ComputeStatistics(Word.WdStatistic.wdStatisticPages, ref IncludeFootnotesAndEndnotes).ToString();
FileInfo[4] = _oDoc.ComputeStatistics(Word.WdStatistic.wdStatisticParagraphs, ref IncludeFootnotesAndEndnotes).ToString();
FileInfo[5] = _oDoc.ComputeStatistics(Word.WdStatistic.wdStatisticWords, ref IncludeFootnotesAndEndnotes).ToString();
return FileInfo;
}
#endregion
#region 设置
/// <summary>
/// 设置页眉
/// </summary>
/// <param name="text">放置的文本</param>
public void SetPageHeader(string text)
{
if (_oWord.ActiveWindow.View.SplitSpecial != Word.WdSpecialPane.wdPaneNone)
_oWord.ActiveWindow.Panes[2].Close();
if (_oWord.ActiveWindow.View.Type == Word.WdViewType.wdNormalView || _oWord.ActiveWindow.View.Type == Word.WdViewType.wdOutlineView)
_oWord.ActiveWindow.ActivePane.View.Type = Word.WdViewType.wdPrintView;
_oWord.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekPrimaryHeader;
this.GotoDocEnd();
this.AppendText(text, "黑体", 9, "plain");
_oWord.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekMainDocument;
}
/// <summary>
/// 设置页脚
/// </summary>
/// <param name="text">放置的文本</param>
public void SetPageFooter(string text)
{
if (_oWord.ActiveWindow.View.SplitSpecial != Word.WdSpecialPane.wdPaneNone)
_oWord.ActiveWindow.Panes[2].Close();
if (_oWord.ActiveWindow.View.Type == Word.WdViewType.wdNormalView || _oWord.ActiveWindow.View.Type == Word.WdViewType.wdOutlineView)
_oWord.ActiveWindow.ActivePane.View.Type = Word.WdViewType.wdPrintView;
_oWord.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekPrimaryFooter;
this.GotoDocEnd();
this.AppendText(text,"黑体",9,"plain");
_oWord.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekMainDocument;
}
public void InsertParaTitle(int level)
{
switch(level)
{
case 1:
_oDoc.Paragraphs.Last.OutlineLevel = Word.WdOutlineLevel.wdOutlineLevel1;
break;
case 2:
_oDoc.Paragraphs.Last.OutlineLevel = Word.WdOutlineLevel.wdOutlineLevel2;
break;
case 3:
_oDoc.Paragraphs.Last.OutlineLevel = Word.WdOutlineLevel.wdOutlineLevel3;
break;
case 4:
_oDoc.Paragraphs.Last.OutlineLevel = Word.WdOutlineLevel.wdOutlineLevel4;
break;
case 5:
_oDoc.Paragraphs.Last.OutlineLevel = Word.WdOutlineLevel.wdOutlineLevel5;
break;
case 6:
_oDoc.Paragraphs.Last.OutlineLevel = Word.WdOutlineLevel.wdOutlineLevel6;
break;
case 7:
_oDoc.Paragraphs.Last.OutlineLevel = Word.WdOutlineLevel.wdOutlineLevel7;
break;
case 8:
_oDoc.Paragraphs.Last.OutlineLevel = Word.WdOutlineLevel.wdOutlineLevel8;
break;
case 9:
_oDoc.Paragraphs.Last.OutlineLevel = Word.WdOutlineLevel.wdOutlineLevel9;
break;
default:
_oDoc.Paragraphs.Last.OutlineLevel = Word.WdOutlineLevel.wdOutlineLevelBodyText;
break;
}
}
public void UpdateCatagory(float fontsize)
{
_oWord.ActiveDocument.TablesOfContents[1].RightAlignPageNumbers = true;
_oWord.ActiveDocument.TablesOfContents[1].Update();
_oWord.ActiveDocument.TablesOfContents[1].Range.Font.Size = fontsize;
_oWord.ActiveDocument.TablesOfContents[1].Range.Paragraphs.LineSpacingRule = Word.WdLineSpacing.wdLineSpace1pt5;
}
public void SetTableStyle()
{
for (int i = 1; i <= _oDoc.Tables.Count; i++)
{
_oDoc.Tables[i].Borders[Word.WdBorderType.wdBorderBottom].Color = Word.WdColor.wdColorBlack;
_oDoc.Tables[i].Borders[Word.WdBorderType.wdBorderTop].Color = Word.WdColor.wdColorBlack;
_oDoc.Tables[i].Borders[Word.WdBorderType.wdBorderLeft].Color = Word.WdColor.wdColorBlack;
_oDoc.Tables[i].Borders[Word.WdBorderType.wdBorderRight].Color = Word.WdColor.wdColorBlack;
_oDoc.Tables[i].Borders[Word.WdBorderType.wdBorderHorizontal].Color = Word.WdColor.wdColorBlack;
_oDoc.Tables[i].Borders[Word.WdBorderType.wdBorderVertical].Color = Word.WdColor.wdColorBlack;
_oDoc.Tables[i].Borders[Word.WdBorderType.wdBorderDiagonalDown].Color = Word.WdColor.wdColorBlack;
_oDoc.Tables[i].Borders[Word.WdBorderType.wdBorderDiagonalUp].Color = Word.WdColor.wdColorBlack;
}
}
/// <summary>
/// 设定对齐
/// </summary>
/// <param name="AlignmentType">对齐方式</param>
public void SetAlignment(string AlignmentType)
{
switch (AlignmentType.ToLower())
{
case "center":
_oWord.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
break;
case "left":
_oWord.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;
break;
case "right":
_oWord.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;
break;
case "justify":
_oWord.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphThaiJustify;
break;
case "distribute":
_oWord.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphDistribute;
break;
}
}
/// <summary>
/// 行距
/// </summary>
/// <param name="spacePoint"></param>
public void SetLineSpacingRule(int spacePoint)
{
switch (spacePoint)
{
case 15:
_oDoc.Paragraphs.LineSpacingRule = Word.WdLineSpacing.wdLineSpace1pt5;
break;
case 1:
_oDoc.Paragraphs.LineSpacingRule = Word.WdLineSpacing.wdLineSpaceSingle;
break;
case 2:
_oDoc.Paragraphs.LineSpacingRule = Word.WdLineSpacing.wdLineSpaceDouble;
break;
}
}
/// <summary>
/// 段后距
/// </summary>
/// <param name="space"></param>
public void SetSpaceAfter(float space)
{
_oDoc.Paragraphs.SpaceAfter = space; //段前、段后
}
/// <summary>
/// 段前距
/// </summary>
/// <param name="space"></param>
public void SetSpaceBefore(float space)
{
_oDoc.Paragraphs.SpaceBefore = space; //段前、段后
}
/**/
/// <summary>
/// 首行缩进
/// </summary>
/// <param name="fltCount">float类型的数值</param>
public void SetFirstLineIndent(float fltCount)
{
_oWord.Selection.ParagraphFormat.FirstLineIndent = fltCount;
}
/**/
/// <summary>
/// 左缩进
/// </summary>
/// <param name="fltCount">float类型的数值</param>
public void SetLeftIndent(float fltCount)
{
_oWord.Selection.ParagraphFormat.LeftIndent = fltCount;
}
/**/
/// <summary>
/// 右缩进
/// </summary>
/// <param name="fltCount">float类型的数值</param>
public void SetRightIndent(float fltCount)
{
_oWord.Selection.ParagraphFormat.RightIndent = fltCount;
}
/// <summary>
/// 设置字体名称
/// </summary>
/// <param name="FontName">字体名称</param>
public void SetFontName(string FontName)
{
_oWord.Selection.Font.Name = FontName;
}
/// <summary>
/// 设置字体风格
/// </summary>
/// <param name="FontStyle">字体风格</param>
public void SetFontStyle(string FontStyle)
{
switch (FontStyle.ToLower())
{
case "bold":
_oWord.Selection.Font.Bold = 1;
break;
case "plain":
_oWord.Selection.Font.Bold = 0;
break;
case "italic":
_oWord.Selection.Font.Italic = 1;
break;
case "underline":
_oWord.Selection.Font.Subscript = 1;
break;
case "strikethrough":
_oWord.Selection.Font.StrikeThrough = 1;
break;
}
}
/// <summary>
/// 取消字体风格
/// </summary>
public void DisabledFontStyle()
{
_oWord.Selection.Font.Bold = 0;
_oWord.Selection.Font.Italic = 0;
_oWord.Selection.Font.Underline = 0;
_oWord.Selection.Font.StrikeThrough = 0;
}
/// <summary>
/// 设定字号
/// </summary>
/// <param name="Size">字号</param>
public void SetFontSize(int Size)
{
_oWord.Selection.Font.Size = Size;
}
/// <summary>
/// 设置颜色
/// </summary>
/// <param name="Color"></param>
public void SetFontColor(Word.WdColor Color)
{
_oWord.Selection.Font.Color = Color;
}
#endregion
#region 选择
/// <summary>
/// 选择页眉
/// </summary>
public void SelectPageHeader()
{
if (_oWord.ActiveWindow.View.SplitSpecial != Word.WdSpecialPane.wdPaneNone)
_oWord.ActiveWindow.Panes[2].Close();
if (_oWord.ActiveWindow.View.Type == Word.WdViewType.wdNormalView || _oWord.ActiveWindow.View.Type == Word.WdViewType.wdOutlineView)
_oWord.ActiveWindow.ActivePane.View.Type = Word.WdViewType.wdPrintView;
_oWord.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekCurrentPageHeader;
}
/// <summary>
/// 选择页脚
/// </summary>
public void SelectPageFooter()
{
if (_oWord.ActiveWindow.View.SplitSpecial != Word.WdSpecialPane.wdPaneNone)
_oWord.ActiveWindow.Panes[2].Close();
if (_oWord.ActiveWindow.View.Type == Word.WdViewType.wdNormalView || _oWord.ActiveWindow.View.Type == Word.WdViewType.wdOutlineView)
_oWord.ActiveWindow.ActivePane.View.Type = Word.WdViewType.wdPrintView;
_oWord.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekCurrentPageFooter;
}
/// <summary>
/// 设置页面切换到主页面
/// </summary>
public void SetDocSeekViewToMainDocument()
{
_oWord.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekMainDocument;
}
/// <summary>
/// 选择段落文本
/// </summary>
/// <param name="Para">段落</param>
public void SelectRange(int Para)
{
_oDoc.Paragraphs[Para].Range.Select();
}
/// <summary>
/// 选择文本
/// </summary>
/// <param name="para">段落</param>
/// <param name="Sentences">句子</param>
public void SelectRange(int para, int Sentences)
{
_oDoc.Paragraphs[para].Range.Sentences[Sentences].Select();
}
/// <summary>
/// 选择文本
/// </summary>
/// <param name="start">开始处</param>
/// <param name="end">结束处</param>
public void SelectRange(long start, long end)
{
object startPos = start;
object endPos = end;
_oDoc.Range(ref startPos, ref endPos).Select();
}
/// <summary>
/// 查找书签
/// </summary>
/// <param name="BookMark">书签名称</param>
public void FindBookMark(string BookMark)
{
object goToBookMark = (int)Word.WdGoToItem.wdGoToBookmark;
object bookmark = BookMark;
_oWord.Selection.GoTo(ref goToBookMark, ref _oMissing, ref _oMissing, ref bookmark);
}
/// <summary>
/// 修改书签
/// </summary>
/// <param name="BookMark">书签名称</param>
/// <param name="newText">新名称</param>
public void ChangeBookMarkText(string BookMark,string newText)
{
object goToBookMark = (int)Word.WdGoToItem.wdGoToBookmark;
object bookmark = BookMark;
_oWord.Selection.GoTo(ref goToBookMark, ref _oMissing, ref _oMissing, ref bookmark).Text = newText;
}
/// <summary>
/// 判断书签是否存在
/// </summary>
/// <param name="BookMark">书签名称</param>
/// <returns>true Or false</returns>
public bool BookMarkExists(string BookMark)
{
return _oDoc.Bookmarks.Exists(BookMark);
}
/// <summary>
/// 转到文档开头
/// </summary>
public void GotoDocBegin()
{
object unit = Word.WdUnits.wdStory;
_oWord.Selection.HomeKey(ref unit, ref _oMissing);
}
/// <summary>
/// 转到文档结尾
/// </summary>
public void GotoDocEnd()
{
object unit = Word.WdUnits.wdStory;
_oWord.Selection.EndKey(ref unit, ref _oMissing);
}
/// <summary>
/// 转到表格
/// </summary>
/// <param name="ntable"></param>
public void GoToTheTable(int ntable)
{
object what = Word.WdUnits.wdTable;
object which = Word.WdGoToDirection.wdGoToFirst;
object count = ntable;
_oWord.Selection.GoTo(ref what, ref which, ref count, ref _oMissing);
_oWord.Selection.Find.ClearFormatting();
_oWord.Selection.Text = "";
}
/// <summary>
/// 转到某个表格的某个单元格
/// </summary>
/// <param name="nTable">表格序号</param>
/// <param name="nRow">行号</param>
/// <param name="nColumn">列号</param>
public void GotoTableCell(int nTable, int nRow, int nColumn)
{
_oDoc.Tables[nTable].Cell(nRow, nColumn).Range.Select();
}
/// <summary>
/// 获取某个表格的某个单元格
/// </summary>
/// <param name="nTable">表格序号</param>
/// <param name="nRow">行号</param>
/// <param name="nColumn">列号</param>
public string GetTableCellValue(int nTable, int nRow, int nColumn)
{
return _oDoc.Tables[nTable].Cell(nRow, nColumn).Range.Text;
}
/// <summary>
/// 转到右边单元格
/// </summary>
public void GoToRightCell()
{
// Selection.MoveRight Unit:=wdCell
object direction = Word.WdUnits.wdCell;
_oWord.Selection.MoveRight(ref direction, ref _oMissing, ref _oMissing);
}
/// <summary>
/// 转到左边单元格
/// </summary>
public void GoToLeftCell()
{
// Selection.MoveRight Unit:=wdCell
object direction = Word.WdUnits.wdCell;
_oWord.Selection.MoveLeft(ref direction, ref _oMissing, ref _oMissing);
}
/// <summary>
/// 表格中转到下面的单元格
/// </summary>
public void GoToDownCell()
{
// Selection.MoveRight Unit:=wdCell
object direction = Word.WdUnits.wdLine;
_oWord.Selection.MoveDown(ref direction, ref _oMissing, ref _oMissing);
}
/// <summary>
/// 表格中转到上面的单元格
/// </summary>
public void GoToUpCell()
{
// Selection.MoveRight Unit:=wdCell
object direction = Word.WdUnits.wdLine;
_oWord.Selection.MoveUp(ref direction, ref _oMissing, ref _oMissing);
}
#endregion
#region 插入文件
/// <summary>
/// 插入图片到WORD文档中
/// </summary>
/// <param name="FileName">文件名称</param>
public void InsertPicture(string FileName)
{
object LinkToFile = false;//链接到文件
object SaveWithDocument = true;//保存到文档中
_oWord.Selection.InlineShapes.AddPicture(FileName, ref LinkToFile, ref SaveWithDocument, ref _oMissing);
//bm.Range.InlineShapes.AddPicture(FileName, ref LinkToFile, ref SaveWithDocument, ref _oMissing);
}
/// <summary>
/// 插入WORD文件
/// </summary>
/// <param name="FileName">文件名称</param>
public void InsertWordFile(string FileName)
{
object ConfirmConversions = false;//是否提醒
object Link = false;//是否做为链接
object Attachment = false;//是否做为附件
_oWord.Selection.InsertFile(FileName, ref _oMissing, ref ConfirmConversions, ref Link, ref Attachment);
}
#endregion
#region 保存
public void SavaAs(object path) //保存文件
{
object format = Word.WdSaveFormat.wdFormatDocument;
_oDoc.SaveAs(ref path, ref format, ref _oMissing, ref _oMissing,
ref _oMissing, ref _oMissing, ref _oMissing, ref _oMissing,
ref _oMissing, ref _oMissing, ref _oMissing, ref _oMissing,
ref _oMissing, ref _oMissing, ref _oMissing, ref _oMissing);
}
#endregion
#region 删除文件
/// <summary>
/// 删除文件
/// </summary>
/// <param name="AbsoluteFilePath">文件名</param>
public void FileDelete(string AbsoluteFilePath)
{
try
{
System.IO.FileInfo objFile = new System.IO.FileInfo(AbsoluteFilePath);
if (objFile.Exists)//如果存在
{
//删除文件.
objFile.Delete();
}
objFile = null;
}
catch
{
}
}
#endregion
#region 关闭所有WINWORD进程
/// <summary>
/// 关闭文档
/// </summary>
public void CloseWordAndDoc()//善后处理
{
if (_oWord != null && _oDoc!=null)
{
_oWord.Quit(ref _oMissing, ref _oMissing, ref _oMissing);
_oDoc = null;
_oWord = null;
}
}
/// <summary>
/// 杀除所有WINWORD进程
/// </summary>
public void KillWinWord()
{
this.Dispose();
GC.Collect();
foreach (System.Diagnostics.Process p in System.Diagnostics.Process.GetProcesses())
{
if (p.ProcessName.ToLower() == "winword")
p.Kill();
}
}
#endregion
}
}
------------------------------------------------------结束-----------------------------------------------------------------
操作实例:
IDocManager Docmanager = new DocManager();
Docmanager.Add(@"D:/test.doc");//打开文件并复制模板到其中
写标签数据:
foreach (Word.Bookmark bm in Docmanager.Document.Bookmarks)
{
if (bm.Name == "B1")
{
Docmanager.setRangleText(bm, "成功添加到第一个标签");
}
else if (bm.Name == "B2")
{
Docmanager.setRangleText(bm, "成功添加到第二个标签");
}
插入图片:
public void InsertPic()
{
string picfilename = @"D:/test.jpg";
try
{
if (File.Exists(picfilename))
{
Docmanager.FindBookMark("InsertPIC");
Docmanager.AppendText(" ");
Docmanager.InsertParaTitle(0);
Docmanager.InsertPicture(picfilename);
File.Delete(picfilename);
}
}
catch (Exception ex) {
throw;
}
}
页眉页脚:
public void InsertDocFooter()
{
Docmanager.SetPageFooter("页脚");
}//页脚
public void InsertDocHeader()
{
Docmanager.SetPageHeader("页眉");
}//页眉
段落设置:
public void SetParagraphs() // {
Docmanager.SetSpaceAfter(0); //段前
Docmanager.SetSpaceBefore(0); //段后
Docmanager.SetLineSpacingRule(15);//行距1.5倍
Docmanager.SetFirstLineIndent(2);//首行缩进m³
}
更新目录:
public void UpdateCatagory()
{
float fontsize = 10;
Docmanager.UpdateCatagory(fontsize);
}
转到文档结尾:
Docmanager.GotoDocEnd();
- 对Word进行各种操作
- 对数组进行各种操作
- js 对表格进行各种操作
- js 对表格进行各种操作
- Cocos2d对图片进行各种操作
- delphi通过OLE对word进行单元格合并操作
- .NET 使用书签如何对Word模板进行操作
- C# 中使用Word文档对图像进行操作
- Qt中使用html语言对word进行图表操作
- 对oracle两日期进行的各种操作
- 【转载】js 对表格进行各种操作(转)
- 对word进行操作, 提示不能加载程序集 Microsoft.Office.Interop.Word.dll
- .net 引用Microsoft.Office.Interop.Word.dll对word文件进行操作
- C#对word操作
- 如何对word文档进行word加密
- 如何对word文档进行操作?附有PB操作EXCEL的资料
- 你妹, 不修改word文档, 直接对word进行ctrl + s操作, md5值居然变了!
- 在Delphi中使用CreateOleObject方法对WORD文件进行操作
- qte环境搭建
- JSP内置对象
- qt-embed环境搭建(送linux光盘)
- ruby 中使用 Dom技术来查找页面元素
- ubuntu9.10 ibus 从源码包安装五笔
- 对Word进行各种操作
- java中类的加载机制
- 学习笔记 从零开始学单片机(1) 建立实验环境
- Velocity在eclipse和Editplus下的开发插件
- Jsp页面,结果集分页和sql(top)分页的数据库性能对比分析
- JS时间显示
- php的几个不太常用的函数
- ACTIVE制作数字签名
- HTTP协议详解