word操作包括打开word文档创建表格合并单元格保存退出
WordOperator
using System;
using SystemWeb;
using MicrosoftOfficeInteropWord;
namespace CustomFileAccess
{
public class WordOperator
{
public void CreateWord()
{
MicrosoftOfficeInteropWordApplication wordApp = new Application();
MicrosoftOfficeInteropWordDocument wordDocument = new Document();
MicrosoftOfficeInteropWordTable wordTable;
MicrosoftOfficeInteropWordTable wordTableCopy;
object myNull = SystemReflectionMissingValue;
object strPath = HttpContextCurrentServerMapPath(@WordTemplete\MyWordTempletedoc);
object styleName = Table Grid ;
try
{
wordDocument = wordAppDocumentsOpen(ref strPath ref myNull ref myNull ref myNull
ref myNull ref myNull ref myNull ref myNull ref myNull ref myNull ref myNull ref myNull
ref myNull ref myNull ref myNull ref myNull);
object start = ;
object end = ;
MicrosoftOfficeInteropWordRange wordRange = wordDocumentRange(ref start ref end);
wordTable = wordDocumentTablesAdd(wordRange ref myNull ref myNull);
wordTableBordersOutsideColor = WdColorwdColorAutomatic;
wordTableBordersOutsideColorIndex = WdColorIndexwdAuto;
wordTableBordersOutsideLineStyle = WdLineStylewdLineStyleSingle;
wordTableBordersOutsideLineWidth = WdLineWidthwdLineWidthpt;
wordTableBordersInsideColor = WdColorwdColorAutomatic;
wordTableBordersInsideColorIndex = WdColorIndexwdAuto;
wordTableBordersInsideLineStyle = WdLineStylewdLineStyleSingle;
wordTableBordersInsideLineWidth = WdLineWidthwdLineWidthpt;
wordDocumentTables[]Cell( )Merge(wordDocumentTables[]Cell( ));
wordDocumentTables[]Cell( )RangeText = cell ;
wordDocumentTables[]Cell( )RangeText = cell ;
wordDocumentTables[]Select();
wordAppSelectionCopy();
wordDocumentTables[]Cell( )RangeText = The First Table;
object myunit = MicrosoftOfficeInteropWordWdUnitswdStory;
wordAppSelectionEndKey(ref myunit ref myNull);
wordAppSelectionTypeParagraph();
wordAppSelectionPaste();
wordDocumentTables[]Cell( )RangeText = The Second Table;
wordDocumentSave();
}
catch
{
wordDocumentClose(ref myNull ref myNull ref myNull);
wordAppQuit(ref myNull ref myNull ref myNull);
if (wordDocument != null)
{
SystemRuntimeInteropServicesMarshalReleaseComObject(wordDocument);
wordDocument = null;
}
if (wordApp != null)
{
SystemRuntimeInteropServicesMarshalReleaseComObject(wordApp);
wordApp = null;
}
GCCollect();
throw new Exception(文档生成失败!);
}
finally
{
wordDocumentClose(ref myNull ref myNull ref myNull);
wordAppQuit(ref myNull ref myNull ref myNull);
}
}
}
}