From 8672fa8f6b28ee3e3cf0d00e05e1b9d3ab7a2bfd Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Fri, 7 Jan 2022 19:04:07 +0300 Subject: [PATCH 1/5] Add CombineTool for conjoining files into columns --- src/PriceListTools/CombineTool.cs | 95 ++++++++++++++++++++++++++ src/PriceListTools/ConjoinTool.cs | 9 +++ src/PriceListTools/ExportTool.cs | 2 +- src/PriceListTools/IConjoinTool.cs | 8 +++ src/PriceListTools/MergeTool.cs | 19 +++--- src/PriceListTools/PriceList.cs | 100 ++++++++++++++++++++-------- src/PriceListTools/PriceListUtil.cs | 2 +- src/RehauSku.Assist.csproj | 3 + src/Ribbon/RibbonController.cs | 24 +++++-- 9 files changed, 219 insertions(+), 43 deletions(-) create mode 100644 src/PriceListTools/CombineTool.cs create mode 100644 src/PriceListTools/ConjoinTool.cs create mode 100644 src/PriceListTools/IConjoinTool.cs diff --git a/src/PriceListTools/CombineTool.cs b/src/PriceListTools/CombineTool.cs new file mode 100644 index 0000000..c536936 --- /dev/null +++ b/src/PriceListTools/CombineTool.cs @@ -0,0 +1,95 @@ +using ExcelDna.Integration; +using Microsoft.Office.Interop.Excel; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; + +namespace RehauSku.PriceListTools +{ + class CombineTool : ConjoinTool, IDisposable, IConjoinTool + { + private Dictionary[] SkuAmount { get; set; } + private string[] FileNames { get; set; } + + public CombineTool() + { + ExcelApp = (Application)ExcelDnaUtil.Application; + } + + public void CollectSkuAmount(string[] files) + { + FileNames = files.Select(x => Path.GetFileNameWithoutExtension(x)).ToArray(); + SkuAmount = new Dictionary[files.Length]; + + ExcelApp.ScreenUpdating = false; + + for (int i = 0; i < files.Length; i++) + { + Workbook wb = ExcelApp.Workbooks.Open(files[i]); + + try + { + PriceList priceList = new PriceList(wb); + SkuAmount[i] = new Dictionary(); + SkuAmount[i].AddValuesFromPriceList(priceList); + } + + catch (Exception ex) + { + System.Windows.Forms.MessageBox.Show + ($"{wb.Name} не является файлом прайс-листа \n\n {ex.Message}", + "Неверный файл прайс-листа!", + System.Windows.Forms.MessageBoxButtons.OK, + System.Windows.Forms.MessageBoxIcon.Error); + } + + finally + { + wb.Close(); + } + } + + ExcelApp.ScreenUpdating = true; + } + + public void ExportToFile(string exportFile) + { + if (SkuAmount.Sum(d => d.Count) < 1) + { + return; + } + + Workbook wb = ExcelApp.Workbooks.Open(exportFile); + PriceList priceList; + + try + { + priceList = new PriceList(wb); + priceList.FillWithValues(SkuAmount, FileNames); + } + + catch (Exception ex) + { + System.Windows.Forms.MessageBox.Show + ($"{RegistryUtil.PriceListPath} не является файлом прайс-листа \n\n {ex.Message}", + "Неверный файл прайс-листа!", + System.Windows.Forms.MessageBoxButtons.OK, + System.Windows.Forms.MessageBoxIcon.Error); + + wb.Close(); + } + } + + public void Dispose() + { + //Dispose(true); + GC.SuppressFinalize(this); + } + + //protected virtual void Dispose(bool disposing) + //{ + + //} + } +} diff --git a/src/PriceListTools/ConjoinTool.cs b/src/PriceListTools/ConjoinTool.cs new file mode 100644 index 0000000..19ad653 --- /dev/null +++ b/src/PriceListTools/ConjoinTool.cs @@ -0,0 +1,9 @@ +using Microsoft.Office.Interop.Excel; + +namespace RehauSku.PriceListTools +{ + internal class ConjoinTool + { + protected Application ExcelApp; + } +} \ No newline at end of file diff --git a/src/PriceListTools/ExportTool.cs b/src/PriceListTools/ExportTool.cs index 61f6f0f..e549a00 100644 --- a/src/PriceListTools/ExportTool.cs +++ b/src/PriceListTools/ExportTool.cs @@ -85,7 +85,7 @@ namespace RehauSku.PriceListTools try { PriceList priceList = new PriceList(wb); - priceList.Fill(SkuAmount); + priceList.FillWithValues(SkuAmount); } catch(Exception ex) diff --git a/src/PriceListTools/IConjoinTool.cs b/src/PriceListTools/IConjoinTool.cs new file mode 100644 index 0000000..3107a16 --- /dev/null +++ b/src/PriceListTools/IConjoinTool.cs @@ -0,0 +1,8 @@ +namespace RehauSku.PriceListTools +{ + internal interface IConjoinTool + { + void CollectSkuAmount(string[] files); + void ExportToFile(string exportFile); + } +} \ No newline at end of file diff --git a/src/PriceListTools/MergeTool.cs b/src/PriceListTools/MergeTool.cs index ddf74d2..2dbe61d 100644 --- a/src/PriceListTools/MergeTool.cs +++ b/src/PriceListTools/MergeTool.cs @@ -5,18 +5,17 @@ using System.Collections.Generic; namespace RehauSku.PriceListTools { - class MergeTool : IDisposable + class MergeTool : ConjoinTool, IDisposable, IConjoinTool { - private Application ExcelApp; - private Dictionary SkuAmount { get; set; } + private Dictionary SkuAmount { get; set; } public MergeTool() { - this.ExcelApp = (Application)ExcelDnaUtil.Application; - this.SkuAmount = new Dictionary(); + ExcelApp = (Application)ExcelDnaUtil.Application; + SkuAmount = new Dictionary(); } - public void AddSkuAmountToDict(string[] files) + public void CollectSkuAmount(string[] files) { ExcelApp.ScreenUpdating = false; foreach (string file in files) @@ -26,13 +25,13 @@ namespace RehauSku.PriceListTools try { PriceList priceList = new PriceList(wb); - SkuAmount.AddValues(priceList); + SkuAmount.AddValuesFromPriceList(priceList); } catch (Exception ex) { System.Windows.Forms.MessageBox.Show - ( $"{wb.Name} не является файлом прайс-листа \n\n {ex.Message}", + ($"{wb.Name} не является файлом прайс-листа \n\n {ex.Message}", "Неверный файл прайс-листа!", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error); @@ -46,7 +45,7 @@ namespace RehauSku.PriceListTools ExcelApp.ScreenUpdating = true; } - public void ExportToNewFile(string exportFile) + public void ExportToFile(string exportFile) { if (SkuAmount.Count < 1) { @@ -59,7 +58,7 @@ namespace RehauSku.PriceListTools try { priceList = new PriceList(wb); - priceList.Fill(SkuAmount); + priceList.FillWithValues(SkuAmount); } catch (Exception ex) diff --git a/src/PriceListTools/PriceList.cs b/src/PriceListTools/PriceList.cs index 35b3f7d..c39b0ba 100644 --- a/src/PriceListTools/PriceList.cs +++ b/src/PriceListTools/PriceList.cs @@ -1,5 +1,6 @@ using Microsoft.Office.Interop.Excel; using System.Collections.Generic; +using System.Linq; namespace RehauSku.PriceListTools { @@ -9,31 +10,25 @@ namespace RehauSku.PriceListTools public readonly PriceListSheet OfferSheet; public readonly PriceListSheet ActiveSheet; - private const string _amountHeader = "Кол-во"; - private const string _skuHeader = "Актуальный материал"; - private const string _offerSheetHeader = "КП"; + private const string amountHeader = "Кол-во"; + private const string skuHeader = "Актуальный материал"; + private const string offerSheetHeader = "КП"; public PriceList(Workbook workbook) { Workbook = workbook; - OfferSheet = new PriceListSheet(workbook.Sheets[_offerSheetHeader]); + OfferSheet = new PriceListSheet(workbook.Sheets[offerSheetHeader]); Worksheet active = workbook.ActiveSheet; - if (active.Name == _offerSheetHeader) + if (active.Name == offerSheetHeader) ActiveSheet = OfferSheet; else - ActiveSheet = new PriceListSheet(active); + ActiveSheet = new PriceListSheet(active); } - public bool IsValid() - { - return OfferSheet.IsValid() && - ActiveSheet.IsValid(); - } - - public void Fill(Dictionary values) + public void FillWithValues(Dictionary values) { Worksheet ws = OfferSheet.sheet; ws.Activate(); @@ -45,6 +40,7 @@ namespace RehauSku.PriceListTools foreach (KeyValuePair kvp in values) { Range cell = ws.Columns[skuColumn].Find(kvp.Key); + if (cell == null) { System.Windows.Forms.MessageBox.Show @@ -53,6 +49,7 @@ namespace RehauSku.PriceListTools System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information); } + else { ws.Cells[cell.Row, amountColumn].Value = kvp.Value; @@ -68,33 +65,82 @@ namespace RehauSku.PriceListTools ws.Application.StatusBar = $"Экспортировано {exportedValues} строк из {values.Count}"; } + public void FillWithValues(Dictionary[] values, string[] filenames) + { + Worksheet ws = OfferSheet.sheet; + ws.Activate(); + + int amountColumn = OfferSheet.amountColumn.Value; + int skuColumn = OfferSheet.skuColumn.Value; + int headerColumn = OfferSheet.headerRow.Value; + + int exportedValues = 0; + + for (int i = 0; i < values.Length; i++) + { + ws.Columns[amountColumn] + .EntireColumn + .Insert(XlInsertShiftDirection.xlShiftToRight, XlInsertFormatOrigin.xlFormatFromRightOrBelow); + + foreach (var kvp in values[i]) + { + Range cell = ws.Columns[skuColumn].Find(kvp.Key); + + if (cell == null) + { + System.Windows.Forms.MessageBox.Show + ($"Артикул {kvp.Key} отсутствует в таблице заказов {RegistryUtil.PriceListPath}", + "Отсутствует позиция в конечной таблице заказов", + System.Windows.Forms.MessageBoxButtons.OK, + System.Windows.Forms.MessageBoxIcon.Information); + } + + else + { + ws.Cells[cell.Row, amountColumn].Value2 = kvp.Value; + Range sumCell = ws.Cells[cell.Row, amountColumn + i + 1]; + + if (sumCell.Value2 == null) + sumCell.Value2 = kvp.Value; + else + sumCell.Value2 += kvp.Value; + + exportedValues++; + } + } + + ws.Cells[headerColumn, amountColumn].Value2 = filenames[i]; + } + + AutoFilter filter = ws.AutoFilter; + int firstFilterColumn = filter.Range.Column; + + filter.Range.AutoFilter(amountColumn - firstFilterColumn + 1 + values.Length, "<>"); + ws.Range["A1"].Activate(); + ws.Application.StatusBar = $"Экспортировано {exportedValues} строк из {values.Sum(x => x.Count)}"; + } + public class PriceListSheet { public readonly Worksheet sheet; public readonly int? headerRow; - public readonly int? amountColumn; + public readonly int? skuColumn; - public object[,] amountCells; + public readonly int? amountColumn; + public object[,] skuCells; + public object[,] amountCells; public PriceListSheet(Worksheet sheet) { this.sheet = sheet; - headerRow = sheet.Cells.Find(_amountHeader).Row; - amountColumn = sheet.Cells.Find(_amountHeader).Column; - skuColumn = sheet.Cells.Find(_skuHeader).Column; + headerRow = sheet.Cells.Find(amountHeader).Row; + amountColumn = sheet.Cells.Find(amountHeader).Column; + skuColumn = sheet.Cells.Find(skuHeader).Column; amountCells = sheet.Columns[amountColumn].Value2; skuCells = sheet.Columns[skuColumn].Value2; - } - - public bool IsValid() - { - return sheet != null && - headerRow != null && - amountColumn != null && - skuColumn != null; - } + } } } } diff --git a/src/PriceListTools/PriceListUtil.cs b/src/PriceListTools/PriceListUtil.cs index 14797d9..3a04cc9 100644 --- a/src/PriceListTools/PriceListUtil.cs +++ b/src/PriceListTools/PriceListUtil.cs @@ -14,7 +14,7 @@ namespace RehauSku.PriceListTools return path; } - public static void AddValues(this Dictionary SkuAmount, PriceList priceList) + public static void AddValuesFromPriceList(this Dictionary SkuAmount, PriceList priceList) { object[,] amountCells = priceList.ActiveSheet.amountCells; object[,] skuCells = priceList.ActiveSheet.skuCells; diff --git a/src/RehauSku.Assist.csproj b/src/RehauSku.Assist.csproj index a977125..1f85926 100644 --- a/src/RehauSku.Assist.csproj +++ b/src/RehauSku.Assist.csproj @@ -106,6 +106,9 @@ + + + diff --git a/src/Ribbon/RibbonController.cs b/src/Ribbon/RibbonController.cs index df6f327..ad27bc2 100644 --- a/src/Ribbon/RibbonController.cs +++ b/src/Ribbon/RibbonController.cs @@ -17,8 +17,11 @@ namespace RehauSku.Ribbon -