Search code examples
c#.netoffice-interopoffice-automationnetoffice

Adding a row into a sheet using NetOffice


I have a

Excel.Workbook book = application.Workbooks.Open(txtPathExcel.Text);
Excel.Worksheet mainSheet = (Excel.Worksheet)book.Sheets[1];

How can i add a new row in mainSheet? I can't find any NetOffice functions for it.


Solution

  • All rows are already exist in the sheet, you just need to select the required.

    The Rows property of the Application class returns a Range object that represents all the rows on the active worksheet. For example:

     Worksheets("Sheet1").Rows(3)
    

    The range object refers to the third row on the sheet.

    Also you may find the Insert method helpful. It inserts a cell or a range of cells into the worksheet or macro sheet and shifts other cells away to make space.