Word中根据表头动态增加表格行,然后往表格里写数据,以及在Word中添加书签,然后替换书签值

     在Word中,对于已经有了固定的表头的表格,但是不知道表格具体需要多少行时,我们可以根据需要动态的增加,以及单元格 的拆分,合并等操作。以下 是具体的例子代码(对于没有表头的表格,参照于我的另一篇博客):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Word;
using System.Windows;
using C_STP_WPF_Building.entity;
namespace C_STP_WPF_Building
{
    public class Report
    {
        private _Application wordApp = null; //代表当前应用程序
        public  _Document wordDoc = null;//代表当前的word文档
        private Table table = null;//代表word文档中的表格
        private Range allRange;
        public _Application Application
        {
            get
            {
                return wordApp;
            }
            set
            {
                wordApp = value;
            }
        }
        public _Document Document
        {
            get
            {
                return wordDoc;
            }
            set
            {
                wordDoc = value;
            }
        }
        //通过模板打开文档
        public void CreateNewDocument(string filePath)
        {
            try
            {
                killWinWordProcess();//杀死word进程
                wordApp = new Word.Application();
                wordApp.DisplayAlerts = WdAlertLevel.wdAlertsNone;
                wordApp.Visible = false;
                object missing = System.Reflection.Missing.Value;
                object templateName = filePath;
                wordDoc = wordApp.Documents.Open(ref templateName, ref missing,
                    ref missing, ref missing, ref missing, ref missing, ref missing,
                    ref missing, ref missing, ref missing, ref missing, ref missing,
                    ref missing, ref missing, ref missing, ref missing);
                table = wordDoc.Tables[15];//文档中的表格,如果是第一个表格Tables[1].
                allRange = wordDoc.Range(missing, missing);  
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
            }
        }

//该类表示向Word表格中的第n个表格中添加num行,然后向数组中的数据写入表格中

    //n为表格序号
    //num为要插入几行数据
    //rp为插入数据的数组

   //包括插入表格,表格合并,表格拆分,插入数据

 public void AddRow10(int n, int num, ResultParameter[] rp, Carproportion[] cararray, Variabless[] variablessarray)
        {
            object miss = System.Reflection.Missing.Value;
            Word.Table table = wordDoc.Content.Tables[n];//获取第几个表格
            int row = variablessarray.Length * cararray.Length;
            for (int i &#

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值