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
• 13,640 points
284 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 gaurav
• 22,040 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
• 7,020 points
296 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
• 7,020 points
165 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
• 13,640 points
769 views
0 votes
1 answer
0 votes
1 answer

Export DataTable to Excel File

Add Interop References. First we need to ...READ MORE

answered Jun 9 in JQuery by gaurav
• 22,040 points
103 views
0 votes
1 answer

How to create and download excel document using asp.net

First, download the Open XML Format SDK 2.0. It comes ...READ MORE

answered Oct 3 in Others by narikkadan
• 37,660 points
122 views
0 votes
1 answer

Merge and export Excel/Word/PDF to PDF

Use GroupDocs.Merger for .NET API to merge Word, Excel, ...READ MORE

answered Oct 3 in Others by narikkadan
• 37,660 points
78 views
0 votes
1 answer

How to remove Blank Rows using EPPlus Excel Package Plus

Check this answer here: https://stackoverflow.com/a/49232456/1114531.  It is checking the ...READ MORE

answered Oct 3 in Others by narikkadan
• 37,660 points
271 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 gaurav
• 22,040 points
477 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 gaurav
• 22,040 points
140 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP