2023-10-13 21:09:48 +04:00
|
|
|
|
using COP.Info;
|
2023-10-24 01:43:07 +04:00
|
|
|
|
using DocumentFormat.OpenXml.EMMA;
|
|
|
|
|
using DocumentFormat.OpenXml.Office2010.Excel;
|
|
|
|
|
using DocumentFormat.OpenXml.Spreadsheet;
|
2023-10-13 21:09:48 +04:00
|
|
|
|
using NPOI.HSSF.Util;
|
|
|
|
|
using NPOI.SS.UserModel;
|
2023-10-24 01:43:07 +04:00
|
|
|
|
using NPOI.SS.Util;
|
2023-10-13 21:09:48 +04:00
|
|
|
|
using NPOI.XSSF.UserModel;
|
2023-10-24 01:43:07 +04:00
|
|
|
|
using System;
|
2023-10-13 21:09:48 +04:00
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using BorderStyle = NPOI.SS.UserModel.BorderStyle;
|
2023-10-24 01:43:07 +04:00
|
|
|
|
using HorizontalAlignment = NPOI.SS.UserModel.HorizontalAlignment;
|
2023-10-13 21:09:48 +04:00
|
|
|
|
|
|
|
|
|
namespace COP
|
|
|
|
|
{
|
|
|
|
|
public partial class ExcelTable : Component
|
|
|
|
|
{
|
|
|
|
|
public ExcelTable()
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ExcelTable(IContainer container)
|
|
|
|
|
{
|
|
|
|
|
container.Add(this);
|
|
|
|
|
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-24 01:43:07 +04:00
|
|
|
|
public void GenerateDocument<T>(ExcelTableInfo<T> info) where T: class
|
2023-10-13 21:09:48 +04:00
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(info.FilePath))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("File path is null or empty.");
|
|
|
|
|
}
|
|
|
|
|
if (string.IsNullOrEmpty(info.DocumentTitle))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("Document title is null or empty.");
|
|
|
|
|
}
|
|
|
|
|
if (info.Data == null || info.Data.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("Data is null or empty.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var workbook = new XSSFWorkbook();
|
|
|
|
|
var sheet = workbook.CreateSheet("Sheet1");
|
|
|
|
|
|
|
|
|
|
sheet.CreateRow(0).CreateCell(0).SetCellValue(info.DocumentTitle);
|
|
|
|
|
|
2023-10-24 01:43:07 +04:00
|
|
|
|
int curRow = 1;
|
|
|
|
|
int curCol = 0;
|
|
|
|
|
List<string> headers = new();
|
2023-10-13 21:09:48 +04:00
|
|
|
|
|
2023-10-24 01:43:07 +04:00
|
|
|
|
var dataCellStyle = GetDataCellStyle(workbook);
|
|
|
|
|
var headerCellStyle = GetHeaderCellStyle(workbook);
|
2023-10-13 21:09:48 +04:00
|
|
|
|
|
2023-10-24 01:43:07 +04:00
|
|
|
|
foreach (var header in info.Headers)
|
2023-10-13 21:09:48 +04:00
|
|
|
|
{
|
2023-10-24 01:43:07 +04:00
|
|
|
|
headers.Add(header.Key);
|
2023-10-13 21:09:48 +04:00
|
|
|
|
|
2023-10-24 01:43:07 +04:00
|
|
|
|
var currentRow = sheet.CreateRow(curRow);
|
|
|
|
|
var curCell = currentRow.CreateCell(curCol);
|
2023-10-13 21:09:48 +04:00
|
|
|
|
|
2023-10-24 01:43:07 +04:00
|
|
|
|
if (header.Value.Item1.Item1.Count > 1)
|
2023-10-13 21:09:48 +04:00
|
|
|
|
{
|
2023-10-24 01:43:07 +04:00
|
|
|
|
sheet.AddMergedRegion(new CellRangeAddress(curRow, curRow + header.Value.Item1.Item1.Count - 1, curCol, curCol));
|
|
|
|
|
curCol++;
|
|
|
|
|
foreach (var item in header.Value.Item1.Item2)
|
|
|
|
|
{
|
|
|
|
|
int id = header.Value.Item1.Item2.IndexOf(item);
|
|
|
|
|
var cellValue = currentRow.CreateCell(1);
|
|
|
|
|
cellValue.SetCellValue(item);
|
|
|
|
|
currentRow.Height = (short)(header.Value.Item2[id] * 20);
|
|
|
|
|
headers.Add(item);
|
|
|
|
|
cellValue.CellStyle = headerCellStyle;
|
|
|
|
|
currentRow = sheet.CreateRow(currentRow.RowNum + 1);
|
|
|
|
|
}
|
2023-10-13 21:09:48 +04:00
|
|
|
|
}
|
2023-10-24 01:43:07 +04:00
|
|
|
|
else
|
2023-10-13 21:09:48 +04:00
|
|
|
|
{
|
2023-10-24 20:50:02 +04:00
|
|
|
|
int id = header.Value.Item1.Item1.IndexOf(header.Value.Item1.Item1[0]);
|
2023-10-24 01:43:07 +04:00
|
|
|
|
sheet.AddMergedRegion(new CellRangeAddress(curRow, curRow, curCol, curCol + 1));
|
|
|
|
|
currentRow.Height = (short)(header.Value.Item2[id] * 20);
|
|
|
|
|
curRow++;
|
2023-10-13 21:09:48 +04:00
|
|
|
|
}
|
|
|
|
|
|
2023-10-24 01:43:07 +04:00
|
|
|
|
curCell.SetCellValue(header.Key);
|
|
|
|
|
curCell.CellStyle = headerCellStyle;
|
|
|
|
|
if (header.Value.Item1.Item1.Count > 1)
|
2023-10-13 21:09:48 +04:00
|
|
|
|
{
|
2023-10-24 01:43:07 +04:00
|
|
|
|
curCol--;
|
|
|
|
|
curRow += header.Value.Item1.Item1.Count;
|
2023-10-13 21:09:48 +04:00
|
|
|
|
}
|
2023-10-24 01:43:07 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
curCol = 2;
|
|
|
|
|
foreach (var data in info.Data)
|
2023-10-13 21:09:48 +04:00
|
|
|
|
{
|
2023-10-24 01:43:07 +04:00
|
|
|
|
curRow = 1;
|
2023-10-13 21:09:48 +04:00
|
|
|
|
|
2023-10-24 01:43:07 +04:00
|
|
|
|
foreach (var header in info.Headers.Values)
|
2023-10-13 21:09:48 +04:00
|
|
|
|
{
|
2023-10-24 01:43:07 +04:00
|
|
|
|
foreach (var item in header.Item1.Item1)
|
|
|
|
|
{
|
|
|
|
|
var property = typeof(T).GetProperty(item);
|
|
|
|
|
var cellValue = sheet.GetRow(curRow)?.CreateCell(curCol);
|
|
|
|
|
if (cellValue != null && property != null)
|
|
|
|
|
{
|
|
|
|
|
var value = property?.GetValue(data)?.ToString();
|
|
|
|
|
var rowHeight = GetRowHeight(item, info);
|
|
|
|
|
var currentRow = sheet.GetRow(curRow);
|
|
|
|
|
if (rowHeight != null)
|
|
|
|
|
{
|
|
|
|
|
currentRow.Height = (short)(rowHeight.Value * 20);
|
|
|
|
|
}
|
|
|
|
|
cellValue.SetCellValue(value);
|
|
|
|
|
cellValue.CellStyle = dataCellStyle;
|
|
|
|
|
}
|
|
|
|
|
curRow++;
|
|
|
|
|
}
|
2023-10-13 21:09:48 +04:00
|
|
|
|
}
|
2023-10-24 01:43:07 +04:00
|
|
|
|
curCol++;
|
|
|
|
|
}
|
2023-10-13 21:09:48 +04:00
|
|
|
|
|
|
|
|
|
using var fs = new FileStream(info.FilePath, FileMode.Create, FileAccess.Write);
|
|
|
|
|
workbook.Write(fs);
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-24 01:43:07 +04:00
|
|
|
|
private static int? GetRowHeight<T>(string header, ExcelTableInfo<T> info) where T:class
|
2023-10-13 21:09:48 +04:00
|
|
|
|
{
|
2023-10-24 01:43:07 +04:00
|
|
|
|
foreach (var item in info.Headers)
|
|
|
|
|
{
|
|
|
|
|
foreach (var elem in item.Value.Item1.Item1)
|
|
|
|
|
{
|
|
|
|
|
int id = item.Value.Item1.Item1.IndexOf(elem);
|
|
|
|
|
if (item.Value.Item1.Item1[id] == header)
|
|
|
|
|
return item.Value.Item2[id];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
2023-10-13 21:09:48 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static ICellStyle GetHeaderCellStyle(IWorkbook workbook)
|
|
|
|
|
{
|
|
|
|
|
var style = workbook.CreateCellStyle();
|
|
|
|
|
style.BorderBottom = BorderStyle.Thin;
|
|
|
|
|
style.BorderLeft = BorderStyle.Thin;
|
|
|
|
|
style.BorderRight = BorderStyle.Thin;
|
|
|
|
|
style.BorderTop = BorderStyle.Thin;
|
|
|
|
|
|
|
|
|
|
var font = workbook.CreateFont();
|
|
|
|
|
font.Boldweight = (short)FontBoldWeight.Bold;
|
|
|
|
|
style.SetFont(font);
|
2023-10-24 01:43:07 +04:00
|
|
|
|
style.Alignment = HorizontalAlignment.Center;
|
|
|
|
|
style.VerticalAlignment = VerticalAlignment.Center;
|
2023-10-13 21:09:48 +04:00
|
|
|
|
|
|
|
|
|
return style;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static ICellStyle GetDataCellStyle(IWorkbook workbook)
|
|
|
|
|
{
|
|
|
|
|
var style = workbook.CreateCellStyle();
|
|
|
|
|
style.BorderBottom = BorderStyle.Thin;
|
|
|
|
|
style.BorderLeft = BorderStyle.Thin;
|
|
|
|
|
style.BorderRight = BorderStyle.Thin;
|
|
|
|
|
style.BorderTop = BorderStyle.Thin;
|
|
|
|
|
|
|
|
|
|
return style;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-10-24 01:43:07 +04:00
|
|
|
|
public class Property
|
|
|
|
|
{
|
|
|
|
|
public string? Name { get; set; }
|
|
|
|
|
}
|
2023-10-13 21:09:48 +04:00
|
|
|
|
}
|