RhSolutions-AddIn/src/PriceListTools/ExportTool.cs

122 lines
3.9 KiB
C#
Raw Normal View History

2021-12-24 16:22:03 +03:00
using ExcelDna.Integration;
using Microsoft.Office.Interop.Excel;
using RehauSku.Assistant;
using System;
using System.Collections.Generic;
namespace RehauSku.PriceListTools
{
2022-01-09 10:37:25 +03:00
internal class ExportTool : AbstractPriceListTool, IDisposable
2021-12-24 16:22:03 +03:00
{
private Dictionary<string, double> SkuAmount { get; set; }
2022-01-09 10:37:25 +03:00
private Range Selection;
2021-12-24 16:22:03 +03:00
public ExportTool()
{
2022-01-09 10:37:25 +03:00
ExcelApp = (Application)ExcelDnaUtil.Application;
2021-12-24 16:22:03 +03:00
Selection = ExcelApp.Selection;
}
2022-01-09 10:37:25 +03:00
public override void GetSource(string[] files)
2021-12-24 16:22:03 +03:00
{
2022-01-09 10:37:25 +03:00
if (Selection != null && Selection.Columns.Count == 2)
FillSkuAmountDict();
else throw new Exception("Неверный диапазон");
2021-12-24 16:22:03 +03:00
}
2022-01-09 10:37:25 +03:00
private void FillSkuAmountDict()
2021-12-24 16:22:03 +03:00
{
object[,] cells = Selection.Value2;
SkuAmount = new Dictionary<string, double>();
int rowsCount = Selection.Rows.Count;
for (int row = 1; row <= rowsCount; row++)
{
if (cells[row, 1] == null || cells[row, 2] == null)
continue;
string sku = null;
double? amount = null;
for (int column = 1; column <= 2; column++)
{
object current = cells[row, column];
if (current.ToString().IsRehauSku())
{
sku = current.ToString();
}
else if (current.GetType() == typeof(string)
&& double.TryParse(current.ToString(), out _))
{
amount = double.Parse((string)current);
}
else if (current.GetType() == typeof(double))
{
amount = (double)current;
}
}
if (sku == null || amount == null)
continue;
if (SkuAmount.ContainsKey(sku))
SkuAmount[sku] += amount.Value;
else
SkuAmount.Add(sku, amount.Value);
}
}
2022-01-09 10:37:25 +03:00
public override void FillPriceList()
2021-12-24 16:22:03 +03:00
{
2022-01-09 10:37:25 +03:00
if (SkuAmount.Count < 1) return;
2021-12-24 17:42:20 +03:00
2022-01-09 10:37:25 +03:00
PriceListSheet offer = NewPriceList.OfferSheet;
offer.Sheet.Activate();
2021-12-24 16:22:03 +03:00
2022-01-09 10:37:25 +03:00
int exportedValues = 0;
2021-12-24 17:42:20 +03:00
2022-01-09 10:37:25 +03:00
foreach (var kvp in SkuAmount)
2021-12-24 17:42:20 +03:00
{
2022-01-09 10:37:25 +03:00
Range cell = offer.Sheet.Columns[offer.skuColumnNumber].Find(kvp.Key);
2021-12-24 17:42:20 +03:00
2022-01-09 10:37:25 +03:00
if (cell == null)
{
System.Windows.Forms.MessageBox.Show
($"Артикул {kvp.Key} отсутствует в таблице заказов {RegistryUtil.PriceListPath}",
"Отсутствует позиция в конечной таблице заказов",
System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Information);
}
else
{
Range sumCell = offer.Sheet.Cells[cell.Row, offer.amountColumnNumber];
if (sumCell.Value2 == null)
sumCell.Value2 = kvp.Value;
else
sumCell.Value2 += kvp.Value;
exportedValues++;
}
2021-12-24 17:42:20 +03:00
}
2022-01-09 10:37:25 +03:00
AutoFilter filter = offer.Sheet.AutoFilter;
int firstFilterColumn = filter.Range.Column;
filter.Range.AutoFilter(offer.amountColumnNumber - firstFilterColumn + 1, "<>");
offer.Sheet.Range["A1"].Activate();
offer.Sheet.Application.StatusBar = $"Экспортировано {exportedValues} строк из {SkuAmount.Count}";
2021-12-24 16:22:03 +03:00
}
public void Dispose()
{
GC.SuppressFinalize(this);
}
}
}