Compare Excel Spreadsheets. Advanced Cell-by-Cell Analysis in Java
Compare Excel Spreadsheets. Advanced Cell-by-Cell Analysis in Java
Leave feedback
On this page
This article demonstrates how to compare Excel spreadsheets (XLSX, XLS) using GroupDocs.Comparison for Java. The examples below are ready to use and let developers 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 Java, 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.
Prerequisites
Before proceeding, make sure you have:
Java 8 or later (Java 17 LTS recommended).
Maven or Gradle to build the project.
GroupDocs.Comparison for Java added to your project. See Installation.
A valid GroupDocs.Comparison license file (temporary license available for evaluation).
Project Structure
The examples assume the following layout:
compare-excel-spreadsheets-java/
βββ src/main/java/
β βββ CompareExcelSpreadsheets.java # Main class with all comparison examples
βββ sample-files/ # Input Excel files (source.xlsx, target.xlsx)
βββ output/ # Generated comparison results
βββ pom.xml # Maven project with the GroupDocs.Comparison dependency
Usage Examples
Basic Comparison
The basicComparison method performs a straightforward comparison using default settings:
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");CompareOptionscompareOptions=newCompareOptions();compareOptions.setLeaveGaps(true);try(Comparercomparer=newComparer(sourcePath)){comparer.add(targetPath);comparer.compare(resultPath,compareOptions);}System.out.println("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:
importcom.groupdocs.comparison.license.License;// ...
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:
importjava.io.FileNotFoundException;importjava.io.UncheckedIOException;importjava.nio.file.Files;importjava.nio.file.Paths;// ...
privatestaticvoidensureFileExists(Stringpath,Stringdescription){if(!Files.exists(Paths.get(path))){thrownewUncheckedIOException(newFileNotFoundException("The "+description+" was not found. 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
Call the methods above from main, after applying the license: