2013-12-18 116 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); 
相關問題