2013-12-18 58 views
6

我有一个表格,我从我使用制表符分割的字符串列表填充表格。使用Office添加一行到MS Word表格

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

所以我增加了一行程序通过我的迭代循环像现在这样的:

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

遗憾的是,加入行之前,而不是当前行之后。

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

回答

5

给参数值作为缺失值的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++; 
} 

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

3

我发现了它,它应该是:

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