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 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<string> files = new();
foreach (string file in dialog.SelectedItems)
{
files.Add(file);
}
SourceFiles = SourcePriceList.GetSourceLists(files.ToArray());
}
else

View File

@ -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<string> files = new();
foreach (string file in dialog.SelectedItems)
{
files.Add(file);
}
SourceFiles = SourcePriceList.GetSourceLists(files.ToArray());
}
else
{

View File

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

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