epplus
Perfect! ✅ Let's replace Microsoft.Office.Interop.Excel
with EPPlus, which:
- Doesn't require Microsoft Excel to be installed.
- Is much faster and more stable for server/desktop apps.
🔧 Updated Requirements
- Read 2 Excel files.
- In File 1,
"Icon Tracker"
sheet → get Column M values. - In File 2,
"ICON management"
sheet → get Column G values. - If value in Column M is not in Column G, write
"Icon not present in management Sheet"
in Column N. - Use EPPlus (works on
.xlsx
only).
📦 Install EPPlus via NuGet
Run this in the NuGet Package Manager:
Install-Package EPPlus
🧠 Updated ExcelService.cs
using EPPlus
using OfficeOpenXml;
using System.Collections.Generic;
using System.IO;
namespace WpfIconCheckerApp.Models
{
public class ExcelService
{
public static string CompareExcelFiles(string file1Path, string file2Path)
{
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
try
{
// Load both Excel files
using var package1 = new ExcelPackage(new FileInfo(file1Path));
using var package2 = new ExcelPackage(new FileInfo(file2Path));
var sheet1 = package1.Workbook.Worksheets["Icon Tracker"];
var sheet2 = package2.Workbook.Worksheets["ICON management"];
if (sheet1 == null || sheet2 == null)
return "One of the required sheets is missing.";
// Read all values from Column G in sheet2 into a HashSet for fast lookup
var iconsInManagementSheet = new HashSet<string>();
int gRow = 2;
while (!string.IsNullOrWhiteSpace(sheet2.Cells[gRow, 7].Text))
{
iconsInManagementSheet.Add(sheet2.Cells[gRow, 7].Text.Trim());
gRow++;
}
// Loop through Column M in sheet1 and compare
int mRow = 2;
int unmatchedCount = 0;
while (!string.IsNullOrWhiteSpace(sheet1.Cells[mRow, 13].Text))
{
string icon = sheet1.Cells[mRow, 13].Text.Trim();
if (!iconsInManagementSheet.Contains(icon))
{
sheet1.Cells[mRow, 14].Value = "Icon not present in management Sheet"; // Column N
unmatchedCount++;
}
mRow++;
}
// Save updated file (overwrite or create new one)
package1.Save();
return $"Comparison done. {unmatchedCount} icons not found. Results written in Column N.";
}
catch (System.Exception ex)
{
return $"Error: {ex.Message}";
}
}
}
}
📝 Notes
- Column M = column 13
- Column G = column 7
- Column N = column 14
- EPPlus uses 1-based indexing
- File is saved in-place (same file is modified). Let me know if you want to save it to a new file.
✅ No Change Needed in UI or ViewModel
Everything else in MainViewModel
and MainWindow.xaml
remains the same.
Would you like me to:
- Save the result to a new Excel file?
- Show a file path to the updated file?
- Add logging or preview in a DataGrid?
Let me know how you'd like to extend it!
Comments
Post a Comment