From 76a8d33c2ac31548d5f209e0c266f1e416238e0f Mon Sep 17 00:00:00 2001 From: Daniel Schick Date: Sat, 4 Oct 2025 10:22:41 +0200 Subject: [PATCH] Replaced ExcelSheetComparer simple functionality --- ENI2/Excel/ExcelComparer.cs | 249 +++++++++++++++--------------------- 1 file changed, 103 insertions(+), 146 deletions(-) diff --git a/ENI2/Excel/ExcelComparer.cs b/ENI2/Excel/ExcelComparer.cs index a78a8e4d..6db1ae7e 100644 --- a/ENI2/Excel/ExcelComparer.cs +++ b/ENI2/Excel/ExcelComparer.cs @@ -4,8 +4,7 @@ using System; using System.IO; -using System.Drawing; -using Microsoft.Office.Interop.Excel; +using ClosedXML.Excel; namespace ENI2.Excel { @@ -17,173 +16,132 @@ namespace ENI2.Excel /// public static class ExcelComparer { - private static readonly int diffColor = ColorTranslator.ToOle(Color.FromArgb(150, 150, 255)); // blue + private static readonly XLColor diffColor = XLColor.FromArgb(150, 150, 255); // blue - private static bool GetSheetRange(Worksheet sheet, out int lastUsedRow, out int lastUsedColumn) + private static bool GetSheetRange(IXLWorksheet sheet, out int lastUsedRow, out int lastUsedColumn) { try { - // sheet.Columns.ClearFormats(); - // sheet.Rows.ClearFormats(); - Range usedRange = sheet.UsedRange; - lastUsedRow = usedRange.Rows.Count; - lastUsedColumn = usedRange.Columns.Count; - - /* - Range last = sheet.Cells.SpecialCells(XlCellType.xlCellTypeLastCell, Type.Missing); - Range range = sheet.get_Range("A1", last); - - lastUsedRow = last.Row; - lastUsedColumn = last.Column; - */ - - return true; + var usedRange = sheet.RangeUsed(); + if (usedRange != null) + { + lastUsedRow = usedRange.RowCount(); + lastUsedColumn = usedRange.ColumnCount(); + return true; + } + else + { + lastUsedColumn = 0; + lastUsedRow = 0; + return false; + } } - catch(Exception) + catch (Exception) { lastUsedColumn = 0; lastUsedRow = 0; return false; } - } - - private static string GetExcelColumnName(int columnNumber) - { - string columnName = ""; - - while (columnNumber > 0) - { - int modulo = (columnNumber - 1) % 26; - columnName = Convert.ToChar('A' + modulo) + columnName; - columnNumber = (columnNumber - modulo) / 26; - } - - return columnName; - } + } public static string Compare(string sourcePath, string targetPath, string comparisonFileName, out string errorMessage) - { + { errorMessage = ""; int counter = 0; try { - File.Copy(targetPath, comparisonFileName); - ExcelReader source = new ExcelReader(sourcePath, true, false); - ExcelReader comparison = new ExcelReader(comparisonFileName, false, false); + File.Copy(targetPath, comparisonFileName, true); - /* erste Variante Vergleich über Namen der Zellen - - // loop through named cells - foreach (string name in comparison.NameDict.Keys) + using (var sourceWorkbook = new XLWorkbook(sourcePath)) + using (var comparisonWorkbook = new XLWorkbook(comparisonFileName)) { - if (!source.NameDict.ContainsKey(name)) continue; - string sourceText = source.ReadText(name); - string targetText = comparison.ReadText(name); - - if (sourceText == null) + // Es werden Zellen der "used range" miteinander verglichen + foreach (var sourceSheet in sourceWorkbook.Worksheets) { - if (targetText != null) + IXLWorksheet targetSheet = null; + foreach (var sheet in comparisonWorkbook.Worksheets) { - comparison.Colorize(name, diffColor); - counter++; - } - } - else if (targetText == null) - { - if (sourceText != null) - { - comparison.Colorize(name, diffColor); - counter++; - } - } - else if ((sourceText != null) && (targetText != null)) - { - if (!sourceText.Equals(targetText)) - { - // turn cell blue - comparison.Colorize(name, diffColor); - counter++; - } - } - } - */ - - // Zweite Version durch alle Sheets werden Zellen der "used range" miteinander verglichen - - foreach(Worksheet sourceSheet in source.Worksheets) - { - Worksheet targetSheet = null; - foreach(Worksheet sheet in comparison.Worksheets) - { - if (sourceSheet.Name.Equals(sheet.Name)) - { - targetSheet = sheet; - break; - } - } - - if (targetSheet == null) continue; - System.Diagnostics.Trace.WriteLine(string.Format("Processing sheet {0}", targetSheet.Name)); - - if(GetSheetRange(sourceSheet, out int sourceRows, out int sourceCols) && GetSheetRange(targetSheet, out int targetRows, out int targetCols)) - { - // read source into 2 dim array - string rangeString = string.Format("A1:{0}{1}", GetExcelColumnName(Math.Max(sourceCols, targetCols)), Math.Max(sourceRows, targetRows)); - object[,] sourceArray = sourceSheet.get_Range(rangeString).Value; - // read target into 2 dim array - object[,] targetArray = targetSheet.get_Range(rangeString).Value; - - for (int rowidx = 1; rowidx <= Math.Max(sourceRows, targetRows); rowidx++) - { - for( int colidx = 1; colidx <= Math.Max(sourceCols, targetCols); colidx++) + if (sourceSheet.Name.Equals(sheet.Name)) { - string sourceText = null; - if(sourceArray[rowidx,colidx] != null) sourceText = sourceArray[rowidx,colidx].ToString(); - string targetText = null; - if(targetArray[rowidx,colidx] != null) targetText = targetArray[rowidx, colidx].ToString(); - - if (sourceText == null) - { - if (targetText != null) - { - string targetRangeName = string.Format("{0}{1}", GetExcelColumnName(colidx), rowidx); - comparison.Colorize(targetSheet.Range[targetRangeName], diffColor); - counter++; - } - } - else if (targetText == null) - { - if (sourceText != null) - { - string targetRangeName = string.Format("{0}{1}", GetExcelColumnName(colidx), rowidx); - comparison.Colorize(targetSheet.Range[targetRangeName], diffColor); - counter++; - } - } - else if ((sourceText != null) && (targetText != null)) - { - if (!sourceText.Equals(targetText)) - { - string targetRangeName = string.Format("{0}{1}", GetExcelColumnName(colidx), rowidx); - // turn cell blue - comparison.Colorize(targetSheet.Range[targetRangeName], diffColor); - counter++; - } - } - + targetSheet = sheet; + break; } } - } - else - { - errorMessage = "failed to get sheet ranges"; - } - } - comparison.Save(comparisonFileName); - source.Dispose(); - comparison.Dispose(); + if (targetSheet == null) continue; + System.Diagnostics.Trace.WriteLine(string.Format("Processing sheet {0}", targetSheet.Name)); + + if (GetSheetRange(sourceSheet, out int sourceRows, out int sourceCols) && GetSheetRange(targetSheet, out int targetRows, out int targetCols)) + { + int maxRows = Math.Max(sourceRows, targetRows); + int maxCols = Math.Max(sourceCols, targetCols); + + for (int rowidx = 1; rowidx <= maxRows; rowidx++) + { + for (int colidx = 1; colidx <= maxCols; colidx++) + { + string sourceText = null; + string targetText = null; + + // Get source cell value + if (rowidx <= sourceRows && colidx <= sourceCols) + { + var sourceCell = sourceSheet.Cell(rowidx, colidx); + if (!sourceCell.IsEmpty()) + { + sourceText = sourceCell.GetString(); + } + } + + // Get target cell value + if (rowidx <= targetRows && colidx <= targetCols) + { + var targetCell = targetSheet.Cell(rowidx, colidx); + if (!targetCell.IsEmpty()) + { + targetText = targetCell.GetString(); + } + } + + if (sourceText == null) + { + if (targetText != null) + { + var cellToHighlight = targetSheet.Cell(rowidx, colidx); + cellToHighlight.Style.Fill.BackgroundColor = diffColor; + counter++; + } + } + else if (targetText == null) + { + if (sourceText != null) + { + var cellToHighlight = targetSheet.Cell(rowidx, colidx); + cellToHighlight.Style.Fill.BackgroundColor = diffColor; + counter++; + } + } + else if ((sourceText != null) && (targetText != null)) + { + if (!sourceText.Equals(targetText)) + { + var cellToHighlight = targetSheet.Cell(rowidx, colidx); + cellToHighlight.Style.Fill.BackgroundColor = diffColor; + counter++; + } + } + } + } + } + else + { + errorMessage = "failed to get sheet ranges"; + } + } + + comparisonWorkbook.SaveAs(comparisonFileName); + } errorMessage = string.Format("{0} differences found", counter); } catch (Exception ex) @@ -193,6 +151,5 @@ namespace ENI2.Excel return comparisonFileName; } - } -} +} \ No newline at end of file