添加一行到MS Word表格使用Office.Interop [英] Add a row to an MS Word table using Office.Interop

查看:981
本文介绍了添加一行到MS Word表格使用Office.Interop的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,我从我使用制表符分割字符串列表填充表的Word模板。

I have a word template with a table that I am populating from a list of strings that I split using tab characters.

我不知道有多少行,文字的我有,因为它可能会有所不同。

I do not know how many lines of text I will have as it will vary.

因此,我通过我的循环遍历像在此之前编程中添加一行:

So I am adding a row programmatically before iterating through my loop like this:

oWordDoc.Tables[2].Rows.Add(oWordDoc.Tables[2].Rows[1]);



不幸的是添加行之前,而不是当前行之后。

Unfortunately it is adding the row before rather than after the current row.

我怎样才能改变我的代码总是有当前行后添加一个空行?

How can I change my code to always have an empty row added after the current row?

推荐答案

退出参数数值作为Row.Add功能缺失值

Leave the parameter value as a missing value for the Row.Add Function

object oMissing = System.Reflection.Missing.Value;        
// get your table or create a new one like this
// you can start with two rows. 
Microsoft.Office.Interop.Word.Table myTable = oWordDoc.Add(myRange, 2,numberOfColumns)
int rowCount = 2; 
//add a row for each item in a collection.
foreach( string s in collectionOfStrings)
{
   myTable.Rows.Add(ref oMissing)
   // do somethign to the row here. add strings etc. 
   myTable.Rows.[rowCount].Cells[1].Range.Text = "Content of column 1";
   myTable.Rows[rowCount].Cells[2].Range.Text = "Content of column 2";
   myTable.Rows[rowCount].Cells[3].Range.Text = "Content of column 3";
   //etc
   rowCount++;
}



我没有测试代码,但应该工作...

I have not tested this code but should work...

这篇关于添加一行到MS Word表格使用Office.Interop的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆