PIbd-32_Artamonova_T.V._COP_2/COP/ExcelTable.cs

216 lines
8.1 KiB
C#

using COP.Info;
using NPOI.HSSF.Util;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System.ComponentModel;
using BorderStyle = NPOI.SS.UserModel.BorderStyle;
namespace COP
{
public partial class ExcelTable : Component
{
public ExcelTable()
{
InitializeComponent();
}
public ExcelTable(IContainer container)
{
container.Add(this);
InitializeComponent();
}
public void GenerateDocument(ExcelTableInfo info)
{
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);
int posString = 2;
int posColumn = 1;
List<string> properties = new();
foreach (var property in info.Properties)
{
if (property.Value.Item1.Count == 1)
{
var cell1 = sheet.GetRow(posString).CreateCell(posColumn);
cell1.SetCellValue(property.Key);
cell1.CellStyle.FillPattern = FillPattern.SolidForeground;
cell1.CellStyle.FillForegroundColor = HSSFColor.LightGreen.Index;
properties.Add(property.Key);
posString++;
continue;
}
var cell = sheet.GetRow(posString).CreateCell(posColumn);
cell.SetCellValue(property.Key);
cell.CellStyle.Rotation = 180;
cell.CellStyle.FillPattern = FillPattern.SolidForeground;
cell.CellStyle.FillForegroundColor = HSSFColor.Aqua.Index;
sheet.SetColumnWidth(posColumn, sheet.DefaultColumnWidth);
posColumn++;
foreach (var field in property.Value.Item1)
{
int id = property.Value.Item1.IndexOf(field);
var cellValue = sheet.GetRow(posString).CreateCell(posColumn);
cellValue.SetCellValue(field);
cellValue.CellStyle.FillPattern = FillPattern.SolidForeground;
cellValue.CellStyle.FillForegroundColor = HSSFColor.Aqua.Index;
sheet.GetRow(posString).Height = (short)property.Value.Item2[id];
properties.Add(field);
posString++;
}
posColumn--;
}
posColumn = 3;
foreach (var data in info.Data)
{
posString = 2;
var type = data.GetType();
foreach (var property in properties)
{
var cellValue = sheet.GetRow(posString).CreateCell(posColumn);
cellValue.SetCellValue((string)(type.GetField(property) == null ? type.GetProperty(property).GetValue(data) : type.GetField(property).GetValue(data)));
posString++;
}
posColumn++;
}
/*var dataCellStyle = GetDataCellStyle(workbook);
for (int row = 0; row < info.HeaderTitles.Count; row++)
{
// Создание заголовков для шапки таблицы
var headerRow = sheet.CreateRow(row + 1);
var headerCellStyle = GetHeaderCellStyle(workbook);
var cell = headerRow.CreateCell(0);
cell.SetCellValue(info.HeaderTitles[row]);
cell.CellStyle = headerCellStyle;
// Заполнение таблицы
for (int col = 0; col < info.Data.Count; col++)
{
var dataCell = headerRow.CreateCell(col + 2);
var value = GetPropertyValue(info.Data[col], row);
dataCell.SetCellValue(value.ToString());
dataCell.CellStyle = dataCellStyle;
}
}*/
// Объединение ячеек в шапке таблицы
/*for (int i = 0; i < info.MergeInfo.Count; i++)
{
var cellMergeInfo = info.MergeInfo[i];
var mergeStartCellRef = CellReference.ConvertNumToColString(cellMergeInfo.StartCol) + (cellMergeInfo.StartRow + 1);
var mergeEndCellRef = CellReference.ConvertNumToColString(cellMergeInfo.EndCol) + (cellMergeInfo.EndRow + 1);
sheet.AddMergedRegion(new CellRangeAddress(cellMergeInfo.StartRow, cellMergeInfo.EndRow, cellMergeInfo.StartCol, cellMergeInfo.EndCol));
var mergeCell = sheet.GetRow(cellMergeInfo.StartRow).CreateCell(cellMergeInfo.StartCol);
mergeCell.SetCellValue(cellMergeInfo.Value);
}*/
/*for (int row = 0; row < info.HeaderTitles.Count; row++)
{
sheet.AddMergedRegion(new CellRangeAddress(row, row, 0, 1));
foreach (var merge in info.MergeInfo)
if (merge.Value != "")
{
sheet.RemoveMergedRegion(row)
}
foreach (var mergedRegion in mergedRegions)
{
sheet.RemoveMergedRegion(mergedRegion);
}
}*/
/*foreach (var merge in info.MergeInfo)
{
for (int row = 0; row < info.HeaderTitles.Count; row++)
{
if (merge.Value != "")
{
var valueCell = sheet.GetRow(row + 1).GetCell(0);
valueCell.SetCellValue(merge.Value);
sheet.AddMergedRegion(new CellRangeAddress(merge.StartRow, merge.EndRow, merge.StartCol, merge.EndCol));
}
else
{
sheet.AddMergedRegion(new CellRangeAddress(merge.StartRow, merge.EndRow, 0, 1));
}
}
}*/
/*// Задание высоты строк
for (int i = 0; i < info.MergeInfo.Count; i++)
{
var row = sheet.GetRow(i);
if (row != null)
{
row.Height = (short)(info.MergeInfo[i].RowHeight * 20);
}
}*/
using var fs = new FileStream(info.FilePath, FileMode.Create, FileAccess.Write);
workbook.Write(fs);
}
private static object GetPropertyValue(object obj, int columnIndex)
{
var properties = obj.GetType().GetProperties();
var field = properties[columnIndex];
return field.GetValue(obj);
}
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);
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;
}
}
}