From de2f7f43ec802e79ab7cdf67642168d39ef53fa2 Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Tue, 28 Mar 2023 09:58:43 +0300 Subject: [PATCH] Switch to Excel internal file dialogs --- RhSolutions.AddIn/Controllers/CombineTool.cs | 17 ++++++-- RhSolutions.AddIn/Controllers/MergeTool.cs | 20 +++++++--- .../Controllers/RibbonController.cs | 35 ++++++---------- RhSolutions.AddIn/Models/Dialog.cs | 40 ------------------- 4 files changed, 41 insertions(+), 71 deletions(-) delete mode 100644 RhSolutions.AddIn/Models/Dialog.cs diff --git a/RhSolutions.AddIn/Controllers/CombineTool.cs b/RhSolutions.AddIn/Controllers/CombineTool.cs index 4d84d44..ea4e782 100644 --- a/RhSolutions.AddIn/Controllers/CombineTool.cs +++ b/RhSolutions.AddIn/Controllers/CombineTool.cs @@ -1,9 +1,9 @@ using Microsoft.Office.Interop.Excel; +using RhSolutions.AddIn; using RhSolutions.Models; using System; using System.Collections.Generic; using System.Linq; -using Dialog = RhSolutions.Models.Dialog; using Range = Microsoft.Office.Interop.Excel.Range; namespace RhSolutions.Controllers @@ -14,11 +14,20 @@ namespace RhSolutions.Controllers public CombineTool() { - string[] files = Dialog.GetMultiplyFiles(); + var dialog = RhSolutionsAddIn.Excel.FileDialog[Microsoft.Office.Core.MsoFileDialogType.msoFileDialogFilePicker]; + dialog.AllowMultiSelect = true; + dialog.Filters.Add("Файлы Excel", "*.xls; *.xlsx; *.xlsm"); - if (files != null) + if (dialog.Show() < 0) { - SourceFiles = SourcePriceList.GetSourceLists(files); + List files = new(); + + foreach (string file in dialog.SelectedItems) + { + files.Add(file); + } + + SourceFiles = SourcePriceList.GetSourceLists(files.ToArray()); } else diff --git a/RhSolutions.AddIn/Controllers/MergeTool.cs b/RhSolutions.AddIn/Controllers/MergeTool.cs index dec8ff7..8566ee7 100644 --- a/RhSolutions.AddIn/Controllers/MergeTool.cs +++ b/RhSolutions.AddIn/Controllers/MergeTool.cs @@ -1,4 +1,5 @@ -using RhSolutions.Models; +using RhSolutions.AddIn; +using RhSolutions.Models; using System; using System.Collections.Generic; using System.Linq; @@ -11,12 +12,21 @@ namespace RhSolutions.Controllers public MergeTool() { - string[] files = Dialog.GetMultiplyFiles(); + var dialog = RhSolutionsAddIn.Excel.FileDialog[Microsoft.Office.Core.MsoFileDialogType.msoFileDialogFilePicker]; + dialog.AllowMultiSelect = true; + dialog.Filters.Add("Файлы Excel", "*.xls; *.xlsx; *.xlsm"); - if (files != null) + if ( dialog.Show() < 0) { - SourceFiles = SourcePriceList.GetSourceLists(files); - } + List files = new(); + + foreach (string file in dialog.SelectedItems) + { + files.Add(file); + } + + SourceFiles = SourcePriceList.GetSourceLists(files.ToArray()); + } else { diff --git a/RhSolutions.AddIn/Controllers/RibbonController.cs b/RhSolutions.AddIn/Controllers/RibbonController.cs index b95bb4f..d819b4b 100644 --- a/RhSolutions.AddIn/Controllers/RibbonController.cs +++ b/RhSolutions.AddIn/Controllers/RibbonController.cs @@ -1,5 +1,4 @@ using ExcelDna.Integration.CustomUI; -using Microsoft.Extensions.DependencyInjection; using Microsoft.Office.Interop.Excel; using RhSolutions.AddIn; using RhSolutions.Services; @@ -55,11 +54,14 @@ namespace RhSolutions.Controllers public void OnSetPricePressed(IRibbonControl control) { - string path = Models.Dialog.GetFilePath(); + var dialog = RhSolutionsAddIn.Excel + .FileDialog[Microsoft.Office.Core.MsoFileDialogType.msoFileDialogFilePicker]; + dialog.AllowMultiSelect = false; + dialog.Filters.Add("Файлы Excel", "*.xls; *.xlsx; *.xlsm"); - if (!string.IsNullOrEmpty(path)) + if (dialog.Show() < 0) { - RhSolutionsAddIn.Configuration.SetPriceListPath(path); + RhSolutionsAddIn.Configuration.SetPriceListPath(dialog.SelectedItems.Item(1)); RhSolutionsAddIn.Configuration.SaveSettings(); } } @@ -68,25 +70,14 @@ namespace RhSolutions.Controllers { try { - ToolBase tool; - switch (control.Id) + ToolBase tool = control.Id switch { - case "export": - tool = new ExportTool(); - break; - case "convert": - tool = new ConvertTool(); - break; - case "merge": - tool = new MergeTool(); - break; - case "combine": - tool = new CombineTool(); - break; - default: - throw new Exception("Неизвестный инструмент"); - } - + "export" => new ExportTool(), + "convert" => new ConvertTool(), + "merge" => new MergeTool(), + "combine" => new CombineTool(), + _ => throw new Exception("Неизвестный инструмент"), + }; tool.OpenNewPrice(); tool.FillTarget(); } diff --git a/RhSolutions.AddIn/Models/Dialog.cs b/RhSolutions.AddIn/Models/Dialog.cs deleted file mode 100644 index abc89b8..0000000 --- a/RhSolutions.AddIn/Models/Dialog.cs +++ /dev/null @@ -1,40 +0,0 @@ -using Microsoft.Office.Interop.Excel; -using System.Collections.Generic; -using System.Windows.Forms; - -namespace RhSolutions.Models -{ - static class Dialog - { - public static string GetFilePath() - { - using (OpenFileDialog dialog = new OpenFileDialog()) - { - dialog.Filter = "Файлы Excel (*.xls;*.xlsx;*.xlsm)|*.xls;*.xlsx;*.xlsm"; - - if (dialog.ShowDialog() == DialogResult.OK) - { - return dialog.FileName; - } - - else return string.Empty; - } - } - - public static string[] GetMultiplyFiles() - { - using (OpenFileDialog dialog = new OpenFileDialog()) - { - dialog.Filter = "Файлы Excel (*.xls;*.xlsx;*.xlsm)|*.xls;*.xlsx;*.xlsm"; - dialog.Multiselect = true; - - if (dialog.ShowDialog() == DialogResult.OK) - { - return dialog.FileNames; - } - - else return null; - } - } - } -}