RhSolutions-AddIn/RhSolutions.AddIn/Controllers/CombineTool.cs
2023-03-28 09:58:43 +03:00

67 lines
2.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Microsoft.Office.Interop.Excel;
using RhSolutions.AddIn;
using RhSolutions.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using Range = Microsoft.Office.Interop.Excel.Range;
namespace RhSolutions.Controllers
{
internal class CombineTool : ToolBase
{
private List<SourcePriceList> SourceFiles { get; set; }
public CombineTool()
{
var dialog = RhSolutionsAddIn.Excel.FileDialog[Microsoft.Office.Core.MsoFileDialogType.msoFileDialogFilePicker];
dialog.AllowMultiSelect = true;
dialog.Filters.Add("Файлы Excel", "*.xls; *.xlsx; *.xlsm");
if (dialog.Show() < 0)
{
List<string> files = new();
foreach (string file in dialog.SelectedItems)
{
files.Add(file);
}
SourceFiles = SourcePriceList.GetSourceLists(files.ToArray());
}
else
{
throw new Exception("Не выбраны файлы");
}
}
public override void FillTarget()
{
using (ProgressBar = new ProgressBar("Заполняю строки...", SourceFiles.Sum(file => file.PositionAmount.Count)))
using (ResultBar = new ResultBar())
{
foreach (SourcePriceList source in SourceFiles)
{
TargetFile.Sheet.Columns[TargetFile.AmountCell.Column]
.EntireColumn
.Insert(XlInsertShiftDirection.xlShiftToRight, XlInsertFormatOrigin.xlFormatFromRightOrBelow);
Range newColumnHeader = TargetFile.Sheet.Cells[TargetFile.AmountCell.Row, TargetFile.AmountCell.Column - 1];
newColumnHeader.Value2 = $"{source.Name}";
newColumnHeader.WrapText = true;
foreach (var kvp in source.PositionAmount)
{
FillPositionAmountToColumns(kvp, TargetFile.AmountCell.Column - 1, TargetFile.AmountCell.Column);
ProgressBar.Update();
}
}
FilterByAmount();
ResultBar.Update();
}
}
}
}