diff --git a/ENI2/ENI2.csproj b/ENI2/ENI2.csproj
index 1af7785f..686ed3ec 100644
--- a/ENI2/ENI2.csproj
+++ b/ENI2/ENI2.csproj
@@ -239,6 +239,7 @@
+
diff --git a/ENI2/EditControls/CompareExcelDialog.xaml b/ENI2/EditControls/CompareExcelDialog.xaml
index ba0eb3dc..cf243be2 100644
--- a/ENI2/EditControls/CompareExcelDialog.xaml
+++ b/ENI2/EditControls/CompareExcelDialog.xaml
@@ -28,7 +28,7 @@
-
+
-
+
diff --git a/ENI2/EditControls/CompareExcelDialog.xaml.cs b/ENI2/EditControls/CompareExcelDialog.xaml.cs
index d36df072..f7cbc548 100644
--- a/ENI2/EditControls/CompareExcelDialog.xaml.cs
+++ b/ENI2/EditControls/CompareExcelDialog.xaml.cs
@@ -1,7 +1,12 @@
-using System;
+// Copyright (c) 2017- schick Informatik
+// Description: Dialogbox zum Vergleichen zweier Excel via Named cells
+//
+
+using bsmd.database;
+using System;
+using System.Diagnostics;
using System.IO;
using System.Windows;
-using System.Windows.Media.Imaging;
namespace ENI2.EditControls
{
@@ -10,14 +15,19 @@ namespace ENI2.EditControls
///
public partial class CompareExcelDialog : Controls.StatusWindowBase
{
+
+ #region Fields
private string _sourcePath = null;
private string _targetPath = null;
+ #endregion
public CompareExcelDialog()
{
InitializeComponent();
}
+ #region Drag&Drop event handler
+
private void imageSource_Drop(object sender, DragEventArgs e)
{
string[] files = (string[]) e.Data.GetData(DataFormats.FileDrop);
@@ -84,14 +94,50 @@ namespace ENI2.EditControls
}
}
+ private void textBoxSource_PreviewDragOver(object sender, DragEventArgs e)
+ {
+ e.Handled = true;
+ }
+
+ #endregion
+
+ #region private methods
+
private void EnableCompareButton()
{
this.buttonCompare.IsEnabled = (_targetPath != null) && (_sourcePath != null);
}
+ #endregion
+
+ #region Comparison button handler logic
+
private void buttonCompare_Click(object sender, RoutedEventArgs e)
{
+ Util.UIHelper.SetBusyState();
+ string resultPath = Excel.ExcelComparer.Compare(_sourcePath, _targetPath, out string errorMessage);
+ if(!errorMessage.IsNullOrEmpty()) {
+ MessageBox.Show(errorMessage, "Comparison error", MessageBoxButton.OK, MessageBoxImage.Warning);
+ }
+ if(File.Exists(resultPath))
+ {
+ if(new FileInfo(resultPath).Length > 0)
+ {
+ Process.Start(resultPath);
+ }
+ }
+ // reset input values
+ this.textBoxSource.Text = null;
+ this.textBoxTarget.Text = null;
+ this._sourcePath = null;
+ this._targetPath = null;
+ EnableCompareButton();
+
+ this.Close();
}
+
+ #endregion
+
}
}
diff --git a/ENI2/Excel/ExcelBase.cs b/ENI2/Excel/ExcelBase.cs
index 34a2df7f..753ee783 100644
--- a/ENI2/Excel/ExcelBase.cs
+++ b/ENI2/Excel/ExcelBase.cs
@@ -29,7 +29,7 @@ namespace ENI2.Excel
#region Construction
- public ExcelBase(string filePath)
+ public ExcelBase()
{
_log = LogManager.GetLogger(this.GetType().Name);
@@ -45,6 +45,8 @@ namespace ENI2.Excel
internal CountryMode Mode { get { return _countryMode; } }
+ internal Dictionary NameDict { get { return _nameDict; } }
+
#endregion
#region protected methods
@@ -136,6 +138,26 @@ namespace ENI2.Excel
return result;
}
+ internal void Colorize(string lookup, int color)
+ {
+ if(_nameDict.ContainsKey(lookup))
+ {
+ var range = _nameDict[lookup].RefersToRange;
+ range.Interior.Color = color;
+ }
+ }
+
+ #endregion
+
+ #region public methods
+
+ public void Save(string path)
+ {
+ this._workBook.SaveAs(path, XlFileFormat.xlOpenXMLWorkbook, Type.Missing, Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlNoChange,
+ Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
+ this._workBook.Saved = true;
+ }
+
#endregion
#region Dispose
diff --git a/ENI2/Excel/ExcelComparer.cs b/ENI2/Excel/ExcelComparer.cs
new file mode 100644
index 00000000..93f30c47
--- /dev/null
+++ b/ENI2/Excel/ExcelComparer.cs
@@ -0,0 +1,75 @@
+// Copyright (c) 2022- schick Informatik
+// Description: Compares excel files and highlights changes
+//
+
+using System;
+using System.IO;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.Drawing;
+
+namespace ENI2.Excel
+{
+ public static class ExcelComparer
+ {
+ private static int diffColor = ColorTranslator.ToOle(Color.FromArgb(150, 150, 255)); // blue
+
+ public static string Compare(string sourcePath, string targetPath, out string errorMessage)
+ {
+ string fileName = Path.GetTempPath() + Guid.NewGuid().ToString() + ".xlsx";
+ errorMessage = "";
+ int counter = 0;
+
+ try
+ {
+ File.Copy(targetPath, fileName);
+ ExcelReader source = new ExcelReader(sourcePath);
+ ExcelReader comparison = new ExcelReader(fileName, false);
+
+ // loop through named cells
+ foreach (string name in comparison.NameDict.Keys)
+ {
+ if (!source.NameDict.ContainsKey(name)) continue;
+ string sourceText = source.ReadText(name);
+ string targetText = comparison.ReadText(name);
+ if (sourceText == null)
+ {
+ if (targetText != null)
+ {
+ comparison.Colorize(name, diffColor);
+ counter++;
+ }
+ }
+ if (targetText == null)
+ {
+ if (sourceText != null)
+ {
+ comparison.Colorize(name, diffColor);
+ counter++;
+ }
+ }
+ if ((sourceText != null) && (targetText != null))
+ {
+ if (!sourceText.Equals(targetText))
+ {
+ // turn cell blue
+ comparison.Colorize(name, diffColor);
+ counter++;
+ }
+ }
+ }
+ comparison.Save(fileName);
+ errorMessage = string.Format("{0} differences found", counter);
+ }
+ catch (Exception ex)
+ {
+ errorMessage = ex.Message;
+ }
+
+ return fileName;
+ }
+
+ }
+}
diff --git a/ENI2/Excel/ExcelManager.cs b/ENI2/Excel/ExcelManager.cs
index 14ae76b3..ee169071 100644
--- a/ENI2/Excel/ExcelManager.cs
+++ b/ENI2/Excel/ExcelManager.cs
@@ -83,7 +83,7 @@ namespace ENI2.Excel
using (ExcelWriter ew = new ExcelWriter(fileName, isRefSheet))
{
ew.WriteData(messages, core, out resultMessage, isRefSheet);
- ew.Save();
+ ew.Save(fileName);
}
}
}
diff --git a/ENI2/Excel/ExcelReader.cs b/ENI2/Excel/ExcelReader.cs
index c5fe8066..2c1af6ce 100644
--- a/ENI2/Excel/ExcelReader.cs
+++ b/ENI2/Excel/ExcelReader.cs
@@ -23,7 +23,7 @@ using System.Text.RegularExpressions;
namespace ENI2.Excel
{
internal class ExcelReader : ExcelBase
- {
+ {
internal enum ReadState { NONE, OK, WARN, FAIL };
@@ -33,9 +33,9 @@ namespace ENI2.Excel
internal Dictionary ImportValues { get; } = new Dictionary();
- public ExcelReader(string filePath) : base(filePath)
+ public ExcelReader(string filePath, bool openReadonly = true)
{
- this._workBook = _excelWorkbooks.Open(filePath, 0, true, 5, "", "", false, XlPlatform.xlWindows, "", false, false, 0, false, false, false);
+ this._workBook = _excelWorkbooks.Open(filePath, 0, openReadonly, 5, "", "", false, XlPlatform.xlWindows, "", false, false, 0, false, false, false);
this.InitNameFields();
// Determine if this is a Dakosy or BSMD Sheet
@@ -100,7 +100,7 @@ namespace ENI2.Excel
string val = this.ReadText(lookup);
if (!val.IsNullOrEmpty())
{
- val = val.ToUpper();
+ val = val.ToUpper();
string portName = LocodeDB.PortNameFromLocode(val);
if (!justPorts) portName = LocodeDB.NameFromLocode(val);
if (portName.IsNullOrEmpty())
diff --git a/ENI2/Excel/ExcelUtil.cs b/ENI2/Excel/ExcelUtil.cs
index 62d4d3dd..e6e53e21 100644
--- a/ENI2/Excel/ExcelUtil.cs
+++ b/ENI2/Excel/ExcelUtil.cs
@@ -21,6 +21,8 @@ namespace ENI2.Excel
{
private static readonly ILog _log = LogManager.GetLogger(typeof(ExcelUtil));
+ #region Process Sheet (normal BSMD sheet import)
+
internal static bool ProcessSheet(ExcelReader reader, out string readMessage, MessageCore messageCore, List notificationClasses)
{
bool result = true;
@@ -134,7 +136,9 @@ namespace ENI2.Excel
}
return result;
- }
+ }
+
+ #endregion
#region ATA
@@ -2343,7 +2347,7 @@ namespace ENI2.Excel
if (sheetValue != null)
{
property.SetValue(dbEntity, sheetValue);
- }
+ }
}
else if (property.PropertyType == typeof(double?))
{
@@ -2351,7 +2355,7 @@ namespace ENI2.Excel
if (sheetValue != null)
{
property.SetValue(dbEntity, sheetValue);
- }
+ }
}
else if (property.PropertyType == typeof(string))
{
@@ -2359,7 +2363,7 @@ namespace ENI2.Excel
if (sheetValue != null)
{
property.SetValue(dbEntity, sheetValue);
- }
+ }
}
else if (property.PropertyType == typeof(int?))
{
@@ -2367,7 +2371,7 @@ namespace ENI2.Excel
if (sheetValue.HasValue)
{
property.SetValue(dbEntity, (int)sheetValue.Value);
- }
+ }
}
else if (property.PropertyType == typeof(byte?))
{
@@ -2375,7 +2379,7 @@ namespace ENI2.Excel
if (sheetValue.HasValue)
{
property.SetValue(dbEntity, (byte)sheetValue.Value);
- }
+ }
}
else if (property.PropertyType == typeof(Boolean?))
{
@@ -2383,7 +2387,7 @@ namespace ENI2.Excel
string boolStringValue = reader.ReadText(lookupNameAttribute.LookupName);
if (sheetValue.HasValue) {
property.SetValue(dbEntity, sheetValue);
- }
+ }
}
else
{
diff --git a/ENI2/Excel/ExcelWriter.cs b/ENI2/Excel/ExcelWriter.cs
index 9a0c9098..6f06015e 100644
--- a/ENI2/Excel/ExcelWriter.cs
+++ b/ENI2/Excel/ExcelWriter.cs
@@ -18,15 +18,11 @@ namespace ENI2.Excel
internal class ExcelWriter : ExcelBase
{
- #region Fields
-
- private readonly string _saveFilePath;
-
- #endregion
+
#region Construction
- public ExcelWriter(string filePath, bool isRefSheet) : base(filePath)
+ public ExcelWriter(string filePath, bool isRefSheet)
{
string filename = @"Excel\EU-NoAD-Data-Collecting-Tool-5_0.xlsx";
if (isRefSheet) filename = @"Excel\Reference_Sheet_DE.xlsx";
@@ -35,7 +31,7 @@ namespace ENI2.Excel
this._workBook = _excelWorkbooks.Open(refFilePath, 0, true, 5, "", "", false, XlPlatform.xlWindows, "", false, false, 0, false, false, false);
this.InitNameFields();
- _saveFilePath = filePath;
+
}
#endregion
@@ -196,13 +192,6 @@ namespace ENI2.Excel
WriteCore(core, isRefSheet);
- }
-
- public void Save()
- {
- this._workBook.SaveAs(_saveFilePath, XlFileFormat.xlOpenXMLWorkbook, Type.Missing, Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlNoChange,
- Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
- this._workBook.Saved = true;
}
#endregion