git_bsmd/ENI2/EditControls/CompareExcelDialog.xaml.cs

203 lines
6.8 KiB
C#

// 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.Net;
using System.Net.Http;
using System.Windows;
namespace ENI2.EditControls
{
/// <summary>
/// Interaction logic for CompareExcelDialog.xaml
/// </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 async void imageSource_Drop(object sender, DragEventArgs e)
{
string[] files = (string[]) e.Data.GetData(DataFormats.FileDrop);
if (files != null)
{
foreach (string file in files)
Console.WriteLine(file);
if (files.Length > 0)
{
if (File.Exists(files[0]))
{
if (files[0].EndsWith("xls") || files[0].EndsWith("xlsx"))
{
_sourcePath = files[0];
textBoxSource.Text = _sourcePath;
}
else
{
textBoxSource.Text = null;
_sourcePath = null;
}
}
else
{
textBoxSource.Text = null;
_sourcePath = null;
}
EnableCompareButton();
}
}
else
{
string link = (string)e.Data.GetData(DataFormats.Text);
if(link != null)
{
using (var client = new WebClient())
{
string fileName = Path.GetTempPath() + Guid.NewGuid().ToString() + ".xlsx";
client.DownloadFile(link, fileName);
}
/*
// check if it is really an url, try to download the file and open it
if (Uri.TryCreate(link, UriKind.Absolute, out Uri uri))
{
HttpClient client = new HttpClient();
var response = await client.GetAsync(uri);
string fileName = Path.GetTempPath() + Guid.NewGuid().ToString() + ".xlsx";
using (var fs = new FileStream(fileName, FileMode.CreateNew))
{
await response.Content.CopyToAsync(fs);
textBoxSource.Text = link;
_sourcePath = fileName;
EnableCompareButton();
}
}
*/
}
}
}
private async void imageTarget_Drop(object sender, DragEventArgs e)
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
if (files != null)
{
foreach (string file in files)
Console.WriteLine(file);
if (files.Length > 0)
{
if (File.Exists(files[0]))
{
if (files[0].EndsWith("xls") || files[0].EndsWith("xlsx"))
{
_targetPath = files[0];
textBoxTarget.Text = _targetPath;
}
else
{
_targetPath = null;
textBoxTarget.Text = null;
}
}
else
{
_targetPath = null;
textBoxTarget.Text = null;
}
EnableCompareButton();
}
}
else
{
string link = (string)e.Data.GetData(DataFormats.Text);
if (link != null)
{
// check if it is really an url, try to download the file and open it
if (Uri.TryCreate(link, UriKind.Absolute, out Uri uri))
{
HttpClient client = new HttpClient();
var response = await client.GetAsync(uri);
string fileName = Path.GetTempPath() + Guid.NewGuid().ToString() + ".xlsx";
using (var fs = new FileStream(fileName, FileMode.CreateNew))
{
await response.Content.CopyToAsync(fs);
textBoxTarget.Text = link;
_targetPath = fileName;
EnableCompareButton();
}
}
}
}
}
private void imageSource_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effects = DragDropEffects.Copy;
}
}
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
}
}