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 Include="Excel\DakosyUtil.cs" />
<Compile Include="Excel\ExcelBase.cs" />
<Compile Include="Excel\ExcelComparer.cs" />
<Compile Include="Excel\ExcelManager.cs" />
<Compile Include="Excel\ExcelReader.cs" />
<Compile Include="Excel\ExcelUtil.cs" />

View File

@ -28,7 +28,7 @@
</AccessText>
</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"/>
<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">
<AccessText TextWrapping="Wrap">
@ -36,7 +36,7 @@
</AccessText>
</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"/>
<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"/>
</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.Windows;
using System.Windows.Media.Imaging;
namespace ENI2.EditControls
{
@ -10,14 +15,19 @@ namespace ENI2.EditControls
/// </summary>
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
}
}
}

View File

@ -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<string, Name> 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

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))
{
ew.WriteData(messages, core, out resultMessage, isRefSheet);
ew.Save();
ew.Save(fileName);
}
}
}

View File

@ -33,9 +33,9 @@ namespace ENI2.Excel
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();
// Determine if this is a Dakosy or BSMD Sheet

View File

@ -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<Message.NotificationClass> notificationClasses)
{
bool result = true;
@ -136,6 +138,8 @@ namespace ENI2.Excel
return result;
}
#endregion
#region ATA
private static bool ScanATA(Message ataMessage, ExcelReader reader)

View File

@ -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
@ -198,13 +194,6 @@ namespace ENI2.Excel
}
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
#region private excel field writing