2023-10-13 17:33:04 +03:00

146 lines
5.9 KiB
C#

using OfficeOpenXml.Drawing;
using OfficeOpenXml;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LicenseContext = OfficeOpenXml.LicenseContext;
using System.Reflection;
using System.Xml.Linq;
using OfficeOpenXml.Style;
using System.Data.Common;
namespace UnvisableComponents
{
public partial class ExcelHead : Component
{
public ExcelHead()
{
InitializeComponent();
}
public ExcelHead(IContainer container)
{
container.Add(this);
InitializeComponent();
}
public void Load<T>(ExcelInfo<T> info) where T:class
{
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
Check<T>(info);
List<string> fields = new List<string>();
using (ExcelPackage excelPackage = new ExcelPackage())
{
excelPackage.Workbook.Properties.Author = "Qawithexperts";
excelPackage.Workbook.Properties.Title = "test Excel";
excelPackage.Workbook.Properties.Subject = "Write in Excel";
excelPackage.Workbook.Properties.Created = DateTime.Now;
ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets.Add("Sheet 1");
worksheet.Cells["A1"].Value = info.Title;
int posString = 2;
int posColumn = 1;
foreach(var step in info.Filds)
{
if(step.Value.Item1.Count == 1)
{
worksheet.Cells[posString, posColumn, posString, posColumn+1].Merge = true;
worksheet.Cells[posString, posColumn].Value = step.Key;
worksheet.Cells[posString, posColumn].Style.Fill.PatternType = ExcelFillStyle.Solid;
worksheet.Cells[posString, posColumn].Style.Fill.BackgroundColor.SetColor(Color.LightGreen);
fields.Add(step.Key);
posString++;
continue;
}
worksheet.Cells[posString, posColumn, posString + step.Value.Item1.Count-1, posColumn].Merge = true;
worksheet.Cells[posString, posColumn].Value = step.Key;
worksheet.Cells[posString, posColumn].Style.TextRotation = 180;
worksheet.Cells[posString, posColumn].Style.Fill.PatternType = ExcelFillStyle.Solid;
worksheet.Cells[posString, posColumn].Style.Fill.BackgroundColor.SetColor(Color.AliceBlue);
worksheet.Column(posColumn).BestFit = true;
posColumn++;
foreach(var field in step.Value.Item1)
{
int id = step.Value.Item1.IndexOf(field);
worksheet.Cells[posString, posColumn].Value = field;
worksheet.Cells[posString, posColumn].Style.Fill.PatternType = ExcelFillStyle.Solid;
worksheet.Cells[posString, posColumn].Style.Fill.BackgroundColor.SetColor(Color.AliceBlue);
worksheet.Row(posString).Height = step.Value.Item2[id];
fields.Add(field);
posString++;
}
posColumn--;
}
posColumn = 3;
foreach (var step in info.Dates)
{
posString = 2;
var type = step.GetType();
foreach(var field in fields)
{
worksheet.Cells[posString, posColumn].Value =
type.GetField(field) == null ? type.GetProperty(field).GetValue(step) : type.GetField(field).GetValue(step);
posString++;
}
posColumn++;
}
CheckTable(worksheet, fields.Count, info.Dates.Count);
FileInfo fi = new FileInfo(info.Path);
excelPackage.SaveAs(fi);
}
}
public void Check<T>(ExcelInfo<T> info) where T : class
{
if (string.IsNullOrEmpty(info.Title))
throw new Exception("Забыли заглавие");
if (string.IsNullOrEmpty(info.Path))
throw new Exception("Забыли путь");
if (!info.Path.EndsWith(".xlsx"))
info.Path += ".xlsx";
if (info.Dates.Count == 0)
throw new Exception("забыли данные");
}
public void CheckTable(ExcelWorksheet worksheet, int rows, int columns)
{
int posString = 2;
int posColumn = 1;
rows += posString;
columns += posColumn;
while(posString < rows)
{
while (posColumn < columns)
{
if (worksheet.Cells[posString, posColumn].Value == null && !worksheet.Cells[posString, posColumn - 1 ,posString, posColumn].Merge)
throw new Exception("Пустая ячейка");
posColumn++;
}
posString++;
}
posString = 2;
posColumn = 3 + columns;
while(posString < rows )
{
if (worksheet.Cells[posString, posColumn].Value != null)
throw new Exception("Выход за границы");
posString++;
}
posString = 2 + rows;
posColumn = 1;
while (posColumn < columns)
{
if (worksheet.Cells[posString, posColumn].Value != null)
throw new Exception("Выход за границы");
posColumn++;
}
}
}
}