RhSolutions-AddIn/Source/PriceListTools/MergeTool.cs

89 lines
2.5 KiB
C#
Raw Normal View History

2021-12-24 16:22:03 +03:00
using ExcelDna.Integration;
using Microsoft.Office.Interop.Excel;
using System;
using System.Collections.Generic;
namespace RehauSku.PriceListTools
{
class MergeTool : IDisposable
{
private Application ExcelApp;
private Dictionary<string, double> SkuAmount { get; set; }
public MergeTool()
{
this.ExcelApp = (Application)ExcelDnaUtil.Application;
this.SkuAmount = new Dictionary<string, double>();
}
public void AddSkuAmountToDict(string[] files)
{
ExcelApp.ScreenUpdating = false;
foreach (string file in files)
{
Workbook wb = ExcelApp.Workbooks.Open(file);
2021-12-24 17:42:20 +03:00
try
{
PriceList priceList = new PriceList(wb);
2021-12-24 16:22:03 +03:00
SkuAmount.AddValues(priceList);
2021-12-24 17:42:20 +03:00
}
2021-12-24 16:22:03 +03:00
2021-12-24 17:42:20 +03:00
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show
( $"{wb.Name} не является файлом прайслиста \n\n {ex.Message}",
"Неверный файл прайс-листа!",
System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Error);
}
finally
{
wb.Close();
}
2021-12-24 16:22:03 +03:00
}
ExcelApp.ScreenUpdating = true;
}
public void ExportToNewFile(string exportFile)
{
2021-12-24 17:42:20 +03:00
if (SkuAmount.Count < 1)
{
return;
}
2021-12-24 16:22:03 +03:00
Workbook wb = ExcelApp.Workbooks.Open(exportFile);
2021-12-24 17:42:20 +03:00
PriceList priceList;
2021-12-24 16:22:03 +03:00
2021-12-24 17:42:20 +03:00
try
{
priceList = new PriceList(wb);
2021-12-24 16:22:03 +03:00
priceList.Fill(SkuAmount);
2021-12-24 17:42:20 +03:00
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show
($"{RegistryUtil.PriceListPath} не является файлом прайслиста \n\n {ex.Message}",
"Неверный файл прайс-листа!",
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)
{
}
}
}