[导入]VS.NET2003中Word 文档操作类(C#)

http://blog.csdn.net/zhangleixp/archive/2006/05/01/704671.aspx

using System;

using System.Collections;

using System.Diagnostics;

using System.ComponentModel;

 

/// <summary>

/// Word 文档操作类。

/// </summary>

/// <author>zhangleipub@126.com</author>

/// <created>2005-12-21</created>

/// <remarks>

/// 首先得添加对"Microsoft Office 11.0 Object Library" 的引用。

/// 方法是:工具栏->项目->添加引用->COM引用。

/// </remarks>

public class WordObject

{

static object oMissing = System.Reflection.Missing.Value;

static Object oFalse = false;

static Object oTrue = true;

private Word.Application WordApp;

private Word.Document WordDoc;

private string errorMsg;

public event CancelEventHandler DocumentClosing;

 

/// <summary>

/// 域集合。

/// </summary>

private Hashtable DocFields = null;

 

#region 属性

/// <summary>

/// Word 应用程序对象。

/// </summary>

public Word.Application Application

{

get

{

return WordApp;

}

}

 

/// <summary>

/// Word 文档对象。

/// </summary>

public Word.Document Document

{

get

{

return WordDoc;

}

}

 

/// <summary>

/// 构造函数,置空

/// </summary>

public WordObject()

{

WordApp = null;

WordDoc = null;

}

 

/// <summary>

/// 设置活动窗口的标题

/// </summary>

public string ActiveWindowCaption

{

get

{

return WordApp.ActiveWindow.Caption;

}

set

{

WordApp.ActiveWindow.Caption = value;

}

}

 

/// <summary>

/// 属性:错误信息

/// </summary>

public string ErrorMsg

{

get

{

return errorMsg ;

}

set

{

errorMsg = value;

Debug.WriteLine(errorMsg);

}

}

 

/// <summary>

/// 取得Word应用程序对象。

/// </summary>

/// <returns></returns>

private Word.Application GetApplication()

{

if(WordApp != null)

{

try

{

WordApp.Dummy2();

}

catch

{

this.Close(false);

Debug.WriteLine("WordApp.Dummy2()异常!");

}

}

 

if(WordApp == null)

{

WordApp = new Word.Application();

WordApp.DocumentBeforeClose += new Word.ApplicationEvents4_DocumentBeforeCloseEventHandler(WordApp_DocumentBeforeClose);

}

 

return WordApp;

}

 

/// <summary>

/// 表格。

/// </summary>

public Word.Tables Tables

{

get

{

return WordDoc.Tables;

}

}

 

/// <summary>

/// 域对象。

/// </summary>

public Word.Fields Fields

{

get

{

return WordDoc.Fields;

}

}

 

#endregion

 

#region 获取或设置文本

/// <summary>

/// 设置域的文本。

/// </summary>

/// <param name="fieldName">域名。</param>

/// <param name="value">显示值。</param>

/// <returns></returns>

public bool SetFieldText(string fieldName, string value)

{

GetFields();

try

{

((Word.Field)(DocFields[fieldName])).Result.Text = value;

}

catch(Exception exp)

{

ErrorMsg = exp.Message + "rn" + exp.StackTrace;

return false;

}

 

ErrorMsg = string.Empty;

return true;

}

 

/// <summary>

/// 获取域的文本。

/// </summary>

/// <param name="fieldName">域名。</param>

/// <returns>域的值。</returns>

public string GetFieldText(string fieldName)

{

GetFields();

string value = null;

try

{

value = ((Word.Field)(DocFields[fieldName])).Result.Text;

}

catch(Exception exp)

{

ErrorMsg = exp.Message + "rn" + exp.StackTrace;

return null;

}

 

ErrorMsg = string.Empty;

return value;

}

 

/// <summary>

/// 设置单元格的值。

/// </summary>

/// <param name="tableIndex">表格的在Word文档中的索引。</param>

/// <param name="row">单元格的行号。</param>

/// <param name="column">单元格的列号。</param>

/// <param name="value">单元格的值。</param>

/// <returns>操作是否成功。</returns>

public bool SetCellText(int tableIndex, int row, int column, string value)

{

try

{

WordDoc.Tables[tableIndex].Cell(row,column).Range.Text = value;

}

catch(Exception exp)

{

ErrorMsg = exp.Message + "rn" + exp.StackTrace;

return false;

}

 

ErrorMsg = string.Empty;

return true;

}

 

/// <summary>

/// 获取单元格的值。

/// </summary>

/// <param name="tableIndex">表格的在Word文档中的索引。</param>

/// <param name="row">单元格的行号。</param>

/// <param name="column">单元格的列号。</param>

/// <returns>单元格的值。</returns>

public string GetCellText(int tableIndex, int row, int column)

{

string value = null;

try

{

value = WordDoc.Tables[tableIndex].Cell(row,column).Range.Text;

}

catch(Exception exp)

{

ErrorMsg = exp.Message + "rn" + exp.StackTrace;

return null;

}

 

ErrorMsg = string.Empty;

return value;

}

 

/// <summary>

/// 初始化域集合对象。

/// </summary>

private void GetFields()

{

if(DocFields != null) // 只初始化一次。

{

return;

}

 

DocFields = new Hashtable();

int fieldsCount = Fields.Count;

for(int i = 1; i <= fieldsCount; ++i)

{

string fieldCode = Fields[i].Code.Text;

int pos = fieldCode.IndexOf(""); // 反斜杠的位置。

if(pos == -1)pos = fieldCode.Length;

string key = fieldCode.Substring(0,pos).Trim();

 

DocFields.Add(key, Fields[i]); // 添加。

}

}

#endregion

 

#region 表格行列操作

 

/// <summary>

/// 插入列。

/// </summary>

/// <param name="tableIndex">表格的在Word文档中的索引。</param>

/// <param name="beforeColumn">在哪一列之前插入新列。</param>

/// <returns>操作是否成功。</returns>

public bool InsertColumn(int tableIndex, int beforeColumn)

{

Word.Table tbl = WordDoc.Tables[tableIndex];

object col = tbl.Columns[beforeColumn];

 

try

{

WordDoc.Tables[tableIndex].Columns.Add(ref col);

}

catch(Exception exp)

{

ErrorMsg = exp.Message + "rn" + exp.StackTrace;

return false;

}

 

ErrorMsg = string.Empty;

return true;

}

 

/// <summary>

/// 插入行。

/// </summary>

/// <param name="tableIndex">表格的在Word文档中的索引。</param>

/// <param name="beforeColumn">在哪一行之前插入新行。</param>

/// <returns>操作是否成功。</returns>

public bool InsertRow(int tableIndex, int beforeRow)

{

Word.Table tbl = WordDoc.Tables[tableIndex];

object row = tbl.Rows[beforeRow];

try

{

WordDoc.Tables[tableIndex].Rows.Add(ref row);

}

catch(Exception exp)

{

ErrorMsg = exp.Message + "rn" + exp.StackTrace;

return false;

}

 

ErrorMsg = string.Empty;

return true;

}

 

#endregion

 

#region 打开

/// <summary>

/// 打开已存在的Word文档。

/// </summary>

/// <param name="bVisible">是否打开Word窗口使得用户可以看到。</param>

/// <returns></returns>

public bool Open(string filename, bool bVisible)

{

GetApplication();

try

{

WordApp.Visible = bVisible;

if(WordDoc != null)

{

WordDoc.Close(ref oFalse, ref oMissing, ref oMissing);

WordDoc = null;

}

object oFileName = filename;

WordDoc = WordApp.Documents.Open(

ref oFileName,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, ref oMissing);

WordDoc.Activate();

}

catch(Exception exp)

{

ErrorMsg = exp.Message + "rn" + exp.StackTrace;

return false;

}

 

ErrorMsg = string.Empty;

return true;

}

 

/// <summary>

/// 根据模板,新建Word一个文档。

/// </summary>

/// <param name="filename">模板路径。模板为空,则新建空白文档。</param>

/// <param name="bVisible">是否打开Word窗口。</param>

/// <returns></returns>

public bool CreateNewDocument(string tplFileName, bool bVisible)

{

GetApplication();

WordApp.Visible = bVisible;

try

{

if(WordDoc != null)

{

WordDoc.Close(ref oFalse, ref oMissing, ref oMissing);

WordDoc = null;

}

 

object oVisible = bVisible;

object oFileName = tplFileName;

if(tplFileName != null && tplFileName.Length > 0)

{

WordDoc = WordApp.Documents.Add(ref oFileName, ref oFalse, ref oMissing, ref oVisible);

}

else

{

WordDoc = WordApp.Documents.Add(ref oMissing, ref oFalse, ref oMissing, ref oVisible);

}

WordDoc.Activate();

}

catch(Exception exp)

{

ErrorMsg = exp.Message + "rn" + exp.StackTrace;

return false;

}

ErrorMsg = string.Empty;

return true;

}

#endregion

 

#region 保存

/// <summary>


文章来源:http://wengjinbao.cn/?tid=485
posted @ 2007-10-18 09:59  海浪~~  阅读(1570)  评论(0编辑  收藏  举报