简体   繁体   English

使用Office.Interop向MS Word表添加一行

[英]Add a row to an MS Word table using Office.Interop

I have a word template with a table that I am populating from a list of strings that I split using tab characters. 我有一个带有表的Word模板,该表是从使用制表符分隔的字符串列表中填充的。

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? 如何更改代码以始终在当前行之后添加一个空行?

Leave the parameter value as a missing value for the Row.Add Function 将参数值保留为Row.Add函数的缺失值

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... 我尚未测试此代码,但应该可以...

I found it, it should be: 我找到了,应该是:

Object oMissing = System.Reflection.Missing.Value;
oWordDoc.Tables[2].Rows.Add(ref oMissing); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM