COP/ExcelComponents/ExcelTable.cs
2024-07-29 20:21:07 +04:00

61 lines
1.3 KiB
C#

using ExcelComponents.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ExcelComponents
{
public partial class ExcelTable : Component
{
private readonly ExcelBuilder builder = new();
public ExcelTable()
{
InitializeComponent();
}
public ExcelTable(IContainer container)
{
container.Add(this);
InitializeComponent();
}
private static bool ConfigIsCorrect<T>(TableConfig<T> config)
{
if (config == null)
{
return false;
}
if (string.IsNullOrEmpty(config.FilePath) || File.Exists(config.FilePath))
{
return false;
}
int[] mergeIndexes = config.Merge.Keys.ToArray();
Array.Sort(mergeIndexes);
for(int i = 0; i < mergeIndexes.Length - 1; i++)
{
if (mergeIndexes[i] + config.Merge[mergeIndexes[i]] >= mergeIndexes[i+1])
{
return false;
}
}
return true;
}
public void GenTable<T>(TableConfig<T> config)
{
if(!ConfigIsCorrect(config))
{
return;
}
builder.CreateDocument();
builder.ConfigureColumns(config.ColWidth);
builder.InsertText(1, 1, config.Title);
builder.InsertTable(config.Merge, config.Headers, config.Props, config.Data);
builder.SaveDocument(config.FilePath);
}
}
}