icon tracking

Here’s a WPF application using MVVM pattern in C# that:

  • Reads two Excel files.
  • Compares values in Column M of the "Icon Tracker" sheet in the first Excel file with Column G of the "ICON management" sheet in the second Excel file.
  • If a value from Column M is not present in Column G, it writes "Icon not present in management Sheet" in the corresponding Column N of the first Excel file.

πŸ›  Technologies Used

  • C#
  • WPF (MVVM)
  • Microsoft.Office.Interop.Excel

πŸ“ Folder Structure

WpfIconCheckerApp/
├── ViewModels/
│   └── MainViewModel.cs
├── Views/
│   └── MainWindow.xaml
├── Models/
│   └── ExcelService.cs
├── App.xaml
├── MainWindow.xaml.cs
└── WpfIconCheckerApp.csproj

1️⃣ MainWindow.xaml (View)

<Window x:Class="WpfIconCheckerApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:vm="clr-namespace:WpfIconCheckerApp.ViewModels"
        Title="Icon Checker" Height="200" Width="400">
    <Window.DataContext>
        <vm:MainViewModel/>
    </Window.DataContext>
    <Grid Margin="10">
        <StackPanel>
            <Button Content="Select Excel Files and Compare" Command="{Binding CompareCommand}" Margin="0,0,0,10"/>
            <TextBlock Text="{Binding Status}" TextWrapping="Wrap"/>
        </StackPanel>
    </Grid>
</Window>

2️⃣ MainViewModel.cs (ViewModel)

using System.Windows.Input;
using Microsoft.Win32;
using WpfIconCheckerApp.Models;
using System.ComponentModel;

namespace WpfIconCheckerApp.ViewModels
{
    public class MainViewModel : INotifyPropertyChanged
    {
        public ICommand CompareCommand { get; }

        private string _status;
        public string Status
        {
            get => _status;
            set { _status = value; OnPropertyChanged(nameof(Status)); }
        }

        public MainViewModel()
        {
            CompareCommand = new RelayCommand(Compare);
        }

        private void Compare()
        {
            var dialog = new OpenFileDialog { Filter = "Excel Files|*.xlsx", Title = "Select 1st Excel (Icon Tracker)" };
            if (dialog.ShowDialog() != true) return;
            string file1 = dialog.FileName;

            dialog.Title = "Select 2nd Excel (ICON management)";
            if (dialog.ShowDialog() != true) return;
            string file2 = dialog.FileName;

            var result = ExcelService.CompareExcelFiles(file1, file2);
            Status = result;
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string name) =>
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
    }
}

3️⃣ RelayCommand.cs (Helper for ICommand)

using System;
using System.Windows.Input;

public class RelayCommand : ICommand
{
    private readonly Action _execute;
    public event EventHandler CanExecuteChanged;

    public RelayCommand(Action execute) => _execute = execute;
    public bool CanExecute(object parameter) => true;
    public void Execute(object parameter) => _execute();
}

4️⃣ ExcelService.cs (Model/Service Layer)

using Excel = Microsoft.Office.Interop.Excel;
using System.Collections.Generic;
using System.Runtime.InteropServices;

namespace WpfIconCheckerApp.Models
{
    public class ExcelService
    {
        public static string CompareExcelFiles(string file1Path, string file2Path)
        {
            Excel.Application excelApp = null;
            Excel.Workbook workbook1 = null;
            Excel.Workbook workbook2 = null;

            try
            {
                excelApp = new Excel.Application();
                workbook1 = excelApp.Workbooks.Open(file1Path);
                workbook2 = excelApp.Workbooks.Open(file2Path);

                Excel.Worksheet sheet1 = workbook1.Sheets["Icon Tracker"];
                Excel.Worksheet sheet2 = workbook2.Sheets["ICON management"];

                Excel.Range colM = sheet1.Range["M2", sheet1.Cells[sheet1.Rows.Count, "M"].End[Excel.XlDirection.xlUp]];
                Excel.Range colG = sheet2.Range["G2", sheet2.Cells[sheet2.Rows.Count, "G"].End[Excel.XlDirection.xlUp]];

                HashSet<string> valuesInSheet2 = new HashSet<string>();
                foreach (Excel.Range cell in colG.Cells)
                {
                    if (cell.Value2 != null)
                        valuesInSheet2.Add(cell.Value2.ToString().Trim());
                }

                foreach (Excel.Range cell in colM.Cells)
                {
                    string value = cell.Value2?.ToString().Trim();
                    if (!string.IsNullOrEmpty(value) && !valuesInSheet2.Contains(value))
                    {
                        Excel.Range colNCell = cell.Offset[0, 1];
                        colNCell.Value2 = "Icon not present in management Sheet";
                    }
                }

                workbook1.Save();
                return "Comparison completed. Results written in Column N.";
            }
            catch (System.Exception ex)
            {
                return $"Error: {ex.Message}";
            }
            finally
            {
                workbook1?.Close(false);
                workbook2?.Close(false);
                excelApp?.Quit();

                Marshal.ReleaseComObject(workbook1);
                Marshal.ReleaseComObject(workbook2);
                Marshal.ReleaseComObject(excelApp);
            }
        }
    }
}

✅ Prerequisites

  1. Install Microsoft.Office.Interop.Excel via NuGet:
Install-Package Microsoft.Office.Interop.Excel
  1. Make sure Excel is installed on your system.

✅ Output

After selecting two Excel files:

  • Values from Column M in "Icon Tracker" will be checked in Column G of "ICON management".
  • If not found, "Icon not present in management Sheet" will be added to Column N of "Icon Tracker" sheet.
  • The first file will be saved with the updates.

Let me know if you want to export results to a new file, use EPPlus instead of Interop, or want drag-and-drop UI instead.

Comments

Popular posts from this blog

My work

color change

epplus