Replaced ExcelSheetComparer simple functionality
This commit is contained in:
parent
7a7ea56c0f
commit
76a8d33c2a
@ -4,8 +4,7 @@
|
|||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Drawing;
|
using ClosedXML.Excel;
|
||||||
using Microsoft.Office.Interop.Excel;
|
|
||||||
|
|
||||||
namespace ENI2.Excel
|
namespace ENI2.Excel
|
||||||
{
|
{
|
||||||
@ -17,29 +16,27 @@ namespace ENI2.Excel
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public static class ExcelComparer
|
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
|
try
|
||||||
{
|
{
|
||||||
// sheet.Columns.ClearFormats();
|
var usedRange = sheet.RangeUsed();
|
||||||
// sheet.Rows.ClearFormats();
|
if (usedRange != null)
|
||||||
Range usedRange = sheet.UsedRange;
|
{
|
||||||
lastUsedRow = usedRange.Rows.Count;
|
lastUsedRow = usedRange.RowCount();
|
||||||
lastUsedColumn = usedRange.Columns.Count;
|
lastUsedColumn = usedRange.ColumnCount();
|
||||||
|
return true;
|
||||||
/*
|
}
|
||||||
Range last = sheet.Cells.SpecialCells(XlCellType.xlCellTypeLastCell, Type.Missing);
|
else
|
||||||
Range range = sheet.get_Range("A1", last);
|
{
|
||||||
|
lastUsedColumn = 0;
|
||||||
lastUsedRow = last.Row;
|
lastUsedRow = 0;
|
||||||
lastUsedColumn = last.Column;
|
return false;
|
||||||
*/
|
}
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
catch(Exception)
|
catch (Exception)
|
||||||
{
|
{
|
||||||
lastUsedColumn = 0;
|
lastUsedColumn = 0;
|
||||||
lastUsedRow = 0;
|
lastUsedRow = 0;
|
||||||
@ -47,20 +44,6 @@ namespace ENI2.Excel
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
public static string Compare(string sourcePath, string targetPath, string comparisonFileName, out string errorMessage)
|
||||||
{
|
{
|
||||||
errorMessage = "";
|
errorMessage = "";
|
||||||
@ -68,122 +51,97 @@ namespace ENI2.Excel
|
|||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
File.Copy(targetPath, comparisonFileName);
|
File.Copy(targetPath, comparisonFileName, true);
|
||||||
ExcelReader source = new ExcelReader(sourcePath, true, false);
|
|
||||||
ExcelReader comparison = new ExcelReader(comparisonFileName, false, false);
|
|
||||||
|
|
||||||
/* erste Variante Vergleich über Namen der Zellen
|
using (var sourceWorkbook = new XLWorkbook(sourcePath))
|
||||||
|
using (var comparisonWorkbook = new XLWorkbook(comparisonFileName))
|
||||||
// loop through named cells
|
|
||||||
foreach (string name in comparison.NameDict.Keys)
|
|
||||||
{
|
{
|
||||||
if (!source.NameDict.ContainsKey(name)) continue;
|
// Es werden Zellen der "used range" miteinander verglichen
|
||||||
string sourceText = source.ReadText(name);
|
foreach (var sourceSheet in sourceWorkbook.Worksheets)
|
||||||
string targetText = comparison.ReadText(name);
|
|
||||||
|
|
||||||
if (sourceText == null)
|
|
||||||
{
|
{
|
||||||
if (targetText != null)
|
IXLWorksheet targetSheet = null;
|
||||||
|
foreach (var sheet in comparisonWorkbook.Worksheets)
|
||||||
{
|
{
|
||||||
comparison.Colorize(name, diffColor);
|
if (sourceSheet.Name.Equals(sheet.Name))
|
||||||
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++)
|
|
||||||
{
|
{
|
||||||
string sourceText = null;
|
targetSheet = sheet;
|
||||||
if(sourceArray[rowidx,colidx] != null) sourceText = sourceArray[rowidx,colidx].ToString();
|
break;
|
||||||
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++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
errorMessage = "failed to get sheet ranges";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
comparison.Save(comparisonFileName);
|
if (targetSheet == null) continue;
|
||||||
source.Dispose();
|
System.Diagnostics.Trace.WriteLine(string.Format("Processing sheet {0}", targetSheet.Name));
|
||||||
comparison.Dispose();
|
|
||||||
|
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);
|
errorMessage = string.Format("{0} differences found", counter);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
@ -193,6 +151,5 @@ namespace ENI2.Excel
|
|||||||
|
|
||||||
return comparisonFileName;
|
return comparisonFileName;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user