close

Great choice! ✅ ClosedXML is:

  • ๐Ÿ’ฏ Free for commercial and non-commercial use (MIT License).
  • ๐Ÿงผ Easier to use than EPPlus.
  • ๐Ÿ“ Works with .xlsx files only.

๐ŸŽฏ Goal Recap

We will use ClosedXML to:

  • Open two Excel files.
  • In File 1, sheet "Icon Tracker", read values from Column M.
  • In File 2, sheet "ICON management", read values from Column G.
  • For each value in Column M:
    • If it’s not found in Column G → write "Icon not present in management Sheet" in Column N (same row).
  • Save File 1 with the updates.

๐Ÿ“ฆ Install ClosedXML

In Package Manager Console:

Install-Package ClosedXML

๐Ÿ“„ ExcelService.cs Using ClosedXML

using ClosedXML.Excel;
using System;
using System.Collections.Generic;
using System.IO;

namespace WpfIconCheckerApp.Models
{
    public class ExcelService
    {
        public static string CompareExcelFiles(string file1Path, string file2Path)
        {
            try
            {
                using var workbook1 = new XLWorkbook(file1Path);
                using var workbook2 = new XLWorkbook(file2Path);

                var sheet1 = workbook1.Worksheet("Icon Tracker");
                var sheet2 = workbook2.Worksheet("ICON management");

                if (sheet1 == null || sheet2 == null)
                    return "Required sheets not found in one or both Excel files.";

                // Read all values from Column G in ICON management sheet
                var iconsInManagement = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
                int row = 2;
                while (!sheet2.Cell(row, 7).IsEmpty()) // Column G
                {
                    var value = sheet2.Cell(row, 7).GetString().Trim();
                    if (!string.IsNullOrEmpty(value))
                        iconsInManagement.Add(value);
                    row++;
                }

                // Check Column M in Icon Tracker sheet and write to Column N
                int row1 = 2;
                int unmatchedCount = 0;
                while (!sheet1.Cell(row1, 13).IsEmpty()) // Column M
                {
                    var iconValue = sheet1.Cell(row1, 13).GetString().Trim();

                    if (!iconsInManagement.Contains(iconValue))
                    {
                        sheet1.Cell(row1, 14).Value = "Icon not present in management Sheet"; // Column N
                        unmatchedCount++;
                    }

                    row1++;
                }

                // Save updated File 1
                workbook1.Save();

                return $"Comparison completed. {unmatchedCount} icon(s) not found. Results saved in Column N.";
            }
            catch (Exception ex)
            {
                return $"Error: {ex.Message}";
            }
        }
    }
}

๐Ÿ“‘ Notes

  • Column G = column 7
  • Column M = column 13
  • Column N = column 14
  • This code modifies File 1 in place. Let me know if you want to save to a new file.

✅ Next Steps

This works smoothly with the rest of the WPF MVVM app. No changes are needed in MainViewModel or the UI.

Would you like me to:

  • Save the result as a new Excel file instead?
  • Add a preview window of unmatched rows?
  • Extend this to support CSV input or drag-and-drop?

Let me know what enhancement you'd like!

Comments

Popular posts from this blog

My work

color change