Compare Excel Spreadsheets. Advanced Cell-by-Cell Analysis in C#
Compare Excel Spreadsheets. Advanced Cell-by-Cell Analysis in C#
Leave feedback
On this page
Note
π‘For the complete working code and detailed explanations, please refer to the full repository here. This repository contains all source files, helper classes, and configuration examples to implement Excel spreadsheet comparison in your .NET applications.
This article demonstrates how to compare Excel spreadsheets (XLSX, XLS) using GroupDocs.Comparison for .NET. The sample project provides ready-to-use examples that enable developers to quickly identify cell-level differences with customizable styling, visibility controls, summary pages, and advanced comparison options.
Excel spreadsheet comparison is essential for financial auditing, data validation, version control, compliance reporting, and collaborative editing. With GroupDocs.Comparison for .NET, you can automate Excel comparison workflows and generate result documents that highlight insertions, deletions, and modifications with configurable visual styles.
π‘ Use this approach when you need to automatically detect and highlight differences between Excel spreadsheet versions without manual review.
This example demonstrates GroupDocs.Comparison’s CompareOptions and StyleSettings classes for custom formatting. Inserted cells appear in green, deleted cells in brown, and changed cells in firebrick, all with bold, italic, and underline formatting.
Hide Inserted Content
The HideInsertedContentComparison method focuses on deletions and modifications:
By setting ShowInsertedContent to false, this sample suppresses the display of any newly added cells in the result document, making deletions and modifications more prominent.
Hide Deleted Content
The HideDeletedContentComparison method focuses on additions and modifications:
Setting ShowDeletedContent to false removes any visual indication of deleted cells, highlighting only additions and changes.
Leave Gaps for Deleted Content
The LeaveGapsComparison method preserves document structure:
privatestaticvoidLeaveGapsComparison(stringsourcePath,stringtargetPath,stringresultPath){EnsureFileExists(sourcePath,"source Excel file");EnsureFileExists(targetPath,"target Excel file");varcompareOptions=newCompareOptions{LeaveGaps=true};using(varcomparer=newComparer(sourcePath)){comparer.Add(targetPath);comparer.Compare(resultPath,compareOptions);}Console.WriteLine("Comparison completed (gaps left for deleted content).");}
Enabling LeaveGaps retains empty cells where deletions occurred, keeping the spreadsheet’s layout intact and making the missing content immediately visible.
Hide Both Inserted and Deleted Content
The HideBothContentComparison method shows only modifications:
Both ShowInsertedContent and ShowDeletedContent are disabled, while LeaveGaps remains true to keep the layout. The result highlights only cells that changed value, ideal for change-impact analysis.
License Management
Apply the GroupDocs.Comparison license before performing comparisons:
privatestaticvoidApplyLicense(){stringlicensePath="path to your license file";Licenselicense=newLicense();license.SetLicense(licensePath);}
Update the licensePath variable with the path to your license file. Without a valid license, the library runs in evaluation mode with limited capabilities.
File Validation
The EnsureFileExists helper method validates file presence:
privatestaticvoidEnsureFileExists(stringpath,stringdescription){if(!File.Exists(path)){thrownewFileNotFoundException($"The {description} was not found. Path: {path}",path);}}
This helper validates file existence before any comparison operation, providing a clear exception message that aids debugging.
Running the Examples
Place your Excel files in the sample-files directory:
source.xlsx - The original Excel file
target.xlsx - The modified Excel file to compare against
Update the license path in Program.cs:
stringlicensePath="path to your license file";
Run the project:
dotnet run
Check the output directory for comparison results:
result_basic.xlsx - Basic comparison result
result_styled.xlsx - Styled comparison with summary page
result_hide_inserted.xlsx - Result with inserted content hidden
result_hide_deleted.xlsx - Result with deleted content hidden
result_leave_gaps.xlsx - Result with gaps for deleted content
result_hide_both.xlsx - Result showing only modifications
Notes
Replace file paths with your actual document locations.
Default styling uses standard colors for inserted, deleted, and modified content.
Summary page generation provides a consolidated view of all changes in a single page.
Visibility controls allow you to focus on specific change types based on your analysis needs.
The LeaveGaps option preserves document structure, making it easier to identify where content was removed.
For detailed examples and advanced scenarios, see the full repository.