Replaced ExcelSheetComparer simple functionality
This commit is contained in:
parent
7938e5ab6e
commit
5f90e51691
@ -4,8 +4,7 @@
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Drawing;
|
||||
using Microsoft.Office.Interop.Excel;
|
||||
using ClosedXML.Excel;
|
||||
|
||||
namespace ENI2.Excel
|
||||
{
|
||||
@ -17,29 +16,27 @@ 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;
|
||||
*/
|
||||
|
||||
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;
|
||||
@ -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)
|
||||
{
|
||||
errorMessage = "";
|
||||
@ -68,122 +51,97 @@ 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)
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user