Home » excel » How to add new row to excel file in C#

How to add new row to excel file in C#

Posted by: admin March 9, 2020 Leave a comment

Questions:

I need to insert a new row below the first row.
using the code below, what i need to add to make it done ?

Excel.Application excelApp = new Excel.Application();
string myPath = @"Data.xlsx";
excelApp.Workbooks.Open(myPath);

// Get Worksheet
Excel.Worksheet worksheet = excelApp.Worksheets[1];
int rowIndex = 2; int colIndex = 2;
for (int i = 0; i < 10; i++)
{
   excelApp.Cells[rowIndex, colIndex] = "\r123";
}

excelApp.Visible = false;

Thanks 🙂

How to&Answers:

Suppose you want to add in the third line:

Range line = (Range)worksheet.Rows[3];
line.Insert();