using System.Data;
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Office.Interop.Word;
namespace Patent.Analyse.Client.Classes
{
public class WordOperator
{
#region 私有变量
private const int NORMAL = 1;
private const int ERROR = 0;
private string m_FileName = String.Empty;
private Microsoft.Office.Interop.Word.Application m_WordApplication = null;
private object m_Missing = System.Reflection.Missing.Value;
private Document m_Doc = null;
private object m_Readonly = false; // 文档是否为只读
private bool m_WordApplicationEnabled = false; // 文档应用程序是否可用
private string m_SaveAsFileName = String.Empty;
#endregion
#region 属性
/// <summary>
/// WORD 文档名称
/// </summary>
public string FileName
{
get { return this.m_FileName; }
set { this.m_FileName = value; }
}
/// <summary>
/// WORD 文档名称
/// </summary>
public string SaveAsFileName
{
get { return this.m_SaveAsFileName; }
set { this.m_SaveAsFileName = value; }
}
/// <summary>
/// WORD 文档名称
/// </summary>
public bool WordApplicationEnabled
{
get { return this.m_WordApplicationEnabled; }
set { this.m_WordApplicationEnabled = value; }
}
#endregion
#region 方法
/// <summary>
/// 构造函数
/// </summary>
public WordOperator()
{
this.m_WordApplication = new Microsoft.Office.Interop.Word.ApplicationClass();
this.m_Doc = new Microsoft.Office.Interop.Word.DocumentClass();
}
/// <summary>
/// 打开文档
/// </summary>
/// <returns>0:异常;1:正常</returns>
public int Open()
{
try
{
// 判断文件名是否正确
if (this.FileName == null || this.FileName == String.Empty)
{
return ERROR;
}
object oFileName = this.FileName;
this.m_Doc = this.m_WordApplication.Documents.Add(ref oFileName, ref this.m_Missing, ref this.m_Readonly, ref this.m_Missing);
this.m_Doc.Activate();
this.m_WordApplication.Visible = this.WordApplicationEnabled;
return NORMAL;
}
catch
{
return ERROR;
}
}
/// <summary>
/// 关闭文档
/// </summary>
/// <returns></returns>
public int Close()
{
try
{
this.m_Doc.Close(ref this.m_Missing, ref this.m_Missing, ref this.m_Missing);
this.m_WordApplication.Quit(ref this.m_Missing, ref this.m_Missing, ref this.m_Missing);
return NORMAL;
}
catch
{
return ERROR;
}
}
/// <summary>
/// 退出所有WORD进程
/// </summary>
public void Quit()
{
if (this.m_Doc != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(this.m_Doc);
this.m_Doc = null;
}
if (this.m_WordApplication != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(this.m_WordApplication);
this.m_WordApplication = null;
}
GC.Collect();
foreach (System.Diagnostics.Process p in System.Diagnostics.Process.GetProcesses())
{
if (p.ProcessName.ToUpper() == "WINWORD")
{
p.Kill();
}
}
}
/// <summary>
/// 保存
/// </summary>
public int Save()
{
try
{
this.m_Doc.Save();
return NORMAL;
}
catch
{
return ERROR;
}
}
/// <summary>
/// 返回保存文件的FileName
/// </summary>
/// <param name="strDefaultExt">要保存文件的类型</param>
/// <param name="strFilter">文件名筛选器字符串</param>
/// <returns>保存的路径</returns>
private string SaveFileName(string strDefaultExt, string strFilter)
{
string fileName = "";
System.Windows.Forms.SaveFileDialog saveFileDlg = new System.Windows.Forms.SaveFileDialog();
saveFileDlg.DefaultExt = strDefaultExt;
saveFileDlg.Filter = strFilter;
if (saveFileDlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
fileName = saveFileDlg.FileName;
return fileName;
}
/// <summary>
/// 另存
/// </summary>
public int SaveAs()
{
try
{
if (this.SaveAsFileName == null || this.SaveAsFileName == String.Empty)
{
return ERROR;
}
object oFileName = this.SaveAsFileName;
this.m_Doc.SaveAs(ref oFileName, ref this.m_Missing, ref this.m_Missing, ref this.m_Missing, ref this.m_Missing, ref this.m_Missing, ref this.m_Missing, ref this.m_Missing, ref this.m_Missing, ref this.m_Missing, ref this.m_Missing, ref this.m_Missing, ref this.m_Missing, ref this.m_Missing, ref this.m_Missing, ref this.m_Missing);
return NORMAL;
}
catch
{
return ERROR;
}
}
/// <summary>
/// 插入
/// </summary>
/// <param name="txt"></param>
/// <returns></returns>
public int InsertText(string txt)
{
try
{
this.m_WordApplication.Selection.TypeText(txt);
return NORMAL;
}
catch
{
return ERROR;
}
}
/// <summary>
/// 在书签后面插入文本信息
/// </summary>
/// <param name="txt">文本信息的具体内容</param>
/// <param name="flag">标志:0-在标签前面插入;1-在标签后面插入</param>
/// <returns></returns>
public int InsertText(string bkname, string txt, int flag)
{
try
{
foreach (Microsoft.Office.Interop.Word.Bookmark bk in this.m_Doc.Bookmarks)
{
if (bk.Name == bkname)
{
bk.Select();
if (flag == 0)
{
bk.Range.InsertBefore(txt);
}
else if (flag == 1)
{
bk.Range.InsertAfter(txt);
}
}
}
return NORMAL;
}
catch
{
return ERROR;
}
}
/// <summary>
/// 从书签后面插入图片
/// </summary>
/// <param name="bkname"></param>
/// <param name="strPicName"></param>
/// <returns></returns>
public int InsertImg(string bkname, string strPicName)
{
try
{
foreach (Microsoft.Office.Interop.Word.Bookmark bk in this.m_Doc.Bookmarks)
{
if (bk.Name == bkname)
{
bk.Select();
bk.Range.InlineShapes.AddPicture(strPicName, ref this.m_Missing, ref this.m_Missing, ref this.m_Missing);
}
}
return NORMAL;
}
catch
{
return ERROR;
}
}
/// <summary>
/// 在段落的后面插入图片
/// </summary>
/// <param name="strPicName"></param>
/// <returns></returns>
public int InsertImg(string strPicName)
{
this.m_Doc.Paragraphs.Last.Application.Selection.Range.InlineShapes.AddPicture(strPicName, ref this.m_Missing, ref this.m_Missing, ref this.m_Missing);
// this.m_Doc.Bookmarks
return 0;
}
/// <summary>
/// 测试图象
/// </summary>
/// <returns></returns>
public int TestParagraph()
{
foreach (Microsoft.Office.Interop.Word.Paragraph p in this.m_Doc.Paragraphs)
{
Console.WriteLine("TXT = " + p.Range.Text);
}
return 0;
}
// 书签操作
// 插入图片
// 打开WORD文档
// 保存WORD文档
// 关闭WORD文档
// 插入文字
// 另存
// WORD TO HTML
// 表格操作
// 段落对齐方式
// 设置字体(名称 颜色 大小 是否加粗 下画线 等)
// 查找
// 替换
// ECT
}
}