Switch to Excel internal file dialogs

This commit is contained in:
Sergey Chebotar 2023-03-28 09:58:43 +03:00
parent 7bb6ce6c5a
commit de2f7f43ec
4 changed files with 41 additions and 71 deletions

View File

@ -1,9 +1,9 @@
using Microsoft.Office.Interop.Excel; using Microsoft.Office.Interop.Excel;
using RhSolutions.AddIn;
using RhSolutions.Models; using RhSolutions.Models;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using Dialog = RhSolutions.Models.Dialog;
using Range = Microsoft.Office.Interop.Excel.Range; using Range = Microsoft.Office.Interop.Excel.Range;
namespace RhSolutions.Controllers namespace RhSolutions.Controllers
@ -14,11 +14,20 @@ namespace RhSolutions.Controllers
public CombineTool() 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<string> files = new();
foreach (string file in dialog.SelectedItems)
{
files.Add(file);
}
SourceFiles = SourcePriceList.GetSourceLists(files.ToArray());
} }
else else

View File

@ -1,4 +1,5 @@
using RhSolutions.Models; using RhSolutions.AddIn;
using RhSolutions.Models;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@ -11,11 +12,20 @@ namespace RhSolutions.Controllers
public MergeTool() 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<string> files = new();
foreach (string file in dialog.SelectedItems)
{
files.Add(file);
}
SourceFiles = SourcePriceList.GetSourceLists(files.ToArray());
} }
else else

View File

@ -1,5 +1,4 @@
using ExcelDna.Integration.CustomUI; using ExcelDna.Integration.CustomUI;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Office.Interop.Excel; using Microsoft.Office.Interop.Excel;
using RhSolutions.AddIn; using RhSolutions.AddIn;
using RhSolutions.Services; using RhSolutions.Services;
@ -55,11 +54,14 @@ namespace RhSolutions.Controllers
public void OnSetPricePressed(IRibbonControl control) 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(); RhSolutionsAddIn.Configuration.SaveSettings();
} }
} }
@ -68,25 +70,14 @@ namespace RhSolutions.Controllers
{ {
try try
{ {
ToolBase tool; ToolBase tool = control.Id switch
switch (control.Id)
{ {
case "export": "export" => new ExportTool(),
tool = new ExportTool(); "convert" => new ConvertTool(),
break; "merge" => new MergeTool(),
case "convert": "combine" => new CombineTool(),
tool = new ConvertTool(); _ => throw new Exception("Неизвестный инструмент"),
break; };
case "merge":
tool = new MergeTool();
break;
case "combine":
tool = new CombineTool();
break;
default:
throw new Exception("Неизвестный инструмент");
}
tool.OpenNewPrice(); tool.OpenNewPrice();
tool.FillTarget(); tool.FillTarget();
} }

View File

@ -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;
}
}
}
}