C#操作WORD(二)

上一篇讲到,一个Document可能会有多个Rang对象。Rang由起始和结束字符来定他的位置。
以下代码为先清空Document里的内容,再在第一行写入内容。
    // Clear out any existing information.
    Object start = Type.Missing;
    Object end = Type.Missing;
     Object unit = Type.Missing;
    Object count = Type.Missing;
    ThisDocument.Range(ref start, ref end). Delete(ref unit, ref count);

    // Set up the header information.
    start = 0;
    end = 0;
     rng = ThisDocument.Range(ref start, ref end);
     rng.InsertBefore("Xiaopai");
     rng.Font.Name = "Verdana";
     rng.Font.Size = 16;
     rng.InsertParagraphAfter();//输入回车

以下为在刚写入的内容后添加一个表格。
    object missingValue = Type.Missing;
    object location = 8; //注:若location超过已有字符的长度将会出错。
    Word.Range rng = ThisDocument.Range(ref location, ref location);
    ThisDocument.Tables.Add(rng, 3, 4, ref missingValue, ref missingValue);

以下为在刚创建的表格里添加一行
    Word.Table tbl = ThisDocument.Tables[1]; //第一个表格为1,而不是0
    Object beforeRow = Type.Missing;
    tbl.Rows.Add(ref beforeRow); //在表格的最后添加一行

填充表格内容
    tbl.Cell(1, 1).Range.Text = "shuai"; //在表格的第一行第一列填入内容。

设置单元格风格
    Word.Range rngCell;
    rngCell = tbl.Cell(1, 2).Range;
    rngCell.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;
    rngCell.Font.Size = 8;
    rngCell.Font.Name = "Verdana";

当时没找到合并单元格的方法。有谁知道的共享一下哈。

参考资料:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_wrcore/html/wrtskhowtocreatewordtables.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_wrcore/html/wrtskhowtoaddrowscolumnstowordtables.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odc_vsto2003_ta/html/odc_VSTWordtbl.asp
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值