RhSolutions-AddIn/Source/DataExport/Exporter.cs

116 lines
3.3 KiB
C#
Raw Normal View History

2021-12-05 16:55:36 +03:00
using ExcelDna.Integration;
using Microsoft.Office.Interop.Excel;
using System;
using System.Collections.Generic;
2021-12-08 09:45:03 +03:00
using System.IO;
2021-12-08 14:45:14 +03:00
using RehauSku.Assistant;
2021-12-05 16:55:36 +03:00
2021-12-08 14:45:14 +03:00
namespace RehauSku.DataExport
2021-12-05 16:55:36 +03:00
{
2021-12-08 14:45:14 +03:00
public class Exporter : IDisposable
2021-12-05 16:55:36 +03:00
{
private Application xlApp;
private Dictionary<string, double> SkuAmount { get; set; }
private object[,] SelectedCells { get; set; }
2021-12-09 09:47:34 +03:00
private string CurrentFilePath { get; set; }
2021-12-05 16:55:36 +03:00
2021-12-08 14:45:14 +03:00
public Exporter()
2021-12-05 16:55:36 +03:00
{
this.xlApp = (Application)ExcelDnaUtil.Application;
2021-12-09 09:47:34 +03:00
this.CurrentFilePath = xlApp.ActiveWorkbook.FullName;
2021-12-05 16:55:36 +03:00
2021-12-09 09:47:34 +03:00
_GetSelectedCells();
2021-12-05 16:55:36 +03:00
}
2021-12-09 09:47:34 +03:00
private void _GetSelectedCells()
2021-12-05 16:55:36 +03:00
{
Range selection = xlApp.Selection;
this.SelectedCells = (object[,])selection.Value2;
}
public bool IsRangeValid()
{
return SelectedCells != null &&
SelectedCells.GetLength(1) == 2;
}
public void FillSkuAmountDict()
{
SkuAmount = new Dictionary<string, double>();
int rowsCount = SelectedCells.GetLength(0);
for (int row = 1; row <= rowsCount; row++)
{
if (SelectedCells[row, 1] == null || SelectedCells[row, 2] == null)
continue;
string sku = null;
double? amount = null;
for (int column = 1; column <= 2; column++)
{
object current = SelectedCells[row, column];
if (current.GetType() == typeof(string)
&& SkuAssist.IsRehauSku((string)current))
sku = (string)current;
else if (current.GetType() == typeof(string)
&& double.TryParse((string)current, 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);
}
}
2021-12-09 09:14:19 +03:00
public void FillPriceList()
{
2021-12-09 09:47:34 +03:00
string exportFile = _GetExportFullPath();
File.Copy(AddIn.priceListPath, exportFile, true);
2021-12-09 09:14:19 +03:00
2021-12-09 09:47:34 +03:00
Workbook wb = xlApp.Workbooks.Open(exportFile);
Worksheet ws = wb.ActiveSheet;
2021-12-09 09:14:19 +03:00
2021-12-09 09:47:34 +03:00
Range amountCell = ws.Cells.Find("Кол-во");
2021-12-07 08:27:30 +03:00
2021-12-09 09:47:34 +03:00
foreach (KeyValuePair<string, double> kvp in SkuAmount)
{
Range cell = ws.Cells.Find(kvp.Key);
ws.Cells[cell.Row, amountCell.Column].Value = kvp.Value;
}
2021-12-07 08:27:30 +03:00
2021-12-09 09:47:34 +03:00
ws.Cells.AutoFilter(7, "<>");
2021-12-09 09:14:19 +03:00
}
2021-12-08 09:45:03 +03:00
2021-12-09 09:47:34 +03:00
private string _GetExportFullPath()
2021-12-09 09:14:19 +03:00
{
2021-12-09 09:47:34 +03:00
string fileExtension = Path.GetExtension(AddIn.priceListPath);
2021-12-08 09:45:03 +03:00
2021-12-09 09:47:34 +03:00
return Path.GetTempFileName() + fileExtension;
2021-12-09 09:14:19 +03:00
}
2021-12-08 09:45:03 +03:00
2021-12-05 16:55:36 +03:00
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
}
}
}