using System;
using SystemCollectionsGeneric;
using SystemText;
using MicrosoftOfficeInteropWord;
using SystemDiagnostics;
namespace OfficeManager
{
public class WordClass : IDisposable
{
#region 字段
private _Application m_WordApp = null;
private _Document m_Document = null;
private object missing = SystemReflectionMissingValue;
#endregion
#region 构造函数与析构函数
public WordClass()
{
m_WordApp = new ApplicationClass();
}
~WordClass()
{
try
{
if (m_WordApp != null)
m_WordAppQuit(ref missing ref missing ref missing);
}
catch (Exception ex)
{
DebugWrite(exToString());
}
}
#endregion
#region 属性
public _Document Document
{
get
{
return m_Document;
}
}
public _Application WordApplication
{
get
{
return m_WordApp;
}
}
public int WordCount
{
get
{
if (m_Document != null)
{
Range rng = m_DocumentContent;
rngSelect();
return m_DocumentCharactersCount;
}
else
return ;
}
}
public object Missing
{
get
{
return missing;
}
}
#endregion
#region 基本任务
#region CreateDocument
public void CreateDocument(string template)
{
object obj_template = template;
if (templateLength <= ) obj_template = missing;
m_Document = m_WordAppDocumentsAdd(ref obj_template ref missing ref missing ref missing);
}
public void CreateDocument()
{
thisCreateDocument();
}
#endregion
#region OpenDocument
public void OpenDocument(string fileName bool readOnly)
{
object obj_FileName = fileName;
object obj_ReadOnly = readOnly;
m_Document = m_WordAppDocumentsOpen(ref obj_FileName ref missing ref obj_ReadOnly 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 OpenDocument(string fileName)
{
thisOpenDocument(fileName false);
}
#endregion
#region Save & SaveAs
public void Save()
{
if (m_Document != null)
m_DocumentSave();
}
public void SaveAs(string fileName)
{
object obj_FileName = fileName;
if (m_Document != null)
{
m_DocumentSaveAs(ref obj_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);
}
}
#endregion
#region Close
public void Close(bool isSaveChanges)
{
object saveChanges = WdSaveOptionswdDoNotSaveChanges;
if (isSaveChanges)
saveChanges = WdSaveOptionswdSaveChanges;
if (m_Document != null)
m_DocumentClose(ref saveChanges ref missing ref missing);
}
#endregion
#region 添加数据
/// <summary>
/// 添加图片
/// </summary>
/// <param name=picName></param>
public void AddPicture(string picName)
{
if (m_WordApp != null)
m_WordAppSelectionInlineShapesAddPicture(picName ref missing ref missing ref missing);
}
/// <summary>
/// 插入页眉
/// </summary>
/// <param name=text></param>
/// <param name=align></param>
public void SetHeader(string text WdParagraphAlignment align)
{
thism_WordAppActiveWindowViewType = WdViewTypewdOutlineView;
thism_WordAppActiveWindowViewSeekView = WdSeekViewwdSeekPrimaryHeader;
thism_WordAppActiveWindowActivePaneSelectionInsertAfter(text); //插入文本
thism_WordAppSelectionParagraphFormatAlignment = align; //设置对齐方式
thism_WordAppActiveWindowViewSeekView = WdSeekViewwdSeekMainDocument; // 跳出页眉设置
}
/// <summary>
/// 插入页脚
/// </summary>
/// <param name=text></param>
/// <param name=align></param>
public void SetFooter(string text WdParagraphAlignment align)
{
thism_WordAppActiveWindowViewType = WdViewTypewdOutlineView;
thism_WordAppActiveWindowViewSeekView = WdSeekViewwdSeekPrimaryFooter;
thism_WordAppActiveWindowActivePaneSelectionInsertAfter(text); //插入文本
thism_WordAppSelectionParagraphFormatAlignment = align; //设置对齐方式
thism_WordAppActiveWindowViewSeekView = WdSeekViewwdSeekMainDocument; // 跳出页眉设置
}
#endregion
#region Print
public void PrintOut()
{
object copies = ;
object pages = ;
object range = WdPrintOutRangewdPrintAllDocument;
object items = WdPrintOutItemwdPrintDocumentContent;
object pageType = WdPrintOutPageswdPrintAllPages;
object oTrue = true;
object oFalse = false;
thism_DocumentPrintOut(
ref oTrue ref oFalse ref range ref missing ref missing ref missing
ref items ref copies ref pages ref pageType ref oFalse ref oTrue
ref missing ref oFalse ref missing ref missing ref missing ref missing);
}
public void PrintPreview()
{
if (m_Document != null)
m_DocumentPrintPreview();
}
#endregion
public void Paste()
{
try
{
if (m_Document != null)
{
m_DocumentActiveWindowSelectionPaste();
}
}
catch (Exception ex)
{
DebugWrite(exMessage);
}
}
#endregion
#region 文档中的文本和对象
public void AppendText(string text)
{
Selection currentSelection = thism_WordAppSelection;
// Store the users current Overtype selection
bool userOvertype = thism_WordAppOptionsOvertype;
// Make sure Overtype is turned off
if (thism_WordAppOptionsOvertype)
{
thism_WordAppOptionsOvertype = false;
}
// Test to see if selection is an insertion point
if (currentSelectionType == WdSelectionTypewdSelectionIP)
{
currentSelectionTypeText(text);
currentSelectionTypeParagraph();
}
else
if (currentSelectionType == WdSelectionTypewdSelectionNormal)
{
// Move to start of selection
if (thism_WordAppOptionsReplaceSelection)
{
object direction = WdCollapseDirectionwdCollapseStart;
currentSelectionCollapse(ref direction);
}
currentSelectionTypeText(text);
currentSelectionTypeParagraph();
}
else
{
// Do nothing
}
// Restore the users Overtype selection
thism_WordAppOptionsOvertype = userOvertype;
}
#endregion
#region 搜索和替换文档中的文本
public void Replace(string oriText string replaceText)
{
object replaceAll = WdReplacewdReplaceAll;
thism_WordAppSelectionFindClearFormatting();
thism_WordAppSelectionFindText = oriText;
thism_WordAppSelectionFindReplacementClearFormatting();
thism_WordAppSelectionFindReplacementText = replaceText;
thism_WordAppSelectionFindExecute(
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);
}
#endregion
#region 文档中的Word表格
/// <summary>
/// 向文档中插入表格
/// </summary>
/// <param name=startIndex>开始位置</param>
/// <param name=endIndex>结束位置</param>
/// <param name=rowCount>行数</param>
/// <param name=columnCount>列数</param>
/// <returns></returns>
public Table AppendTable(int startIndex int endIndex int rowCount int columnCount)
{
object start = startIndex;
object end = endIndex;
Range tableLocation = thism_DocumentRange(ref start ref end);
return thism_DocumentTablesAdd(tableLocation rowCount columnCount ref missing ref missing);
}
/// <summary>
/// 添加行
/// </summary>
/// <param name=table></param>
/// <returns></returns>
public Row AppendRow(Table table)
{
object row = tableRows[];
return tableRowsAdd(ref row);
}
/// <summary>
/// 添加列
/// </summary>
/// <param name=table></param>
/// <returns></returns>
public Column AppendColumn(Table table)
{
object column = tableColumns[];
return tableColumnsAdd(ref column);
}
/// <summary>
/// 设置单元格的文本和对齐方式
/// </summary>
/// <param name=cell>单元格</param>
/// <param name=text>文本</param>
/// <param name=align>对齐方式</param>
public void SetCellText(Cell cell string text WdParagraphAlignment align)
{
cellRangeText = text;
cellRangeParagraphFormatAlignment = align;
}
#endregion
#region IDisposable 成员
public void Dispose()
{
try
{
if (m_WordApp != null)
m_WordAppQuit(ref missing ref missing ref missing);
}
catch (Exception ex)
{
DebugWrite(exToString());
}
}
#endregion
}
}