Compare CSV Files using C# .NET

We recently discussed the basic way how to compare two CSV files within our .NET applications. Today, in this article, we will further learn to compare more than two CSV using C#. Additionally, we will discuss how to compare the password-protected CSV files. Last but not least, how to accept and reject the found changes using C# within .NET application.

The following topics are covered below:

.NET API for Comparing CSV Files

GroupDocs.Comparison has its .NET API that allows comparing CSV files in various ways. We will use it to compare CSV files within the .NET application. It further supports the comparison of more than two CSV files, password-protected files, accepting and rejecting the discovered changes.

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.

Compare CSV Files using C#

We have already discussed the basic comparison of two CSV files earlier. There, we looked into the files comparison C# source code, sample input files for comparison, and the comparison output file. You can visit that article for a basic comparison.

Compare More than Two CSV Files using C#

If you have more than two CSV files to compare, nothing to worry about. It is just as simple as comparing two files. You just need to add the third file to the Comparer.

The following are the steps to compare more than two CSV files using C#.

  • Instantiate the Comparer class with the first CSV file to compare.
  • Add the second CSV file using the appropriate Add method.
  • Similarly, add as many other CSV files using the Add method.
  • Lastly, invoke the Compare method the get the comparison results of all the added CSV files.

The following C# source code compares multiple CSV files and generates the comparison results in separate CSV and HTML files. The HTML output file highlights the changes in different colors.

Compare Password Protected CSV Files using C#

If your CSV files are password-protected, you just need to provide the password while loading these files. There is no difference in the rest of the procedure for comparing multiple password-protected CSV files.

The following are the steps to compare password-protected CSV files using C#.

  • Prepare the loading options using the LoadOptions class and by setting the password to open the CSV file.
  • Load the first CSV file to compare while instantiating the Comparer class.
  • Add the second CSV file using the appropriate Add method.
  • Similarly, add as many other CSV files using any of the Add methods.
  • Lastly, call the Compare method to get the comparison of all the added CSV files.

The following C# source code compares multiple password-protected CSV files and generates the comparison results in separate CSV and HTML files.

Accept or Reject Identified Changes of CSV Files using C#

Just like the feature of Microsoft Word, you can implement the track changes option for the CSV files within your .NET applications. After the file comparison, you can accept or reject each of the identified changes. The following are the steps to compare CSV files and then accept or reject the identified changes.

  • Load the source and add target CSV files using the Comparer class.
  • Perform the comparison of the loaded files using the Compare method.
  • Fetch the identified changes using the GetChanges method.
  • Now you can traverse the changes and set the ComparisonAction of each change.
    • For the agreed changes you can set them as Accept.
    • For the disagreed changes you can set them as Reject.
  • Finally, call the ApplyChanges method to get the resultant document having the applied changes.

The following C# source code compares two CSV files and then accepts and rejects the changes accordingly.

Get a Free API License

You can get a free temporary license in order to use the API without the evaluation limitations.

Conclusion

To sum up, today we learned how to compare two or more CSV files using C#. Along with the comparison of password-protected CSV files, we learned to programmatically accept and reject the identified changes within the .NET applications.

There are many other customizations to control the comparison results, like setting the comparison sensitivity, showing only the summary page, ignoring gaps, and much more. Learn about these features from the documentation.

You can start building your own comparison applications for various supported document formats. For queries, contact us via the forum.

See Also