RhSolutions-AddIn/src/PriceListTools/Source.cs

105 lines
3.4 KiB
C#
Raw Normal View History

2022-01-28 16:19:39 +03:00
using ExcelDna.Integration;
using Microsoft.Office.Interop.Excel;
2022-01-27 09:59:33 +03:00
using System;
2022-01-27 10:22:30 +03:00
using System.Collections.Generic;
using System.Linq;
2022-02-02 10:23:50 +03:00
using RehauSku.Interface;
2022-01-27 09:59:33 +03:00
namespace RehauSku.PriceListTools
{
2022-01-27 10:22:30 +03:00
internal class Source : PriceList
2022-01-27 09:59:33 +03:00
{
2022-01-28 11:41:35 +03:00
public Dictionary<Position, double> PositionAmount { get; private set; }
2022-01-27 09:59:33 +03:00
2022-01-27 10:22:30 +03:00
public Source(Workbook workbook)
2022-01-27 09:59:33 +03:00
{
2022-01-28 12:44:37 +03:00
if (workbook == null)
{
throw new ArgumentException($"Нет рабочего файла");
}
2022-01-27 09:59:33 +03:00
Sheet = workbook.ActiveSheet;
2022-01-27 10:22:30 +03:00
Name = workbook.Name;
2022-01-27 09:59:33 +03:00
Range[] cells = new []
{
amountCell = Sheet.Cells.Find(amountHeader),
skuCell = Sheet.Cells.Find(skuHeader),
groupCell = Sheet.Cells.Find(groupHeader),
nameCell = Sheet.Cells.Find(nameHeader)
};
2022-01-27 09:59:33 +03:00
if (cells.Any(x => x == null))
2022-01-27 09:59:33 +03:00
{
2022-01-27 17:34:03 +03:00
throw new ArgumentException($"Файл {Name} не распознан");
2022-01-27 09:59:33 +03:00
}
2022-01-28 11:41:35 +03:00
CreatePositionsDict();
2022-01-27 09:59:33 +03:00
}
2022-01-28 16:19:39 +03:00
public static List<Source> GetSourceLists(string[] files)
{
var ExcelApp = (Application)ExcelDnaUtil.Application;
2022-02-02 10:23:50 +03:00
ProgressBar bar = new ProgressBar(files.Length);
2022-01-28 16:19:39 +03:00
List<Source> sourceFiles = new List<Source>();
foreach (string file in files)
{
ExcelApp.ScreenUpdating = false;
2022-01-28 16:19:39 +03:00
Workbook wb = ExcelApp.Workbooks.Open(file);
try
{
Source priceList = new Source(wb);
sourceFiles.Add(priceList);
wb.Close();
2022-02-02 10:23:50 +03:00
bar.DoProgress();
2022-01-28 16:19:39 +03:00
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show
(ex.Message,
"Ошибка открытия исходного прайс-листа",
System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Information);
wb.Close();
2022-02-02 10:23:50 +03:00
bar.DoProgress();
2022-01-28 16:19:39 +03:00
}
ExcelApp.ScreenUpdating = true;
2022-01-28 16:19:39 +03:00
}
return sourceFiles;
}
2022-01-28 11:41:35 +03:00
private void CreatePositionsDict()
2022-01-27 09:59:33 +03:00
{
2022-01-28 11:41:35 +03:00
PositionAmount = new Dictionary<Position, double>();
2022-01-27 09:59:33 +03:00
for (int row = amountCell.Row + 1; row <= Sheet.Cells[Sheet.Rows.Count, amountCell.Column].End[XlDirection.xlUp].Row; row++)
2022-01-27 09:59:33 +03:00
{
2022-01-28 15:59:47 +03:00
object amount = Sheet.Cells[row, amountCell.Column].Value2;
2022-01-27 09:59:33 +03:00
if (amount != null && (double)amount != 0)
{
2022-01-28 15:59:47 +03:00
object group = Sheet.Cells[row, groupCell.Column].Value2;
object name = Sheet.Cells[row, nameCell.Column].Value2;
object sku = Sheet.Cells[row, skuCell.Column].Value2;
2022-01-28 11:41:35 +03:00
Position p = new Position(group.ToString(), sku.ToString(), name.ToString());
2022-01-27 09:59:33 +03:00
2022-01-28 11:41:35 +03:00
if (PositionAmount.ContainsKey(p))
{
PositionAmount[p] += (double)amount;
}
2022-01-27 09:59:33 +03:00
else
2022-01-28 11:41:35 +03:00
{
PositionAmount.Add(p, (double)amount);
}
2022-01-27 09:59:33 +03:00
}
}
}
}
}