PIbd-31_Shanygin.A.V._COP_27/ComponentOrientedProgramming/NotVisualComponent/ExcelHardTable.cs

73 lines
2.4 KiB
C#
Raw Normal View History

2024-09-19 19:34:15 +04:00
using NotVisualComponent.Helpers;
using NotVisualComponent.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NotVisualComponent
{
public partial class ExcelHardTable : Component
{
public ExcelHardTable()
{
InitializeComponent();
}
public ExcelHardTable(IContainer container)
{
container.Add(this);
InitializeComponent();
}
public void CreateDoc<T>(TableWithHeaderConfig<T> config)
{
config.CheckFields();
config.ColumnsRowsDataCount = (config.ColumnsRowsWidth.Count, config.Data.Count + 1);
IContext creator = new WorkWithExcel();
creator.CreateHeader(config.Header);
creator.CreateTableWithHeader();
creator.CreateMultiHeader(config);
var array = new string[config.Data.Count, config.Headers.Count];
for (var j = 0; j < config.Data.Count; j++)
{
for (var i = 0; i < config.Headers.Count; i++)
{
(int, int, string, string) first = (0, 0, null, null);
foreach (var x in config.Headers.Where(x => x.ColumnIndex == i))
{
first = x;
break;
}
var (_, _, _, name) = first;
if (name != null)
{
var propertyInfo = config.Data[j]?.GetType().GetProperty(name);
if (propertyInfo != null)
{
object? value = propertyInfo.GetValue(config.Data[j], null);
array[j, i] = value == null
? config.NullReplace
: value.ToString();
}
else
{
array[j, i] = config.NullReplace;
}
}
else
{
array[j, i] = config.NullReplace;
}
}
}
creator.LoadDataToTableWithMultiHeader(array, config.ColumnsRowsWidth[1].Row);
creator.SaveDoc(config.FilePath);
}
}
}