using System.Collections.Generic;
namespace ExcelDuplicateRemover
public static void Main(string[] args)
var inputExcelFile = "input.xlsx";
var outputExcelFile = "output.xlsx";
int[] columnIndices = {0, 1, 2};
using (var package = new ExcelPackage(new FileInfo(inputExcelFile)))
var worksheet = package.Workbook.Worksheets["Sheet1"];
Console.WriteLine("Worksheet not found. Make sure the worksheet name is correct.");
var duplicates = new HashSet<int>();
foreach (int columnIndex in columnIndices)
var seenNames = new HashSet<string>();
for (int rowIndex = startRow; rowIndex <= worksheet.Dimension.End.Row; rowIndex++)
var cell = worksheet.Cells[rowIndex, columnIndex + 1];
var cellValue = cell.Text;
if (seenNames.Contains(cellValue))
duplicates.Add(rowIndex);
seenNames.Add(cellValue);
Console.WriteLine("Deleting rows");
var sortedDuplicates = new List<int>(duplicates);
sortedDuplicates.Sort((a, b) => -1 * a.CompareTo(b));
foreach (int rowIndex in sortedDuplicates)
worksheet.DeleteRow(rowIndex);
package.SaveAs(new FileInfo(outputExcelFile));
Console.WriteLine("Done. Duplicates removed and saved to {outputExcelFile}");