// Copyright (c) 2017- schick Informatik // Description: Dialogbox zum Vergleichen zweier Excel via Named cells // using bsmd.database; using Microsoft.Win32; using System; using System.Diagnostics; using System.IO; using System.Net; using System.Net.Http; using System.Windows; namespace ENI2.EditControls { /// /// Interaction logic for CompareExcelDialog.xaml /// 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); 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 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); } 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 #region click on textboxes opens file selection private void textBoxSource_PreviewMouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e) { OpenFileDialog ofd = new OpenFileDialog { Filter = "Excel Files|*.xls;*.xlsx" }; if (ofd.ShowDialog() ?? false) { textBoxSource.Text = ofd.FileName; _sourcePath = ofd.FileName; } EnableCompareButton(); } private void textBoxTarget_PreviewMouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e) { OpenFileDialog ofd = new OpenFileDialog { Filter = "Excel Files|*.xls;*.xlsx" }; if (ofd.ShowDialog() ?? false) { textBoxTarget.Text = ofd.FileName; _targetPath = ofd.FileName; } EnableCompareButton(); } #endregion } }