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

Our*_*nas 9 c# ms-word office-interop

我有一个带有表格的单词模板,我从使用制表符分割的字符串列表中填充.

我不知道会有多少行文字,因为它会有所不同.

所以我在迭代循环之前以编程方式添加一行,如下所示:

oWordDoc.Tables[2].Rows.Add(oWordDoc.Tables[2].Rows[1]);
Run Code Online (Sandbox Code Playgroud)

不幸的是,它是在前一行而不是在当前行之后添加行.

如何更改我的代码以在当前行之后添加空行?

joe*_*cop 6

将参数值保留为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++;
}
Run Code Online (Sandbox Code Playgroud)

我没有测试过这段代码但应该工作......


Our*_*nas 5

我找到了,应该是:

Object oMissing = System.Reflection.Missing.Value;
oWordDoc.Tables[2].Rows.Add(ref oMissing); 
Run Code Online (Sandbox Code Playgroud)