I am working with C# Desktop Application. I am trying to delete header rows that appears multiple times in the excel sheet. The method which i have found i working and removing the rows from the sheet but they are taking too much time. the records in sheets are around 6k to 8k each. Below are the functions:
public void DeleteRow_GameDetailSheet(Excel.Worksheet clsWorksheet, string rowStartingWithString)
{
int rowIndex = 5;
Excel.Range range = clsWorksheet.UsedRange;
if (rowStartingWithString.Length > 0)
{
while (rowIndex >= 5)
{
if (rowIndex >= 5)
{
if(rowIndex==5)
{
((Excel.Range)clsWorksheet.Rows[4, Type.Missing]).Delete();
((Excel.Range)clsWorksheet.Rows[3, Type.Missing]).Delete();
((Excel.Range)clsWorksheet.Rows[2, Type.Missing]).Delete();
((Excel.Range)clsWorksheet.Rows[1, Type.Missing]).Delete();
}
else
{
((Excel.Range)clsWorksheet.Rows[rowIndex, Type.Missing]).Delete();
}
if (rowIndex>5 && rowStartingWithString == "Club ID")
{
for (int i = 0; i < 8; i++)
{
rowIndex = rowIndex - 1;
((Excel.Range)clsWorksheet.Rows[rowIndex, Type.Missing]).Delete();
}
}
};
rowIndex = FindRow(range, rowStartingWithString);
}
}
}
public int FindRow(Excel.Range range, String marker)
{
int row = -1;
try
{
// row = range.Find(marker).Row;
row = (int)(range.Find(marker)?.Row);
return row;
}
catch (Exception ex)
{
row = -1;
return row;
}
}
Kindly someone suggest me what am doing wrong that it's taking too much time. it normally took around 15 to 25 minute to complete the process. Any help would be highly appreciated. Thanks