如何用C#編程實現動態生成Word文檔並填充數據的效果呢?要使用C#操作word首先要添加引用
添加引用>COM>Microsoft Word Object Library
在cs文件中添加
using Word;
下面的例子中包括C#對Word文檔的創建插入表格設置樣式等操作
(例子中代碼有些涉及數據信息部分被省略重要是介紹一些C#操作word文檔的方法)
public string CreateWordFile( string CheckedInfo)
{
string message = ;
try
{
Object Nothing = SystemReflectionMissingValue;
DirectoryCreateDirectory( C:/CNSI ); // 創建文件所在目錄
string name = CNSI_ + DateTimeNowToShortString() + doc ;
object filename = C://CNSI// + name; // 文件保存路徑
// 創建Word文檔
WordApplication WordApp = new WordApplicationClass();
WordDocument WordDoc = WordAppDocumentsAdd( ref Nothing ref Nothing ref Nothing ref Nothing);
// 添加頁眉
WordAppActiveWindowViewType = WdViewTypewdOutlineView;
WordAppActiveWindowViewSeekView = WdSeekViewwdSeekPrimaryHeader;
WordAppActiveWindowActivePaneSelectionInsertAfter( [頁眉內容] );
WordAppSelectionParagraphFormatAlignment = WordWdParagraphAlignmentwdAlignParagraphRight; // 設置右對齊
WordAppActiveWindowViewSeekView = WdSeekViewwdSeekMainDocument; // 跳出頁眉設置
WordAppSelectionParagraphFormatLineSpacing = f; // 設置文檔的行間距
// 移動焦點並換行
object count = ;
object WdLine = WordWdUnitswdLine; // 換一行;
WordAppSelectionMoveDown( ref WdLine ref count ref Nothing); // 移動焦點
WordAppSelectionTypeParagraph(); // 插入段落
// 文檔中創建表格
WordTable newTable = WordDocTablesAdd(WordAppSelectionRange ref Nothing ref Nothing);
// 設置表格樣式
newTableBordersOutsideLineStyle = WordWdLineStylewdLineStyleThickThinLargeGap;
newTableBordersInsideLineStyle = WordWdLineStylewdLineStyleSingle;
newTableColumns[ ]Width = f;
newTableColumns[ ]Width = f;
newTableColumns[ ]Width = f;
// 填充表格內容
newTableCell( )RangeText = 產品詳細信息表 ;
newTableCell( )RangeBold = ; // 設置單元格中字體為粗體
// 合並單元格
newTableCell( )Merge(newTableCell( ));
WordAppSelectionCellsVerticalAlignment = WordWdCellVerticalAlignmentwdCellAlignVerticalCenter; // 垂直居中
WordAppSelectionParagraphFormatAlignment = WordWdParagraphAlignmentwdAlignParagraphCenter; // 水平居中
// 填充表格內容
newTableCell( )RangeText = 產品基本信息 ;
newTableCell( )RangeFontColor = WordWdColorwdColorDarkBlue; // 設置單元格內字體顏色
// 合並單元格
newTableCell( )Merge(newTableCell( ));
WordAppSelectionCellsVerticalAlignment = WordWdCellVerticalAlignmentwdCellAlignVerticalCenter;
// 填充表格內容
newTableCell( )RangeText = 品牌名稱 ;
newTableCell( )RangeText = BrandName;
// 縱向合並單元格
newTableCell( )Select(); // 選中一行
object moveUnit = WordWdUnitswdLine;
object moveCount = ;
object moveExtend = WordWdMovementTypewdExtend;
WordAppSelectionMoveDown( ref moveUnit ref moveCount ref moveExtend);
WordAppSelectionCellsMerge();
// 插入圖片
string FileName = Picture; // 圖片所在路徑
object LinkToFile = false ;
object SaveWithDocument = true ;
object Anchor = WordDocApplicationSelectionRange;
WordDocApplicationActiveDocumentInlineShapesAddPicture(FileName ref LinkToFile ref SaveWithDocument ref Anchor);
WordDocApplicationActiveDocumentInlineShapes[ ]Width = f; // 圖片寬度
WordDocApplicationActiveDocumentInlineShapes[ ]Height = f; // 圖片高度
// 將圖片設置為四周環繞型
WordShape s = WordDocApplicationActiveDocumentInlineShapes[ ]ConvertToShape();
sWrapFormatType = WordWdWrapTypewdWrapSquare;
newTableCell( )RangeText = 產品特殊屬性 ;
newTableCell( )Merge(newTableCell( ));
// 在表格中增加行
WordDocContentTables[ ]RowsAdd( ref Nothing);
WordDocParagraphsLastRangeText = 文檔創建時間 + DateTimeNowToString(); // 落款
WordDocParagraphsLastAlignment = WordWdParagraphAlignmentwdAlignParagraphRight;
// 文件保存
WordDocSaveAs( ref filename ref Nothing ref Nothing ref Nothing ref Nothing ref Nothing ref Nothing ref Nothing ref Nothing ref Nothing ref Nothing ref Nothing ref Nothing ref Nothing ref Nothing ref Nothing);
WordDocClose( ref Nothing ref Nothing ref Nothing);
WordAppQuit( ref Nothing ref Nothing ref Nothing);
message = name + 文檔生成成功以保存到C:CNSI下 ;
}
catch
{
message = 文件導出異常! ;
}
return message;
}
From:http://tw.wingwit.com/Article/program/net/201311/11754.html