CSV files are vastly used while sharing large data in a compact size. Such data contain comma-separated values which are not really very human readable. However, it is quite easy to manipulate CSV files using editors like Microsoft Excel, OpenOffice Calc, or LibreOffice. There are certain scenarios where we need to compare two large CSV files for their difference. In order to perform this comparison, we do it programmatically. Earlier, we have discussed this CSV files comparison in Java. This article covers the basic way how to compare two CSV files using C# within the .NET application.
.NET API for Comparing CSV Files
GroupDocs provides its document comparison solution for various file formats. We will use its .NET API to compare CSV files within the application. It allows comparing two or more CSV files for differences. It further supports comparing password-protected CSV files, accepting and rejecting the discovered changes and much more.
You can download the DLLs or MSI installer from the downloads section or install the API by adding its package to your .NET application via NuGet.
PM> Install-Package GroupDocs.Comparison
Running examples for its features are also available at GitHub. Visit its documentation and API Reference for guidance.
How to Compare CSV Files using C#
Let’s head towards our objective and perform the basic comparison. You just have to load the comparison file and then call compare feature to get the comparison results. The following two are the files that are compared in the article below:
The following are the steps to compare two CSV files for differences using C#:
- First, load the CSV file using the Comparer class.
- Then, add the second CSV file using the respective Add method.
- Finally, compare both the files using the Compare method.
The following C# code compares the CSV files and provides the differences in a CSV output within the .NET application.
The output result is as follows:
Get a Free API License
You can get a free temporary license in order to use the API without the evaluation limitations.
Conclusion
To conclude, we have learned how to compare two CSV files within a .NET application using C#. The features let you find the dissimilarities between any two large CSV files. The summary of the comparison also provides the count for differences found within the compared files. Using these, you can build your own online CSV files comparison .NET application.
For more details and to learn about the API, visit its documentation. For queries, contact us via the forum.