C#在Word文档指定位置处理表格

正在做的项目里,需要开发一个小工具,将需要的数据插入到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, 54ref 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任务

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值