Comparer ist funktional :-)

This commit is contained in:
Daniel Schick 2022-06-17 11:20:58 +02:00
parent ab357ff88d
commit f1f18c9835
9 changed files with 168 additions and 31 deletions

View File

@ -239,6 +239,7 @@
</Compile> </Compile>
<Compile Include="Excel\DakosyUtil.cs" /> <Compile Include="Excel\DakosyUtil.cs" />
<Compile Include="Excel\ExcelBase.cs" /> <Compile Include="Excel\ExcelBase.cs" />
<Compile Include="Excel\ExcelComparer.cs" />
<Compile Include="Excel\ExcelManager.cs" /> <Compile Include="Excel\ExcelManager.cs" />
<Compile Include="Excel\ExcelReader.cs" /> <Compile Include="Excel\ExcelReader.cs" />
<Compile Include="Excel\ExcelUtil.cs" /> <Compile Include="Excel\ExcelUtil.cs" />

View File

@ -28,7 +28,7 @@
</AccessText> </AccessText>
</Label> </Label>
<Image x:Name="imageSource" Grid.Row="1" Grid.Column="1" Margin="4" Source="../Resources/import2.png" AllowDrop="True" Drop="imageSource_Drop" DragEnter="imageSource_DragEnter"/> <Image x:Name="imageSource" Grid.Row="1" Grid.Column="1" Margin="4" Source="../Resources/import2.png" AllowDrop="True" Drop="imageSource_Drop" DragEnter="imageSource_DragEnter"/>
<TextBox TextWrapping="Wrap" x:Name="textBoxSource" Grid.Row="1" Grid.Column="2" IsReadOnly="True" Margin="2" Drop="imageSource_Drop" AllowDrop="True" DragEnter="imageSource_DragEnter"/> <TextBox TextWrapping="Wrap" x:Name="textBoxSource" Grid.Row="1" Grid.Column="2" IsReadOnly="True" Margin="2" Drop="imageSource_Drop" AllowDrop="True" DragEnter="imageSource_DragEnter" PreviewDragOver="textBoxSource_PreviewDragOver"/>
<Label VerticalAlignment="Center" Grid.Row="2" Grid.Column="0"> <Label VerticalAlignment="Center" Grid.Row="2" Grid.Column="0">
<AccessText TextWrapping="Wrap"> <AccessText TextWrapping="Wrap">
@ -36,7 +36,7 @@
</AccessText> </AccessText>
</Label> </Label>
<Image x:Name="imageTarget" Grid.Row="2" Grid.Column="1" Margin="4" Source="../Resources/import1.png" AllowDrop="True" Drop="imageTarget_Drop" DragEnter="imageSource_DragEnter"/> <Image x:Name="imageTarget" Grid.Row="2" Grid.Column="1" Margin="4" Source="../Resources/import1.png" AllowDrop="True" Drop="imageTarget_Drop" DragEnter="imageSource_DragEnter"/>
<TextBox TextWrapping="Wrap" x:Name="textBoxTarget" Grid.Row="2" Grid.Column="2" IsReadOnly="True" Margin="2" AllowDrop="True" Drop="imageTarget_Drop" DragEnter="imageSource_DragEnter"/> <TextBox TextWrapping="Wrap" x:Name="textBoxTarget" Grid.Row="2" Grid.Column="2" IsReadOnly="True" Margin="2" AllowDrop="True" Drop="imageTarget_Drop" DragEnter="imageSource_DragEnter" PreviewDragOver="textBoxSource_PreviewDragOver"/>
<Button x:Name="buttonCompare" Margin="2" Grid.Row="3" Grid.Column="1" Content="Compare" IsEnabled="False" Click="buttonCompare_Click"/> <Button x:Name="buttonCompare" Margin="2" Grid.Row="3" Grid.Column="1" Content="Compare" IsEnabled="False" Click="buttonCompare_Click"/>
</Grid> </Grid>

View File

@ -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.IO;
using System.Windows; using System.Windows;
using System.Windows.Media.Imaging;
namespace ENI2.EditControls namespace ENI2.EditControls
{ {
@ -10,14 +15,19 @@ namespace ENI2.EditControls
/// </summary> /// </summary>
public partial class CompareExcelDialog : Controls.StatusWindowBase public partial class CompareExcelDialog : Controls.StatusWindowBase
{ {
#region Fields
private string _sourcePath = null; private string _sourcePath = null;
private string _targetPath = null; private string _targetPath = null;
#endregion
public CompareExcelDialog() public CompareExcelDialog()
{ {
InitializeComponent(); InitializeComponent();
} }
#region Drag&Drop event handler
private void imageSource_Drop(object sender, DragEventArgs e) private void imageSource_Drop(object sender, DragEventArgs e)
{ {
string[] files = (string[]) e.Data.GetData(DataFormats.FileDrop); 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() private void EnableCompareButton()
{ {
this.buttonCompare.IsEnabled = (_targetPath != null) && (_sourcePath != null); this.buttonCompare.IsEnabled = (_targetPath != null) && (_sourcePath != null);
} }
#endregion
#region Comparison button handler logic
private void buttonCompare_Click(object sender, RoutedEventArgs e) 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
} }
} }

View File

@ -29,7 +29,7 @@ namespace ENI2.Excel
#region Construction #region Construction
public ExcelBase(string filePath) public ExcelBase()
{ {
_log = LogManager.GetLogger(this.GetType().Name); _log = LogManager.GetLogger(this.GetType().Name);
@ -45,6 +45,8 @@ namespace ENI2.Excel
internal CountryMode Mode { get { return _countryMode; } } internal CountryMode Mode { get { return _countryMode; } }
internal Dictionary<string, Name> NameDict { get { return _nameDict; } }
#endregion #endregion
#region protected methods #region protected methods
@ -136,6 +138,26 @@ namespace ENI2.Excel
return result; 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 #endregion
#region Dispose #region Dispose

View File

@ -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;
}
}
}

View File

@ -83,7 +83,7 @@ namespace ENI2.Excel
using (ExcelWriter ew = new ExcelWriter(fileName, isRefSheet)) using (ExcelWriter ew = new ExcelWriter(fileName, isRefSheet))
{ {
ew.WriteData(messages, core, out resultMessage, isRefSheet); ew.WriteData(messages, core, out resultMessage, isRefSheet);
ew.Save(); ew.Save(fileName);
} }
} }
} }

View File

@ -23,7 +23,7 @@ using System.Text.RegularExpressions;
namespace ENI2.Excel namespace ENI2.Excel
{ {
internal class ExcelReader : ExcelBase internal class ExcelReader : ExcelBase
{ {
internal enum ReadState { NONE, OK, WARN, FAIL }; internal enum ReadState { NONE, OK, WARN, FAIL };
@ -33,9 +33,9 @@ namespace ENI2.Excel
internal Dictionary<string, string> ImportValues { get; } = new Dictionary<string, string>(); internal Dictionary<string, string> ImportValues { get; } = new Dictionary<string, string>();
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(); this.InitNameFields();
// Determine if this is a Dakosy or BSMD Sheet // Determine if this is a Dakosy or BSMD Sheet
@ -100,7 +100,7 @@ namespace ENI2.Excel
string val = this.ReadText(lookup); string val = this.ReadText(lookup);
if (!val.IsNullOrEmpty()) if (!val.IsNullOrEmpty())
{ {
val = val.ToUpper(); val = val.ToUpper();
string portName = LocodeDB.PortNameFromLocode(val); string portName = LocodeDB.PortNameFromLocode(val);
if (!justPorts) portName = LocodeDB.NameFromLocode(val); if (!justPorts) portName = LocodeDB.NameFromLocode(val);
if (portName.IsNullOrEmpty()) if (portName.IsNullOrEmpty())

View File

@ -21,6 +21,8 @@ namespace ENI2.Excel
{ {
private static readonly ILog _log = LogManager.GetLogger(typeof(ExcelUtil)); 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<Message.NotificationClass> notificationClasses) internal static bool ProcessSheet(ExcelReader reader, out string readMessage, MessageCore messageCore, List<Message.NotificationClass> notificationClasses)
{ {
bool result = true; bool result = true;
@ -134,7 +136,9 @@ namespace ENI2.Excel
} }
return result; return result;
} }
#endregion
#region ATA #region ATA
@ -2343,7 +2347,7 @@ namespace ENI2.Excel
if (sheetValue != null) if (sheetValue != null)
{ {
property.SetValue(dbEntity, sheetValue); property.SetValue(dbEntity, sheetValue);
} }
} }
else if (property.PropertyType == typeof(double?)) else if (property.PropertyType == typeof(double?))
{ {
@ -2351,7 +2355,7 @@ namespace ENI2.Excel
if (sheetValue != null) if (sheetValue != null)
{ {
property.SetValue(dbEntity, sheetValue); property.SetValue(dbEntity, sheetValue);
} }
} }
else if (property.PropertyType == typeof(string)) else if (property.PropertyType == typeof(string))
{ {
@ -2359,7 +2363,7 @@ namespace ENI2.Excel
if (sheetValue != null) if (sheetValue != null)
{ {
property.SetValue(dbEntity, sheetValue); property.SetValue(dbEntity, sheetValue);
} }
} }
else if (property.PropertyType == typeof(int?)) else if (property.PropertyType == typeof(int?))
{ {
@ -2367,7 +2371,7 @@ namespace ENI2.Excel
if (sheetValue.HasValue) if (sheetValue.HasValue)
{ {
property.SetValue(dbEntity, (int)sheetValue.Value); property.SetValue(dbEntity, (int)sheetValue.Value);
} }
} }
else if (property.PropertyType == typeof(byte?)) else if (property.PropertyType == typeof(byte?))
{ {
@ -2375,7 +2379,7 @@ namespace ENI2.Excel
if (sheetValue.HasValue) if (sheetValue.HasValue)
{ {
property.SetValue(dbEntity, (byte)sheetValue.Value); property.SetValue(dbEntity, (byte)sheetValue.Value);
} }
} }
else if (property.PropertyType == typeof(Boolean?)) else if (property.PropertyType == typeof(Boolean?))
{ {
@ -2383,7 +2387,7 @@ namespace ENI2.Excel
string boolStringValue = reader.ReadText(lookupNameAttribute.LookupName); string boolStringValue = reader.ReadText(lookupNameAttribute.LookupName);
if (sheetValue.HasValue) { if (sheetValue.HasValue) {
property.SetValue(dbEntity, sheetValue); property.SetValue(dbEntity, sheetValue);
} }
} }
else else
{ {

View File

@ -18,15 +18,11 @@ namespace ENI2.Excel
internal class ExcelWriter : ExcelBase internal class ExcelWriter : ExcelBase
{ {
#region Fields
private readonly string _saveFilePath;
#endregion
#region Construction #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"; string filename = @"Excel\EU-NoAD-Data-Collecting-Tool-5_0.xlsx";
if (isRefSheet) filename = @"Excel\Reference_Sheet_DE.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._workBook = _excelWorkbooks.Open(refFilePath, 0, true, 5, "", "", false, XlPlatform.xlWindows, "", false, false, 0, false, false, false);
this.InitNameFields(); this.InitNameFields();
_saveFilePath = filePath;
} }
#endregion #endregion
@ -196,13 +192,6 @@ namespace ENI2.Excel
WriteCore(core, isRefSheet); 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 #endregion