12

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 :)

Indy9000
  • 8,651
  • 2
  • 32
  • 37
Alexander
  • 215
  • 1
  • 2
  • 9
  • 1
    This question has been asked already some times, such as: http://stackoverflow.com/questions/13418776/excel-insert-rows-not-add – K_B Apr 30 '13 at 16:17

1 Answers1

25

Suppose you want to add in the third line:

Range line = (Range)worksheet.Rows[3];
line.Insert();
Termininja
  • 6,620
  • 12
  • 48
  • 49
Daniel Möller
  • 84,878
  • 18
  • 192
  • 214