打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
VS.NET2003中Word 文档操作类(C#)

 

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>

 

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
C# word生成PDF加水印
(转)C# Office操作
asp.net 填充word 模版标签并下载使用笔记
XML与DataTable/DataSet互转(C#) 把数据库中表的内容转存为XML文件
java 使用jacob+jacob工具类实现html页面导出word
javadoc
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服