Replaced ExcelSheetComparer simple functionality

This commit is contained in:
Daniel Schick 2025-10-04 10:22:41 +02:00
parent 7938e5ab6e
commit 5f90e51691

View File

@ -4,8 +4,7 @@
using System;
using System.IO;
using System.Drawing;
using Microsoft.Office.Interop.Excel;
using ClosedXML.Excel;
namespace ENI2.Excel
{
@ -17,48 +16,32 @@ namespace ENI2.Excel
/// </summary>
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;
*/
var usedRange = sheet.RangeUsed();
if (usedRange != null)
{
lastUsedRow = usedRange.RowCount();
lastUsedColumn = usedRange.ColumnCount();
return true;
}
catch(Exception)
else
{
lastUsedColumn = 0;
lastUsedRow = 0;
return false;
}
}
private static string GetExcelColumnName(int columnNumber)
catch (Exception)
{
string columnName = "";
while (columnNumber > 0)
{
int modulo = (columnNumber - 1) % 26;
columnName = Convert.ToChar('A' + modulo) + columnName;
columnNumber = (columnNumber - modulo) / 26;
lastUsedColumn = 0;
lastUsedRow = 0;
return false;
}
return columnName;
}
public static string Compare(string sourcePath, string targetPath, string comparisonFileName, out string errorMessage)
@ -68,53 +51,16 @@ namespace ENI2.Excel
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)
{
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)
IXLWorksheet targetSheet = null;
foreach (var sheet in comparisonWorkbook.Worksheets)
{
if (sourceSheet.Name.Equals(sheet.Name))
{
@ -126,29 +72,44 @@ namespace ENI2.Excel
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))
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;
int maxRows = Math.Max(sourceRows, targetRows);
int maxCols = Math.Max(sourceCols, targetCols);
for (int rowidx = 1; rowidx <= Math.Max(sourceRows, targetRows); rowidx++)
for (int rowidx = 1; rowidx <= maxRows; rowidx++)
{
for( int colidx = 1; colidx <= Math.Max(sourceCols, targetCols); colidx++)
for (int colidx = 1; colidx <= maxCols; colidx++)
{
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();
// 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)
{
string targetRangeName = string.Format("{0}{1}", GetExcelColumnName(colidx), rowidx);
comparison.Colorize(targetSheet.Range[targetRangeName], diffColor);
var cellToHighlight = targetSheet.Cell(rowidx, colidx);
cellToHighlight.Style.Fill.BackgroundColor = diffColor;
counter++;
}
}
@ -156,8 +117,8 @@ namespace ENI2.Excel
{
if (sourceText != null)
{
string targetRangeName = string.Format("{0}{1}", GetExcelColumnName(colidx), rowidx);
comparison.Colorize(targetSheet.Range[targetRangeName], diffColor);
var cellToHighlight = targetSheet.Cell(rowidx, colidx);
cellToHighlight.Style.Fill.BackgroundColor = diffColor;
counter++;
}
}
@ -165,13 +126,11 @@ namespace ENI2.Excel
{
if (!sourceText.Equals(targetText))
{
string targetRangeName = string.Format("{0}{1}", GetExcelColumnName(colidx), rowidx);
// turn cell blue
comparison.Colorize(targetSheet.Range[targetRangeName], diffColor);
var cellToHighlight = targetSheet.Cell(rowidx, colidx);
cellToHighlight.Style.Fill.BackgroundColor = diffColor;
counter++;
}
}
}
}
}
@ -181,9 +140,8 @@ namespace ENI2.Excel
}
}
comparison.Save(comparisonFileName);
source.Dispose();
comparison.Dispose();
comparisonWorkbook.SaveAs(comparisonFileName);
}
errorMessage = string.Format("{0} differences found", counter);
}
catch (Exception ex)
@ -193,6 +151,5 @@ namespace ENI2.Excel
return comparisonFileName;
}
}
}