RhSolutions-AddIn/src/PriceListTools/ExportTool.cs

116 lines
3.2 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
{
class ExportTool : IDisposable
{
private Application ExcelApp;
private Dictionary<string, double> SkuAmount { get; set; }
private Range Selection { get; set; }
public ExportTool()
{
this.ExcelApp = (Application)ExcelDnaUtil.Application;
Selection = ExcelApp.Selection;
if (IsRangeValid())
_FillSkuAmountDict();
}
public bool IsRangeValid()
{
return Selection != null &&
Selection.Columns.Count == 2;
}
private void _FillSkuAmountDict()
{
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);
}
}
public void ExportToNewFile()
{
2021-12-24 17:42:20 +03:00
if (SkuAmount.Count < 1)
{
return;
}
2021-12-24 16:22:03 +03:00
string exportFile = PriceListUtil.CreateNewExportFile();
Workbook wb = ExcelApp.Workbooks.Open(exportFile);
2021-12-24 17:42:20 +03:00
try
{
PriceList priceList = new PriceList(wb);
priceList.FillWithValues(SkuAmount);
2021-12-24 17:42:20 +03:00
}
catch(Exception ex)
{
System.Windows.Forms.MessageBox.Show
2021-12-26 18:22:32 +03:00
($"{RegistryUtil.PriceListPath} не является файлом прайс-листа \n\n {ex.Message}",
2021-12-24 17:42:20 +03:00
"Неверный файл прайс-листа!",
System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Error);
wb.Close();
}
2021-12-24 16:22:03 +03:00
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
}
}
}