Excel Vergleich jetzt mit Speicherdialog

This commit is contained in:
Daniel Schick 2022-09-21 17:48:43 +02:00
parent eb001c78bc
commit 6ca0a5dda9
5 changed files with 30 additions and 17 deletions

View File

@ -3,6 +3,7 @@
// //
using bsmd.database; using bsmd.database;
using Microsoft.Win32;
using System; using System;
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
@ -174,17 +175,28 @@ namespace ENI2.EditControls
private void buttonCompare_Click(object sender, RoutedEventArgs e) private void buttonCompare_Click(object sender, RoutedEventArgs e)
{ {
Util.UIHelper.SetBusyState(); Util.UIHelper.SetBusyState();
string resultPath = Excel.ExcelComparer.Compare(_sourcePath, _targetPath, out string errorMessage);
if(!errorMessage.IsNullOrEmpty()) { string defaultName = string.Format("{0}.xlsx", Guid.NewGuid().ToString());
SaveFileDialog sfd = new SaveFileDialog
{
Filter = "Excel Files|*.xls;*.xlsx",
FileName = defaultName
};
if (sfd.ShowDialog() ?? false)
{
string resultPath = Excel.ExcelComparer.Compare(_sourcePath, _targetPath, sfd.FileName, out string errorMessage);
if (!errorMessage.IsNullOrEmpty()) {
MessageBox.Show(errorMessage, "Comparison error", MessageBoxButton.OK, MessageBoxImage.Warning); MessageBox.Show(errorMessage, "Comparison error", MessageBoxButton.OK, MessageBoxImage.Warning);
} }
if(File.Exists(resultPath)) if (File.Exists(resultPath))
{ {
if(new FileInfo(resultPath).Length > 0) if (new FileInfo(resultPath).Length > 0)
{ {
Process.Start(resultPath); Process.Start(resultPath);
} }
} }
}
// reset input values // reset input values
this.textBoxSource.Text = null; this.textBoxSource.Text = null;

View File

@ -61,17 +61,16 @@ namespace ENI2.Excel
return columnName; return columnName;
} }
public static string Compare(string sourcePath, string targetPath, out string errorMessage) public static string Compare(string sourcePath, string targetPath, string comparisonFileName, out string errorMessage)
{ {
string fileName = Path.GetTempPath() + Guid.NewGuid().ToString() + ".xlsx";
errorMessage = ""; errorMessage = "";
int counter = 0; int counter = 0;
try try
{ {
File.Copy(targetPath, fileName); File.Copy(targetPath, comparisonFileName);
ExcelReader source = new ExcelReader(sourcePath, true, false); ExcelReader source = new ExcelReader(sourcePath, true, false);
ExcelReader comparison = new ExcelReader(fileName, false, false); ExcelReader comparison = new ExcelReader(comparisonFileName, false, false);
/* erste Variante Vergleich über Namen der Zellen /* erste Variante Vergleich über Namen der Zellen
@ -182,7 +181,9 @@ namespace ENI2.Excel
} }
} }
comparison.Save(fileName); comparison.Save(comparisonFileName);
source.Dispose();
comparison.Dispose();
errorMessage = string.Format("{0} differences found", counter); errorMessage = string.Format("{0} differences found", counter);
} }
catch (Exception ex) catch (Exception ex)
@ -190,7 +191,7 @@ namespace ENI2.Excel
errorMessage = ex.Message; errorMessage = ex.Message;
} }
return fileName; return comparisonFileName;
} }
} }