I need to delete certain rows from an .xlsx file using a C# program.
The whole table should be gone through and if a certain word appears in column B, the row should be removed.
My current solution looks like this:
public void RemoveLines(string path)
{
ExcelPackage.LicenseContext = LicenseContext.Commercial;
using (var package = new ExcelPackage(path))
{
ExcelWorksheet worksheet = package.Workbook.Worksheets[0];
for (int i = 1; i < worksheet.Rows.Count(); i++)
{
if (worksheet.Cells[i, 2].Text.ToLower().Contains("comment") || worksheet.Cells[i, 2].Text.ToLower().Contains("network"))
{
worksheet.DeleteRow(i);
i--;
}
}
package.SaveAs(path.Replace(".xlsx", "_CLEAN.xlsx"));
}
}
That works, but since I have a huge file with 70,000 lines, the whole thing takes about 10 minutes.
I’ve already tried saving the whole worksheet in an array, but I don’t know how to get the revised array back into a worksheet^^.
I’m currently using the EPPlus package.
Can anyone tell me how I can make the whole thing even faster, perhaps using an array or another method?
Thanks in advance
Regards