varshini
varshini

Reputation: 21

extracting duplicate records from excel using c#.net

I am new to asp.net.I have an excel sheet which has many duplicate records. I need to display the duplicate records alone saying that these are the duplicates. How can I do it. Is there a particular query to get only the duplicates.Please help!!

Upvotes: 2

Views: 4510

Answers (2)

Pankaj
Pankaj

Reputation: 10115

First I will like to suggest you to use "ExcelReaderFactory" to read the excel records.

VB.Net Code

Dim excelReader As IExcelDataReader = Nothing
Dim result As DataSet = Nothing
Dim stream As FileStream = Nothing
stream = File.Open(Server.MapPath("FilePath.xls"), FileMode.Open, FileAccess.Read)
excelReader = ExcelReaderFactory.CreateBinaryReader(stream)
result = excelReader.AsDataSet()
stream.Close()
stream.Dispose()
excelReader.Close()
result.Dispose()

C# Code

IExcelDataReader excelReader = null;
DataSet result = null;
FileStream stream = null;
stream = File.Open(Server.MapPath("FilePath.xls"), FileMode.Open, FileAccess.Read);
excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
result = excelReader.AsDataSet();
stream.Close();
stream.Dispose();
excelReader.Close();
result.Dispose();

After getting the data in datatable you can use select function.

And last Dispose your datatable.

Upvotes: 1

IAmTimCorey
IAmTimCorey

Reputation: 16757

OK, you didn't provide a lot of detail in what you are having trouble with so I'm going to provide you with a good overview of what you need to do. Then you can look at it, work through the examples, and see if you are still confused or missing anything.

Basically, you are going to use the ADO.NET Jet OLEDB connection to Excel to query the data. Basically you will do a SELECT statement. Now I don't believe that you can do a SELECT DISTINCT, which is what would be easiest (please try in order to verify), but you can at least do a SELECT statement to get all of your data into a DataTable.

Once you have your data in the DataTable, you will apply a view, which will allow you to filter out duplicate data. The basic code would be similar to:

ds.Tables["YourTable"].DefaultView.ToTable(true,”uniqueID”);

You can adapt this to your needs. Here are a couple links to help you get started:

How to query Excel from ASP.NET

How to get a distinct row from a DataTable

Upvotes: 0

Related Questions