diff --git a/src/AddIn/AddIn.cs b/src/AddIn/AddIn.cs index 67cdcc8..93d8aec 100644 --- a/src/AddIn/AddIn.cs +++ b/src/AddIn/AddIn.cs @@ -1,6 +1,7 @@ using ExcelDna.Integration; using ExcelDna.IntelliSense; using ExcelDna.Registration; +using Microsoft.Office.Interop.Excel; using System.Net.Http; using System.Runtime.Caching; @@ -20,6 +21,7 @@ namespace RehauSku { public static HttpClient httpClient; public static MemoryCache memoryCache; + public static Application Excel; public void AutoOpen() { @@ -28,6 +30,7 @@ namespace RehauSku RegisterFunctions(); IntelliSenseServer.Install(); RegistryUtil.Initialize(); + Excel = (Application)ExcelDnaUtil.Application; } public void AutoClose() diff --git a/src/AddIn/RegistryUtil.cs b/src/AddIn/RegistryUtil.cs index 40d0ec2..ceee2fe 100644 --- a/src/AddIn/RegistryUtil.cs +++ b/src/AddIn/RegistryUtil.cs @@ -2,56 +2,56 @@ using System.IO; using RehauSku.Forms; using System.Windows.Forms; +using ExcelDna.Integration; namespace RehauSku { static class RegistryUtil { - private static string _priceListPath; - private static int? _storeResponseOrder; - private static RegistryKey _RootKey { get; set; } + private static string priceListPath; + private static int? storeResponseOrder; + private static RegistryKey RootKey { get; set; } public static void Initialize() { - _RootKey = Registry.CurrentUser.CreateSubKey(@"SOFTWARE\REHAU\SkuAssist"); - _priceListPath = _RootKey.GetValue("PriceListPath") as string; - _storeResponseOrder = _RootKey.GetValue("StoreResponseOrder") as int?; + RootKey = Registry.CurrentUser.CreateSubKey(@"SOFTWARE\REHAU\SkuAssist"); + priceListPath = RootKey.GetValue("PriceListPath") as string; + storeResponseOrder = RootKey.GetValue("StoreResponseOrder") as int?; } public static void Uninitialize() { - _RootKey.Close(); - + RootKey.Close(); } public static bool IsPriceListPathEmpty() { - return string.IsNullOrEmpty(_priceListPath); + return string.IsNullOrEmpty(priceListPath); } public static string PriceListPath { get { - if (IsPriceListPathEmpty() || !File.Exists(_priceListPath)) + if (IsPriceListPathEmpty() || !File.Exists(priceListPath)) { - MessageBox.Show("Прайс-лист отсутствует или неверный файл прайс-листа", "Укажите файл прайс-листа", MessageBoxButtons.OK, MessageBoxIcon.Warning); + //MessageBox.Show("Прайс-лист отсутствует или неверный файл прайс-листа", "Укажите файл прайс-листа", MessageBoxButtons.OK, MessageBoxIcon.Warning); string fileName = Dialog.GetFilePath(); - _priceListPath = fileName; - _RootKey.SetValue("PriceListPath", fileName); - return _priceListPath; + priceListPath = fileName; + RootKey.SetValue("PriceListPath", fileName); + return priceListPath; } else { - return _priceListPath; + return priceListPath; } } set { - _priceListPath = value; - _RootKey.SetValue("PriceListPath", value); + priceListPath = value; + RootKey.SetValue("PriceListPath", value); } } @@ -59,16 +59,16 @@ namespace RehauSku { get { - if (_storeResponseOrder == null) + if (storeResponseOrder == null) { - _RootKey.SetValue("StoreResponseOrder", (int)ResponseOrder.Default); - _storeResponseOrder = (int)ResponseOrder.Default; - return (ResponseOrder)_storeResponseOrder.Value; + RootKey.SetValue("StoreResponseOrder", (int)ResponseOrder.Default); + storeResponseOrder = (int)ResponseOrder.Default; + return (ResponseOrder)storeResponseOrder.Value; } else { - return (ResponseOrder)_storeResponseOrder.Value; + return (ResponseOrder)storeResponseOrder.Value; } } } diff --git a/src/Forms/Dialog.cs b/src/Forms/Dialog.cs index 170cc81..cc1c29a 100644 --- a/src/Forms/Dialog.cs +++ b/src/Forms/Dialog.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using Microsoft.Office.Interop.Excel; +using System.Collections.Generic; using System.Windows.Forms; namespace RehauSku.Forms @@ -42,5 +43,20 @@ namespace RehauSku.Forms return fileNames.ToArray(); } + + public static void SaveWorkbookAs() + { + Workbook wb = AddIn.Excel.ActiveWorkbook; + string currentFilename = wb.FullName; + string fileFilter = "Файлы Excel (*.xls;*.xlsx;*.xlsm),*.xls;*.xlsx;*.xlsm"; + + object fileName = AddIn.Excel.GetSaveAsFilename(currentFilename, fileFilter); + + if (fileName.GetType() == typeof(string)) + wb.SaveAs(fileName); + + else + wb.Close(false); + } } } diff --git a/src/PriceListTools/CombineTool.cs b/src/PriceListTools/CombineTool.cs index 303146c..cf02059 100644 --- a/src/PriceListTools/CombineTool.cs +++ b/src/PriceListTools/CombineTool.cs @@ -63,7 +63,9 @@ namespace RehauSku.PriceListTools filter.Range.AutoFilter(offer.amountColumnNumber - firstFilterColumn + 1 + exportedLists, "<>"); offer.Sheet.Range["A1"].Activate(); - offer.Sheet.Application.StatusBar = $"Экспортировано {exportedValues} строк из {sourcePriceLists.Count} файлов"; + AddIn.Excel.StatusBar = $"Экспортировано {exportedValues} строк из {sourcePriceLists.Count} файлов"; + + Forms.Dialog.SaveWorkbookAs(); } public void Dispose() diff --git a/src/PriceListTools/ExportTool.cs b/src/PriceListTools/ExportTool.cs index be76dff..a93097d 100644 --- a/src/PriceListTools/ExportTool.cs +++ b/src/PriceListTools/ExportTool.cs @@ -113,7 +113,9 @@ namespace RehauSku.PriceListTools filter.Range.AutoFilter(offer.amountColumnNumber - firstFilterColumn + 1, "<>"); offer.Sheet.Range["A1"].Activate(); - offer.Sheet.Application.StatusBar = $"Экспортировано {exportedValues} строк из {SkuAmount.Count}"; + AddIn.Excel.StatusBar = $"Экспортировано {exportedValues} строк из {SkuAmount.Count}"; + + Forms.Dialog.SaveWorkbookAs(); } public void Dispose() diff --git a/src/PriceListTools/MergeTool.cs b/src/PriceListTools/MergeTool.cs index 4f5522c..493f8a8 100644 --- a/src/PriceListTools/MergeTool.cs +++ b/src/PriceListTools/MergeTool.cs @@ -52,7 +52,9 @@ namespace RehauSku.PriceListTools filter.Range.AutoFilter(offer.amountColumnNumber - firstFilterColumn + 1, "<>"); offer.Sheet.Range["A1"].Activate(); - offer.Sheet.Application.StatusBar = $"Экспортировано {exportedValues} строк из {sourcePriceLists.Count} файлов"; + AddIn.Excel.StatusBar = $"Экспортировано {exportedValues} строк из {sourcePriceLists.Count} файлов"; + + Forms.Dialog.SaveWorkbookAs(); } public void Dispose() diff --git a/src/PriceListTools/PriceListSheet.cs b/src/PriceListTools/PriceListSheet.cs index e10913b..8a34c2f 100644 --- a/src/PriceListTools/PriceListSheet.cs +++ b/src/PriceListTools/PriceListSheet.cs @@ -31,7 +31,7 @@ namespace RehauSku.PriceListTools if (amountCell == null || skuCell == null) { - Sheet.Application.StatusBar = $"Лист {Name} не распознан"; + AddIn.Excel.StatusBar = $"Лист {Name} не распознан"; return false; } diff --git a/src/Ribbon/RibbonController.cs b/src/Ribbon/RibbonController.cs index 15bd1a0..ed59541 100644 --- a/src/Ribbon/RibbonController.cs +++ b/src/Ribbon/RibbonController.cs @@ -40,10 +40,13 @@ namespace RehauSku.Ribbon using (MergeTool mergeTool = new MergeTool()) { string[] files = Dialog.GetMultiplyFiles(); - mergeTool.GetSource(files); - string exportFile = PriceList.CreateNewFile(); - mergeTool.OpenNewPrice(exportFile); - mergeTool.FillPriceList(); + if (files.Length != 0) + { + mergeTool.GetSource(files); + string exportFile = RegistryUtil.PriceListPath; + mergeTool.OpenNewPrice(exportFile); + mergeTool.FillPriceList(); + } } } @@ -52,10 +55,13 @@ namespace RehauSku.Ribbon using (CombineTool combineTool = new CombineTool()) { string[] files = Dialog.GetMultiplyFiles(); - combineTool.GetSource(files); - string exportFile = PriceList.CreateNewFile(); - combineTool.OpenNewPrice(exportFile); - combineTool.FillPriceList(); + if (files.Length != 0) + { + combineTool.GetSource(files); + string exportFile = RegistryUtil.PriceListPath; + combineTool.OpenNewPrice(exportFile); + combineTool.FillPriceList(); + } } } @@ -66,7 +72,7 @@ namespace RehauSku.Ribbon using (ExportTool exportTool = new ExportTool()) { exportTool.GetSource(); - string exportFile = PriceList.CreateNewFile(); + string exportFile = RegistryUtil.PriceListPath; exportTool.OpenNewPrice(exportFile); exportTool.FillPriceList(); }