2021-12-24 16:22:03 +03:00
|
|
|
|
using Microsoft.Office.Interop.Excel;
|
|
|
|
|
using System.Collections.Generic;
|
2022-01-07 19:04:07 +03:00
|
|
|
|
using System.Linq;
|
2021-12-24 16:22:03 +03:00
|
|
|
|
|
|
|
|
|
namespace RehauSku.PriceListTools
|
|
|
|
|
{
|
|
|
|
|
class PriceList
|
|
|
|
|
{
|
|
|
|
|
public readonly Workbook Workbook;
|
|
|
|
|
public readonly PriceListSheet OfferSheet;
|
|
|
|
|
public readonly PriceListSheet ActiveSheet;
|
|
|
|
|
|
2022-01-07 19:04:07 +03:00
|
|
|
|
private const string amountHeader = "Кол-во";
|
|
|
|
|
private const string skuHeader = "Актуальный материал";
|
|
|
|
|
private const string offerSheetHeader = "КП";
|
2021-12-24 16:22:03 +03:00
|
|
|
|
|
|
|
|
|
public PriceList(Workbook workbook)
|
|
|
|
|
{
|
|
|
|
|
Workbook = workbook;
|
2022-01-07 19:04:07 +03:00
|
|
|
|
OfferSheet = new PriceListSheet(workbook.Sheets[offerSheetHeader]);
|
2021-12-24 16:22:03 +03:00
|
|
|
|
|
|
|
|
|
Worksheet active = workbook.ActiveSheet;
|
|
|
|
|
|
2022-01-07 19:04:07 +03:00
|
|
|
|
if (active.Name == offerSheetHeader)
|
2021-12-24 16:22:03 +03:00
|
|
|
|
ActiveSheet = OfferSheet;
|
|
|
|
|
|
|
|
|
|
else
|
2022-01-07 19:04:07 +03:00
|
|
|
|
ActiveSheet = new PriceListSheet(active);
|
2021-12-24 16:22:03 +03:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-07 19:04:07 +03:00
|
|
|
|
public void FillWithValues(Dictionary<string, double> values)
|
2021-12-24 16:22:03 +03:00
|
|
|
|
{
|
|
|
|
|
Worksheet ws = OfferSheet.sheet;
|
|
|
|
|
ws.Activate();
|
|
|
|
|
|
|
|
|
|
int amountColumn = OfferSheet.amountColumn.Value;
|
|
|
|
|
int skuColumn = OfferSheet.skuColumn.Value;
|
2022-01-03 13:17:39 +03:00
|
|
|
|
int exportedValues = 0;
|
2021-12-24 16:22:03 +03:00
|
|
|
|
|
|
|
|
|
foreach (KeyValuePair<string, double> kvp in values)
|
|
|
|
|
{
|
|
|
|
|
Range cell = ws.Columns[skuColumn].Find(kvp.Key);
|
2022-01-07 19:04:07 +03:00
|
|
|
|
|
2021-12-28 17:15:30 +03:00
|
|
|
|
if (cell == null)
|
|
|
|
|
{
|
|
|
|
|
System.Windows.Forms.MessageBox.Show
|
|
|
|
|
($"Артикул {kvp.Key} отсутствует в таблице заказов {RegistryUtil.PriceListPath}",
|
|
|
|
|
"Отсутствует позиция в конечной таблице заказов",
|
|
|
|
|
System.Windows.Forms.MessageBoxButtons.OK,
|
|
|
|
|
System.Windows.Forms.MessageBoxIcon.Information);
|
2022-01-03 13:17:39 +03:00
|
|
|
|
}
|
2022-01-07 19:04:07 +03:00
|
|
|
|
|
2021-12-28 17:15:30 +03:00
|
|
|
|
else
|
2022-01-03 13:17:39 +03:00
|
|
|
|
{
|
2021-12-28 17:15:30 +03:00
|
|
|
|
ws.Cells[cell.Row, amountColumn].Value = kvp.Value;
|
2022-01-03 13:17:39 +03:00
|
|
|
|
exportedValues++;
|
|
|
|
|
}
|
2021-12-24 16:22:03 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AutoFilter filter = ws.AutoFilter;
|
|
|
|
|
int firstFilterColumn = filter.Range.Column;
|
|
|
|
|
|
|
|
|
|
filter.Range.AutoFilter(amountColumn - firstFilterColumn + 1, "<>");
|
|
|
|
|
ws.Range["A1"].Activate();
|
2022-01-03 13:17:39 +03:00
|
|
|
|
ws.Application.StatusBar = $"Экспортировано {exportedValues} строк из {values.Count}";
|
2021-12-24 16:22:03 +03:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-07 19:04:07 +03:00
|
|
|
|
public void FillWithValues(Dictionary<string, double>[] 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)}";
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-24 16:22:03 +03:00
|
|
|
|
public class PriceListSheet
|
|
|
|
|
{
|
|
|
|
|
public readonly Worksheet sheet;
|
|
|
|
|
public readonly int? headerRow;
|
2022-01-07 19:04:07 +03:00
|
|
|
|
|
2021-12-24 16:22:03 +03:00
|
|
|
|
public readonly int? skuColumn;
|
2022-01-07 19:04:07 +03:00
|
|
|
|
public readonly int? amountColumn;
|
|
|
|
|
|
2021-12-24 16:22:03 +03:00
|
|
|
|
public object[,] skuCells;
|
2022-01-07 19:04:07 +03:00
|
|
|
|
public object[,] amountCells;
|
2021-12-24 16:22:03 +03:00
|
|
|
|
|
|
|
|
|
public PriceListSheet(Worksheet sheet)
|
|
|
|
|
{
|
|
|
|
|
this.sheet = sheet;
|
2022-01-07 19:04:07 +03:00
|
|
|
|
headerRow = sheet.Cells.Find(amountHeader).Row;
|
|
|
|
|
amountColumn = sheet.Cells.Find(amountHeader).Column;
|
|
|
|
|
skuColumn = sheet.Cells.Find(skuHeader).Column;
|
2021-12-24 16:22:03 +03:00
|
|
|
|
|
|
|
|
|
amountCells = sheet.Columns[amountColumn].Value2;
|
|
|
|
|
skuCells = sheet.Columns[skuColumn].Value2;
|
2022-01-07 19:04:07 +03:00
|
|
|
|
}
|
2021-12-24 16:22:03 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|