Deleting duplicate rows in Excel using Epplus

0 votes

I have a worksheet with a number of rows and several columns. I want to delete all duplicate rows in this worksheet. In other words, the highlighted rows in this screenshot should be deleted, and the rows below should be moved up:

enter image description here

and should result in the following:

enter image description here

I'm using the following snippet of code:

List<int> rowsToDelete = new List<int>();
for (int row = 1; row <= worksheet.Dimension.End.Row; row++)
{
    string a = worksheet.Cells[row,1].Value.ToString();
    string b = worksheet.Cells[row,2].Value.ToString();
    string c = worksheet.Cells[row,3].Value.ToString();

    int i = row + 1;
    while (worksheet.Cells[i,1].Value.ToString().Equals(a) &&
           worksheet.Cells[i,2].Value.ToString().Equals(b) &&
           worksheet.Cells[i,3].Value.ToString().Equals(c))
    {
        rowsToDelete.add(i);
        i++;
    }
}
foreach (var row in rowsToDelete)
{
    worksheet.Delete(row);
}

It is not deleting the correct rows. How can I fix this?

This is using Epplus 4.5.3.3 and .NET Framework 4.6.1

Feb 23 in Database by Edureka
• 9,320 points
33 views

1 answer to this question.

0 votes
  1. You need to re-think this… the while loop… as soon as it finds a row that is NOT a duplicate then it will exit the while loop. ...
  2. @JohnG that is the purpose of the while loop, it should iterate through the duplicates only for a particular row.
answered Feb 23 by Edureka
• 8,820 points

Related Questions In Database

0 votes
1 answer

Convert Rows to columns using 'Pivot' in SQL Server

You can use the PIVOT function to ...READ MORE

answered Feb 8 in Database by Vaani
• 5,040 points
14 views
0 votes
1 answer

How to delete duplicate rows in SQL Server?

CTEs and ROW_NUMBER can be combined together which will ...READ MORE

answered Feb 10 in Database by Vaani
• 5,040 points
17 views
0 votes
0 answers

How to get address, Column Name and Row Name of all marked rows in Excel table as rows in new worksheet

 need the row/column combinations marked with an ...READ MORE

Feb 24 in Database by Edureka
• 9,320 points
25 views
0 votes
1 answer
0 votes
2 answers

Is there a .NET equivalent to Apache Hadoop?

Hadoop is a Java-based platform. So, to ...READ MORE

answered Jul 16, 2020 in Big Data Hadoop by Suhana
• 340 points
609 views
0 votes
1 answer

Validate String against USPS State Abbreviations

Try something like this: private static String states ...READ MORE

answered Sep 20, 2018 in IoT (Internet of Things) by Annie97
• 2,160 points
207 views
0 votes
1 answer

Authenticate on an ASP.Net Forms Authorization website from a console app

Essentially, we need to record a regular ...READ MORE

answered Sep 20, 2018 in IoT (Internet of Things) by Annie97
• 2,160 points
232 views
0 votes
1 answer

Deleting duplicate rows in Excel using Epplus

Versions 3.5 and 2.0 of the NET ...READ MORE

answered Mar 25 in Database by Edureka
• 8,820 points
41 views
0 votes
1 answer

Convert a number to a letter in C# for use in Microsoft Excel [duplicate]

If you are familiar with using formulas ...READ MORE

answered Feb 23 in Database by Edureka
• 8,820 points
36 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP