How to compare cell values in Excel using C#, VB.NET?
Syncfusion Excel (XlsIO) library is a .NET Excel library used to create, read, and edit Excel documents. It also converts Excel documents to PDF files.
This article explains how to compare cell values in Excel using
Include the following namespace in the Program.cs file.
C#
using Syncfusion.XlsIO;
VB.NET
Imports Syncfusion.XlsIO
Use the following code snippet for comparing cell values in Excel.
C#
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
FileStream inputStream = new FileStream("Data/Input.xlsx", FileMode.Open, FileAccess.Read);
IWorkbook workbook = application.Workbooks.Open(inputStream);
IWorksheet worksheet = workbook.Worksheets[0];
IRange usedRange = worksheet.UsedRange;
// Loop through rows (starting from row 2 to skip header if needed)
for (int row = 2; row <= usedRange.LastRow; row++)
{
// Compare values between column 1 and column 2
if (worksheet.UsedRange[row, 1].Value != worksheet.UsedRange[row, 2].Value && !worksheet.UsedRange[row, 2].IsBlank)
{
// Highlight mismatched cells with red color
worksheet.UsedRange[row, 2].CellStyle.ColorIndex = ExcelKnownColors.Red;
}
}
#region Save
// Saving the workbook
FileStream outputStream = new FileStream(Path.GetFullPath("Output/Output.xlsx"), FileMode.Create, FileAccess.Write);
workbook.SaveAs(outputStream);
#endregion
// Dispose streams
outputStream.Dispose();
inputStream.Dispose();
}
VB.NET
Using excelEngine As New ExcelEngine()
Dim application As IApplication = excelEngine.Excel
application.DefaultVersion = ExcelVersion.Xlsx
Dim workbook As IWorkbook = application.Workbooks.Open("Input.xlsx")
Dim worksheet As IWorksheet = workbook.Worksheets(0)
Dim usedRange As IRange = worksheet.UsedRange
' Loop through rows (starting from row 2 to skip header if needed)
For row As Integer = 2 To usedRange.LastRow
Dim value1 As String = worksheet.UsedRange(row, 1).Value
Dim value2 As String = worksheet.UsedRange(row, 2).Value
' Compare values between column 1 and column 2
If value1 <> value2 AndAlso Not worksheet.UsedRange(row, 2).IsBlank Then
' Highlight mismatched cells with red color
worksheet.UsedRange(row, 2).CellStyle.ColorIndex = ExcelKnownColors.Red
End If
Next
' Saving the workbook
workbook.SaveAs("Output.xlsx")
End Using
You can get the complete sample for comparing cell values in Excel from the below link :
I hope you enjoyed learning about how to compare cell values in Excel using
Take a moment to peruse the documentation where you can find basic Excel document processing options along with features like import and export data, charts, formulas, conditional formatting, data validation, tables, pivot tables, and protect Excel documents, as well as the PDF, CSV, and Image conversions with code examples.
You can refer to our XIsIO’s feature tour page to learn about its other groundbreaking features. Explore our UG documentation and online demos to understand how to manipulate data in Excel documents.
If you are an existing user, you can access our latest components from the License and Downloads page. For new users, you can try our 30-day free trial to check out XlsIO and other Syncfusion components.
If you have any queries or require clarification, please let us know in the comments below or contact us through our support forums, Support Tickets, or feedback portal. We are always happy to assist you!