Я использую EPPluse для работы с Excel.
Ниже приведен мой код.
using (ExcelPackage package = new ExcelPackage(fileInfo))
{
ExcelWorksheet worksheet = package.Workbook.Worksheets[1];
int noofQuotas = 0;//no of row per group should be added
int headingTemplateStartingRow = 47;
int headingTemplateEndingRow = 50;
int headerStartingRowId = 47;//starting row of group table heading
int headerEndingRowId = 50; //last row of group table heading
int insertbefore = 51;//last row of default excel
int NoOfcolumnsToCopy = 30;
int gapBetweenGroups = 2;
worksheet.DeleteRow(51, 2, true);
foreach (AnswerEstimateGroup obj in Groups)
{
noofQuotas = obj.Quotas.Count;
//copy the header from group 1
int totalrows = worksheet.Dimension.Rows;
worksheet.Cells[headingTemplateStartingRow, 1, headingTemplateEndingRow, NoOfcolumnsToCopy].Copy(worksheet.Cells[headerStartingRowId, 1, headerEndingRowId, NoOfcolumnsToCopy]);
foreach (Quota qobj in obj.Quotas)
{
worksheet.InsertRow(insertbefore, 1);
}
worksheet.InsertRow(insertbefore, gapBetweenGroups);
headerStartingRowId = headerEndingRowId + noofQuotas + gapBetweenGroups;
headerEndingRowId = headerStartingRowId + 4;
insertbefore = headerEndingRowId + 1;
worksheet.InsertRow(insertbefore,20);
package.Save();
}
}
В цикле for я могу копировать строки, но во второй раз я получаю сообщение об ошибке.
![enter image description here](https://i.stack.imgur.com/NFEb2.png)
Я не мог понять, что пошло не так