Search code examples
c#excelvisual-studiovstolistobject

Adding a Row to an Excel Table


I am working on an Excel 2013/2015 VSTO workbook application. From data entered in a Windows form I'd like to create a new record in an existing Table on one of my worksheets.

How do I insert a new Table row into an Excel Table (not a simple worksheet array) using C# and VSTO?


Solution

  • The Microsoft.Office.Tools.Excel.ListObject provides the needed functionality. The table can be referenced and rows added as follows:

    Microsoft.Office.Tools.Excel.ListObject lo = Globals.Sheet1.MyTable;
    lo.ListRows.Add();
    

    Optionally, the position of the new row can be specified (https://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.listrows.add.aspx). Default is at the end of the table.

    IntelliSense did not show the Add() function in my case, which caused the confusion. Just type it in.