Compare Two Files or More in C# – Compare Word Document, Excel Spreadsheet or Any Other

Document comparison is one of the most common requirements for today’s programming world. Whether it is to compare word files, compare excel files, PDF documents or even compare text files or any other document format, accuracy is the key factor while comparing.

Compare Files with Document Comparison API for .NET Developers

This article will give you the idea, how GroupDocs.Comparison facilitates programmers to compare any two or more documents in many ways. On-Premise APIs of GroupDocs.Comparison are currently available for .NET and Java, however, this article is inclined towards C# developers.

Compare two Excel Files, Word Files or any Document in C#

GroupDocs.Comparison allows developers to compare two documents (in fact more than 2). The resulting document shows the changes between the two files in comparison. Below mentioned code shows how you can compare two excel files in just 3 lines of code in C#.

  1. Instantiate the Comparer object with the source document path.
  2. Call Add method to specify the target document path.
  3. Call Compare method.
  4. That’s it.
using (Comparer comparer = new Comparer(“source.xlsx”))
{
    comparer.Add(“target.xlsx”);
    comparer.Compare(“result.xlsx”);
}

Comparing excel spreadsheets or Microsoft Word documents are just among the subset of comparisons that are supported by the .NET API of GroupDocs.Comparison. Below is the list of supported formats. You can visit the documentation to stay updated.

Document TypeFile Formats
Word ProcessingDOC, DOCX, DOCM, DOT, DOTX, DOTM, RTF, TXT
SpreadsheetsXLS, XLSX, XLSM, XLT, XLTM, XLSB, XLSM, CSV
PresentationsPPT, PPTX, PPS, PPSX, POT, POTX
OpenDocumentODT, ODP, OTP, ODS, OTT
PortablePDF
Microsoft Visio DrawingsVSD, VSDX, VSS, VST, VDX
Note TakingONE
WebHTM, HTML, MHT, MHTML
eBooksMOBI
ImagesBMP, GIF, JPG, JPEG, PNG, DICOM, DJVU, DWG, DXF
EmailsEML, EMLX, MSG

Compare two or more Documents in C#

After the release of GroupDocs.Comparison for .NET 20.2, the API now supports:

  • Comparison of more than two Microsoft Excel and OpenOffice spreadsheets (XLS, XLSX, ODS, CSV, …)
  • Compare multiple Microsoft OneNote documents.

The API already supports the comparison of multiple files for various document formats. Following code snippet shows how quickly, multiple excel files can be compared in C#.

using (Comparer comparer = new Comparer(“source.xlsx”)
{
    comparer.Add(“target1.xlsx”);
    comparer.Add(“target2.xlsx”);
    comparer.Add(“target3.xlsx”);
    comparer.Compare(“result.xlsx”);
}

Compare Documents from Stream

As a programmer, you are not only allowed to compare documents that are available on local storage, in fact, we can compare documents from the stream.

  1. Just initialize the Comparer object with the source document stream.
  2. Call Add method and specify the target stream.
  3. Call Compare method
using (Comparer comparer = new Comparer(File.OpenRead(“source.docx”))
{
    comparer.Add(File.OpenRead(“target1.docx”));
    comparer.Add(File.OpenRead(“target2.docx”));
    comparer.Add(File.OpenRead(“target3.docx”));
    comparer.Compare(File.Create(“result.docx”));
}

Compare Password Protected Word Documents / Excel Spreadsheet

Password protection is common in the official documentation. Using the document comparison .NET API, it allows its users/developers to compare password-protected documents.

Just a little change in the code as compared to the code for comparing documents that are not password-protected. While loading the document, use LoadOptions to specify the document password. Below is the sample comparison code for your assistance.

using (Comparer comparer = new Comparer("source.docx", new LoadOptions() { Password = "1234" }))
{
    comparer.Add("target1.docx", new LoadOptions() { Password = "5678" });
    comparer.Add("target2.docx", new LoadOptions() { Password = "5678" });
    comparer.Add("target3.docx", new LoadOptions() { Password = "5678" });
    comparer.Compare("result.docx");
}

Comparison of Documents with Specific Settings

One step ahead of just comparing, using the code similar to the mentioned below, you can compare multiple documents with your customized comparison settings.

CompareOptions provides you the opportunity to specify your comparison options like font styling for detected changes etc.

using (Comparer comparer = new Comparer(“source.docx”)
{
    comparer.Add(“target1.docx”);
    comparer.Add(“target2.docx”);
    comparer.Add(“target3.docx”);
    CompareOptions compareOptions = new CompareOptions()
    {
        InsertedItemStyle = new StyleSettings()
        {
            FontColor = System.Drawing.Color.Yellow
        }
    };
    comparer.Compare(“result.docx”, compareOptions);
}

Compare Programming Language Files

GroupDocs continuously increasing the support to compare more file formats. After the release v 20.2, you can now also compare JSON files using .NET API. Following are the programming language file formats that are recently added to the supported document formats list:

ActionScriptObjective C/C++
AssemblerPerl
C-BasedPHP
CSharpPython
GroovyRuby
JavaScriptScala
JavaShell/Batch Script, Log, Diff, Config, LESS
JSONSQL

Let’s Talk

You can build your own application using the above-highlighted features. We will be delighted if you contact us on the forum to discuss, solving a problem or share your feedback.