Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add new row to excel file in C# [duplicate]

Tags:

c#

excel

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

like image 543
Alexander Avatar asked Apr 30 '13 16:04

Alexander


People also ask

How do I automatically Insert a new row?

Select the entire row which you want to insert a blank row above, and press Shift + Ctrl + + keys together, then a blank row is inserted.

How do you Insert a row in Excel with Ctrl?

Sometimes you may want to add a blank row to your spreadsheet. Luckily, there is an easy keyboard shortcut for this: Ctrl+Shift+Plus. Simply click a row number to select a row, hold down the Ctrl and Shift keys, and press plus (+). Excel will then add a row above the selected row.


1 Answers

Suppose you want to add in the third line:

Range line = (Range)worksheet.Rows[3];
line.Insert();
like image 122
Daniel Möller Avatar answered Oct 20 '22 03:10

Daniel Möller