RhSolutions-AddIn/Source/DataExport/ExportTool.cs

130 lines
3.7 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-23 15:31:23 +03:00
public class ExportTool : IDisposable
2021-12-05 16:55:36 +03:00
{
private Application xlApp;
private Dictionary<string, double> SkuAmount { get; set; }
2021-12-23 15:31:23 +03:00
private Range Selection { 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-23 15:31:23 +03:00
public ExportTool()
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
{
2021-12-23 15:31:23 +03:00
Selection = xlApp.Selection;
2021-12-05 16:55:36 +03:00
}
public bool IsRangeValid()
{
2021-12-23 15:31:23 +03:00
return Selection.Columns.Count == 2;
2021-12-05 16:55:36 +03:00
}
2021-12-22 17:07:37 +03:00
private void FillSkuAmountDict()
2021-12-05 16:55:36 +03:00
{
2021-12-23 15:31:23 +03:00
object[,] cells = Selection.Value2;
2021-12-05 16:55:36 +03:00
SkuAmount = new Dictionary<string, double>();
2021-12-23 15:31:23 +03:00
int rowsCount = Selection.Rows.Count;
2021-12-05 16:55:36 +03:00
for (int row = 1; row <= rowsCount; row++)
{
2021-12-23 15:31:23 +03:00
if (cells[row, 1] == null || cells[row, 2] == null)
2021-12-05 16:55:36 +03:00
continue;
string sku = null;
double? amount = null;
for (int column = 1; column <= 2; column++)
{
2021-12-23 15:31:23 +03:00
object current = cells[row, column];
2021-12-05 16:55:36 +03:00
if (current.GetType() == typeof(string)
&& ((string)current).IsRehauSku())
2021-12-05 16:55:36 +03:00
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-22 17:07:37 +03:00
public void FillNewPriceList()
2021-12-09 09:14:19 +03:00
{
2021-12-23 15:31:23 +03:00
const string amountHeader = "Кол-во";
const string skuHeader = "Актуальный материал";
2021-12-22 17:07:37 +03:00
FillSkuAmountDict();
2021-12-09 09:47:34 +03:00
string exportFile = _GetExportFullPath();
2021-12-22 08:26:54 +03:00
File.Copy(RegistryUtil.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);
2021-12-23 15:31:23 +03:00
Worksheet ws = wb.Sheets["КП"];
ws.Activate();
2021-12-09 09:14:19 +03:00
2021-12-23 15:31:23 +03:00
int amountColumn = ws.Cells.Find(amountHeader).Column;
int skuColumn = ws.Cells.Find(skuHeader).Column;
2021-12-07 08:27:30 +03:00
2021-12-09 09:47:34 +03:00
foreach (KeyValuePair<string, double> kvp in SkuAmount)
{
2021-12-22 17:07:37 +03:00
Range cell = ws.Columns[skuColumn].Find(kvp.Key);
ws.Cells[cell.Row, amountColumn].Value = kvp.Value;
2021-12-09 09:47:34 +03:00
}
2021-12-07 08:27:30 +03:00
2021-12-23 15:31:23 +03:00
AutoFilter filter = ws.AutoFilter;
int firstFilterColumn = filter.Range.Column;
filter.Range.AutoFilter(amountColumn - firstFilterColumn + 1, "<>");
ws.Range["A1"].Activate();
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-22 08:26:54 +03:00
string fileExtension = Path.GetExtension(RegistryUtil.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)
{
}
}
2021-12-23 15:31:23 +03:00
class SelectionCheck
{
}
2021-12-05 16:55:36 +03:00
}