如何在C#中将新行添加到Excel文件中[重复]

时间:2022-09-02 08:29:09

This question already has an answer here:

这个问题在这里已有答案:

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

谢谢 :)

1 个解决方案

#1


12  

Suppose you want to add in the third line:

假设您要添加第三行:

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

#1


12  

Suppose you want to add in the third line:

假设您要添加第三行:

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