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, 2022 in Database by Edureka
• 13,690 points
1,515 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, 2022 by gaurav
• 23,260 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, 2022 in Database by Vaani
• 7,070 points
1,620 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, 2022 in Database by Vaani
• 7,070 points
829 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, 2022 in Database by Edureka
• 13,690 points
2,068 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, 2022 in JQuery by gaurav
• 23,260 points
785 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, 2022 in Others by narikkadan
• 63,600 points
2,964 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, 2022 in Others by narikkadan
• 63,600 points
1,510 views
0 votes
1 answer

How to remove Blank Rows using EPPlus Excel Package Plus

It is checking the last row if ...READ MORE

answered Oct 3, 2022 in Others by narikkadan
• 63,600 points
3,358 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, 2022 in Database by gaurav
• 23,260 points
1,623 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, 2022 in Database by gaurav
• 23,260 points
863 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