diff options
author | Serghei Cebotari <51533848+schebotar@users.noreply.github.com> | 2022-01-28 18:20:30 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-01-28 18:20:30 +0300 |
commit | ec1d38f2d4926ddd89dc8f17d29617ea4ddefa82 (patch) | |
tree | 9fd3a44e58693dc9bbc8d0e406ba4de21b39ec86 /src/PriceListTools/Source.cs | |
parent | d688578a46e3a3383371c1df952fa2898c828a9a (diff) | |
parent | 2ad016bb4c332ecad6d12d824a84f15616ecea38 (diff) |
Merge pull request #12 from schebotar/dev
Dev
Diffstat (limited to 'src/PriceListTools/Source.cs')
-rw-r--r-- | src/PriceListTools/Source.cs | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/src/PriceListTools/Source.cs b/src/PriceListTools/Source.cs new file mode 100644 index 0000000..5013157 --- /dev/null +++ b/src/PriceListTools/Source.cs @@ -0,0 +1,95 @@ +using ExcelDna.Integration; +using Microsoft.Office.Interop.Excel; +using System; +using System.Collections.Generic; + +namespace RehauSku.PriceListTools +{ + internal class Source : PriceList + { + public Dictionary<Position, double> PositionAmount { get; private set; } + + public Source(Workbook workbook) + { + if (workbook == null) + { + throw new ArgumentException($"Нет рабочего файла"); + } + + Sheet = workbook.ActiveSheet; + Name = workbook.Name; + + amountCell = Sheet.Cells.Find(amountHeader); + skuCell = Sheet.Cells.Find(skuHeader); + groupCell = Sheet.Cells.Find(groupHeader); + nameCell = Sheet.Cells.Find(nameHeader); + + if (amountCell == null || skuCell == null || groupCell == null || nameCell == null) + { + throw new ArgumentException($"Файл {Name} не распознан"); + } + + CreatePositionsDict(); + } + + public static List<Source> GetSourceLists(string[] files) + { + var ExcelApp = (Application)ExcelDnaUtil.Application; + + List<Source> sourceFiles = new List<Source>(); + + ExcelApp.ScreenUpdating = false; + foreach (string file in files) + { + Workbook wb = ExcelApp.Workbooks.Open(file); + try + { + Source priceList = new Source(wb); + sourceFiles.Add(priceList); + wb.Close(); + } + catch (Exception ex) + { + System.Windows.Forms.MessageBox.Show + (ex.Message, + "Ошибка открытия исходного прайс-листа", + System.Windows.Forms.MessageBoxButtons.OK, + System.Windows.Forms.MessageBoxIcon.Information); + wb.Close(); + } + } + ExcelApp.ScreenUpdating = true; + + return sourceFiles; + } + + private void CreatePositionsDict() + { + PositionAmount = new Dictionary<Position, double>(); + + for (int row = amountCell.Row + 1; row < Sheet.AutoFilter.Range.Rows.Count; row++) + { + object amount = Sheet.Cells[row, amountCell.Column].Value2; + + if (amount != null && (double)amount != 0) + { + object group = Sheet.Cells[row, groupCell.Column].Value2; + object name = Sheet.Cells[row, nameCell.Column].Value2; + object sku = Sheet.Cells[row, skuCell.Column].Value2; + + Position p = new Position(group.ToString(), sku.ToString(), name.ToString()); + + if (PositionAmount.ContainsKey(p)) + { + PositionAmount[p] += (double)amount; + } + else + { + PositionAmount.Add(p, (double)amount); + } + } + } + } + } +} + |