正在做的項目裡,需要開發一個小工具,將需要的資料插入到Word文檔中。這當中有一項需求,要求能夠在Word文檔中某處插入表格,或者刪除該處表格。
這個小工具是在VS.Net2005、Office2007下開發的。
1、在Word文檔中插入一個書籤,書籤名稱為“tl”;
2、在VS2005建立一個C#項目,然後在引用中添加Word類庫;由於我使用的是Office2007,因此選擇的是"Microsoft Word 12.0 Object Library",如果你使用的是Office2003,就應該選擇11.0;
3、在代碼頂部添加對Word類庫的引用;
using Word = Microsoft.Office.Interop.Word;
4、開啟Word文檔
object missingValue = System.Reflection.Missing.Value;
object myTrue = false; //不顯示Word視窗
object fileName = @"F:\Doc1.doc";
Word._Application oWord = new Word.ApplicationClass();
Word._Document oDoc;
oDoc = oWord.Documents.Open(ref fileName, ref missingValue,
ref myTrue, ref missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref missingValue,
ref missingValue);
5、找到剛才添加的書籤
object tmp = "t1";
Word.Range startRange = oWord.ActiveDocument.Bookmarks.get_Item(ref tmp).Range;
6、刪除在該位置的表格
Word.Table tbl = startRange.Tables[1];
tbl.Delete();
如果書籤所在的位置並沒有插入表格,程式並不會刪除該位置下面的表格,而是會拋出異常,報錯。
7、插入表格,並劃線
//添加表格
oDoc.Tables.Add(startRange, 5, 4, ref missingValue, ref missingValue);
//為表格劃線
startRange.Tables[1].Borders[WdBorderType.wdBorderTop].LineStyle = WdLineStyle.wdLineStyleSingle;
startRange.Tables[1].Borders[WdBorderType.wdBorderLeft].LineStyle = WdLineStyle.wdLineStyleSingle;
startRange.Tables[1].Borders[WdBorderType.wdBorderRight].LineStyle = WdLineStyle.wdLineStyleSingle;
startRange.Tables[1].Borders[WdBorderType.wdBorderBottom].LineStyle = WdLineStyle.wdLineStyleSingle;
startRange.Tables[1].Borders[WdBorderType.wdBorderHorizontal].LineStyle = WdLineStyle.wdLineStyleSingle;
startRange.Tables[1].Borders[WdBorderType.wdBorderVertical].LineStyle = WdLineStyle.wdLineStyleSingle;
全部的代碼如下:
object missingValue = System.Reflection.Missing.Value;
object myTrue = false; //不顯示Word視窗
object fileName = @"F:\Doc1.doc";
Word._Application oWord = new Word.ApplicationClass();
Word._Document oDoc;
oDoc = oWord.Documents.Open(ref fileName, ref missingValue,
ref myTrue, ref missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref missingValue,
ref missingValue);
try
{
object tmp = "t1";
Word.Range startRange = oWord.ActiveDocument.Bookmarks.get_Item(ref tmp).Range;
//刪除指定書籤位置後的第一個表格
Word.Table tbl = startRange.Tables[1];
tbl.Delete();
//添加表格
oDoc.Tables.Add(startRange, 5, 4, ref missingValue, ref missingValue);
//為表格劃線
startRange.Tables[1].Borders[WdBorderType.wdBorderTop].LineStyle = WdLineStyle.wdLineStyleSingle;
startRange.Tables[1].Borders[WdBorderType.wdBorderLeft].LineStyle = WdLineStyle.wdLineStyleSingle;
startRange.Tables[1].Borders[WdBorderType.wdBorderRight].LineStyle = WdLineStyle.wdLineStyleSingle;
startRange.Tables[1].Borders[WdBorderType.wdBorderBottom].LineStyle = WdLineStyle.wdLineStyleSingle;
startRange.Tables[1].Borders[WdBorderType.wdBorderHorizontal].LineStyle = WdLineStyle.wdLineStyleSingle;
startRange.Tables[1].Borders[WdBorderType.wdBorderVertical].LineStyle = WdLineStyle.wdLineStyleSingle;
}
catch
{
//異常處理
}
object bSaveChange = true;
oDoc.Close(ref bSaveChange, ref missingValue, ref missingValue);
oDoc = null;
oWord = null;
代碼很簡單,在寫這執行個體的過程中我參考了如下資料:
Word物件模型概述
Word任務