I have an excel file and I am trying to find matching values based on these criteria.
- For ARMADAS B2: D542295890, I am getting the ARMADAW A2:B05KAYIP
- I will look for B05KAYIP in the E column ARMADAIWC and get the corresponding D SAPIWC value 0010
- Then I look for 0010 in column G SAPW and get the matching value of D542295890 ARMADAS B2.
- If B and H match then I want to color them.
This console application finds the matching values but since there are approx. 400K rows of data for both B and H to match it takes ages to finish. I wonder how I can optimize this code to get the matching results like in my code way faster?
using OfficeOpenXml;
using OfficeOpenXml.Style;
using System.Drawing;
string filePath = @"C:\Users\197199\Desktop\SAP-Armada\Test.xlsx";
string logFilePath = @"C:\Users\197199\Desktop\SAP-Armada\matches.txt";
// Ensure the file exists
if (!File.Exists(filePath))
{
Console.WriteLine("File not found.");
return;
}
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
// Load the Excel file
using var package = new ExcelPackage(new FileInfo(filePath));
// Get the first worksheet in the workbook
var worksheet = package.Workbook.Worksheets[0];
// Define column indices (assuming 1-based index)
int armadasCol = 2; // Replace with actual column index for ARMADAS
int armadawCol = 1; // Replace with actual column index for ARMADAW
int armadaiWCCol = 5; // Replace with actual column index for ARMADAIWC
int sapiWCCol = 4; // Replace with actual column index for SAPIWC
int gCol = 7; // Replace with actual column index for G
int sapsCol = 8; // Replace with actual column index for SAPS
using (StreamWriter writer = new StreamWriter(logFilePath))
{
// Read the content of the worksheet and perform the comparison
for (int row = 2; row <= worksheet.Dimension.End.Row; row++) // Assuming first row is header
{
string armadasValue = worksheet.Cells[row, armadasCol].Text;
string armadawValue = worksheet.Cells[row, armadawCol].Text;
if (string.IsNullOrEmpty(armadasValue) || string.IsNullOrEmpty(armadawValue))
{
continue; // Skip empty rows
}
// Find the corresponding value in ARMADAIWC and SAPIWC
string sapiWCValue = null;
for (int i = 2; i <= worksheet.Dimension.End.Row; i++)
{
if (worksheet.Cells[i, armadaiWCCol].Text == armadawValue)
{
sapiWCValue = worksheet.Cells[i, sapiWCCol].Text;
break;
}
}
if (string.IsNullOrEmpty(sapiWCValue))
{
continue; // Skip if no corresponding SAPIWC value is found
}
// Find the corresponding value in SAPS
string sapsValue = null;
for (int j = 2; j <= worksheet.Dimension.End.Row; j++)
{
if (worksheet.Cells[j, gCol].Text == sapiWCValue)
{
if (armadasValue == worksheet.Cells[j, sapsCol].Text)
{
sapsValue = worksheet.Cells[j, sapsCol].Text;
// Apply coloring to the matching cells
worksheet.Cells[row, armadasCol].Style.Fill.PatternType = ExcelFillStyle.Solid;
worksheet.Cells[row, armadasCol].Style.Fill.BackgroundColor.SetColor(Color.Yellow);
worksheet.Cells[j, sapsCol].Style.Fill.PatternType = ExcelFillStyle.Solid;
worksheet.Cells[j, sapsCol].Style.Fill.BackgroundColor.SetColor(Color.Yellow);
string message = $"Match found at row {row}: ARMADA WAREHOUSE = {armadawValue}, ARMADA SERIAL = {armadasValue}, SAP WAREHOUSE = {sapiWCValue}, SAP SERIAL = {sapsValue}{Environment.NewLine}";
Console.WriteLine(message);
writer.WriteLine(message);
break;
}
}
}
if (string.IsNullOrEmpty(sapsValue))
{
continue; // Skip if no corresponding SAPS value is found
}
}
}
// Save the changes
package.Save();
Console.WriteLine("Comparison and coloring completed.");
