如何在C#中向excel文件添加新行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16304469/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 00:42:14  来源:igfitidea点击:

How to add new row to excel file in C#

c#excel

提问by Alexander

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

谢谢 :)

采纳答案by Daniel M?ller

Suppose you want to add in the third line:

假设你想在第三行添加:

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