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

后端 未结 2 1857
野的像风
野的像风 2020-12-04 01:56

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 a

2条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-04 02:29

    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...

提交回复
热议问题