2 компонент 2 лаба
This commit is contained in:
parent
d1c4275408
commit
1e39b833c9
@ -1,9 +1,15 @@
|
||||
using COP.Info;
|
||||
using DocumentFormat.OpenXml.EMMA;
|
||||
using DocumentFormat.OpenXml.Office2010.Excel;
|
||||
using DocumentFormat.OpenXml.Spreadsheet;
|
||||
using NPOI.HSSF.Util;
|
||||
using NPOI.SS.UserModel;
|
||||
using NPOI.SS.Util;
|
||||
using NPOI.XSSF.UserModel;
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
using BorderStyle = NPOI.SS.UserModel.BorderStyle;
|
||||
using HorizontalAlignment = NPOI.SS.UserModel.HorizontalAlignment;
|
||||
|
||||
namespace COP
|
||||
{
|
||||
@ -21,7 +27,7 @@ namespace COP
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
public void GenerateDocument(ExcelTableInfo info)
|
||||
public void GenerateDocument<T>(ExcelTableInfo<T> info) where T: class
|
||||
{
|
||||
if (string.IsNullOrEmpty(info.FilePath))
|
||||
{
|
||||
@ -36,154 +42,102 @@ namespace COP
|
||||
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();
|
||||
int curRow = 1;
|
||||
int curCol = 0;
|
||||
List<string> headers = 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 dataCellStyle = GetDataCellStyle(workbook);
|
||||
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++)
|
||||
|
||||
foreach (var header in info.Headers)
|
||||
{
|
||||
var dataCell = headerRow.CreateCell(col + 2);
|
||||
var value = GetPropertyValue(info.Data[col], row);
|
||||
dataCell.SetCellValue(value.ToString());
|
||||
dataCell.CellStyle = dataCellStyle;
|
||||
headers.Add(header.Key);
|
||||
|
||||
var currentRow = sheet.CreateRow(curRow);
|
||||
var curCell = currentRow.CreateCell(curCol);
|
||||
|
||||
if (header.Value.Item1.Item1.Count > 1)
|
||||
{
|
||||
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);
|
||||
}
|
||||
}*/
|
||||
|
||||
// Объединение ячеек в шапке таблицы
|
||||
/*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));
|
||||
int id = header.Value.Item1.Item1.IndexOf(header.Value.Item1.Item1.First());
|
||||
sheet.AddMergedRegion(new CellRangeAddress(curRow, curRow, curCol, curCol + 1));
|
||||
currentRow.Height = (short)(header.Value.Item2[id] * 20);
|
||||
curRow++;
|
||||
}
|
||||
}
|
||||
}*/
|
||||
|
||||
/*// Задание высоты строк
|
||||
for (int i = 0; i < info.MergeInfo.Count; i++)
|
||||
curCell.SetCellValue(header.Key);
|
||||
curCell.CellStyle = headerCellStyle;
|
||||
if (header.Value.Item1.Item1.Count > 1)
|
||||
{
|
||||
var row = sheet.GetRow(i);
|
||||
if (row != null)
|
||||
{
|
||||
row.Height = (short)(info.MergeInfo[i].RowHeight * 20);
|
||||
curCol--;
|
||||
curRow += header.Value.Item1.Item1.Count;
|
||||
}
|
||||
}
|
||||
|
||||
curCol = 2;
|
||||
foreach (var data in info.Data)
|
||||
{
|
||||
curRow = 1;
|
||||
|
||||
foreach (var header in info.Headers.Values)
|
||||
{
|
||||
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++;
|
||||
}
|
||||
}
|
||||
curCol++;
|
||||
}
|
||||
}*/
|
||||
|
||||
using var fs = new FileStream(info.FilePath, FileMode.Create, FileAccess.Write);
|
||||
workbook.Write(fs);
|
||||
}
|
||||
|
||||
private static object GetPropertyValue(object obj, int columnIndex)
|
||||
private static int? GetRowHeight<T>(string header, ExcelTableInfo<T> info) where T:class
|
||||
{
|
||||
var properties = obj.GetType().GetProperties();
|
||||
var field = properties[columnIndex];
|
||||
|
||||
return field.GetValue(obj);
|
||||
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;
|
||||
}
|
||||
|
||||
private static ICellStyle GetHeaderCellStyle(IWorkbook workbook)
|
||||
@ -197,6 +151,8 @@ namespace COP
|
||||
var font = workbook.CreateFont();
|
||||
font.Boldweight = (short)FontBoldWeight.Bold;
|
||||
style.SetFont(font);
|
||||
style.Alignment = HorizontalAlignment.Center;
|
||||
style.VerticalAlignment = VerticalAlignment.Center;
|
||||
|
||||
return style;
|
||||
}
|
||||
@ -212,4 +168,8 @@ namespace COP
|
||||
return style;
|
||||
}
|
||||
}
|
||||
public class Property
|
||||
{
|
||||
public string? Name { get; set; }
|
||||
}
|
||||
}
|
||||
|
@ -1,77 +1,18 @@
|
||||
namespace COP.Info
|
||||
{
|
||||
public class ExcelTableInfo
|
||||
public class ExcelTableInfo<T> where T: class
|
||||
{
|
||||
public string? FilePath { get; set; } = string.Empty;
|
||||
public string? DocumentTitle { get; set; } = string.Empty;
|
||||
public List<object>? Data { get; set; } = new();
|
||||
public Dictionary<string, (List<string>, List<int>)> Properties = new();
|
||||
|
||||
public ExcelTableInfo(string filePath, string documentTitle, List<object> data, Dictionary<string, (List<string>, List<int>)>? properties)
|
||||
{
|
||||
FilePath = filePath;
|
||||
DocumentTitle = documentTitle;
|
||||
Data = data;
|
||||
Properties = properties;
|
||||
}
|
||||
public void addDictionary(string name, string property, int height)
|
||||
{
|
||||
if (Properties.ContainsKey(name))
|
||||
{
|
||||
Properties[name].Item1.Add(property);
|
||||
Properties[name].Item2.Add(height);
|
||||
}
|
||||
else
|
||||
{
|
||||
Properties.Add(name, (new List<string>(), new List<int>()));
|
||||
Properties[name].Item1.Add(property);
|
||||
Properties[name].Item2.Add(height);
|
||||
}
|
||||
}
|
||||
public void addDictionaryAlone(string item, int height)
|
||||
{
|
||||
|
||||
Properties.Add(item, (new List<string>(), new List<int>()));
|
||||
Properties[item].Item1.Add(item);
|
||||
Properties[item].Item2.Add(height);
|
||||
|
||||
}
|
||||
/*public string FilePath { get; set; } = string.Empty;
|
||||
public string FilePath { get; set; } = string.Empty;
|
||||
public string DocumentTitle { get; set; } = string.Empty;
|
||||
public List<MergeInfo> MergeInfo;
|
||||
public List<string>? HeaderTitles;
|
||||
public List<object>? Data;
|
||||
public Dictionary<string, (List<string>, List<int>)> Headers { get; set; }
|
||||
public void addDictionary(string name, string header, int height)
|
||||
{
|
||||
if (headers.ContainsKey(name))
|
||||
{
|
||||
headers[name].Item1.Add(header);
|
||||
headers[name].Item2.Add(height);
|
||||
}
|
||||
else
|
||||
{
|
||||
headers.Add(name, (new List<string>(), new List<int>()));
|
||||
headers[name].Item1.Add(header);
|
||||
headers[name].Item2.Add(height);
|
||||
}
|
||||
}
|
||||
public void addDictionaryAlone(string header, int height)
|
||||
{
|
||||
public List<T>? Data;
|
||||
public Dictionary<string, ((List<string>, List<string>), List<int>)> Headers { get; set; }
|
||||
|
||||
headers.Add(header, (new List<string>(), new List<int>()));
|
||||
headers[header].Item1.Add(header);
|
||||
headers[header].Item2.Add(height);
|
||||
|
||||
}
|
||||
|
||||
public ExcelTableInfo(string filePath, string documentTitle, List<MergeInfo> mergeInfo, List<string> headerTitles, List<object> data)
|
||||
public ExcelTableInfo (string filePath, string documentTitle, List<T> data, Dictionary<string, ((List<string>, List<string>), List<int>)> headers)
|
||||
{
|
||||
FilePath = filePath;
|
||||
DocumentTitle = documentTitle;
|
||||
MergeInfo = mergeInfo;
|
||||
HeaderTitles = headerTitles;
|
||||
Data = data;
|
||||
}*/
|
||||
Headers = headers;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ using COP.Enums;
|
||||
using COP.Info;
|
||||
using DocumentFormat.OpenXml.EMMA;
|
||||
using System.ComponentModel;
|
||||
using System.Linq.Expressions;
|
||||
using static COP.ExcelComponent;
|
||||
|
||||
namespace TestComponents
|
||||
@ -111,62 +112,23 @@ namespace TestComponents
|
||||
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
}
|
||||
}
|
||||
public class Sportsmen
|
||||
{
|
||||
public string name;
|
||||
public string sport;
|
||||
public string city;
|
||||
public string country;
|
||||
public string Region { get { return city; } set { city = value; } }
|
||||
public string awards;
|
||||
public Sportsmen(string name, string sport, string city, string country, string awards)
|
||||
{
|
||||
this.name = name;
|
||||
this.sport = sport;
|
||||
this.city = city;
|
||||
this.country = country;
|
||||
this.awards = awards;
|
||||
}
|
||||
public Sportsmen() { }
|
||||
public override string ToString()
|
||||
{
|
||||
return country + " " + city + " " + name + " " + sport + " " + awards;
|
||||
}
|
||||
}
|
||||
|
||||
string[] names = { "Vova M", "Sasha A", "Dima D", "Danila L" };
|
||||
string[] sports = { "Run", "Swim", "Cycle", "Race", "Box" };
|
||||
string[] cities = { "Moskow", "Samara", "Piter", "Kazan", "Kyrsk" };
|
||||
string[] countries = { "Russia", "Spain", "China", "India", "Brazil" };
|
||||
string[] rewards = { "#1", "#2", "KMS", "Olymp", "MS" };
|
||||
List<Employee> sportsmens = new();
|
||||
|
||||
private void buttonSaveTable_Click(object sender, EventArgs e)
|
||||
{
|
||||
ExcelTable table = new();
|
||||
string path = @"C:\\Users\\User\\Documents\\testtable.xlsx";
|
||||
string title = "title";
|
||||
var dataList = new List<object>
|
||||
var data = new List<Employee>
|
||||
{
|
||||
new Employee { Id = 1, Status = "Active", Name = "John", Surname = "Doe", Age = "30", Department = "IT", Position = "Manager" },
|
||||
new Employee { Id = 2, Status = "Active", Name = "Jane", Surname = "Smith", Age = "35", Department = "Design", Position = "Senior" },
|
||||
};
|
||||
|
||||
var info = new ExcelTableInfo(path, "Sample Document", new List<object>(), new Dictionary<string, (List<string>, List<int>)>());
|
||||
|
||||
info.addDictionary("Table1", "Field1", 20);
|
||||
info.addDictionary("Table1", "Field2", 15);
|
||||
info.addDictionary("Table2", "Field1", 25);
|
||||
info.addDictionary("Table2", "Field2", 18);
|
||||
info.addDictionary("Table2", "Field3", 22);
|
||||
|
||||
info.addDictionaryAlone("Table3", 30);
|
||||
|
||||
info.Data.Add(new Employee1 { Department = "Dept1", Position = "Position1", Name = "Employee1" });
|
||||
info.Data.Add(new Employee1 { Department = "Dept2", Position = "Position2", Name = "Employee2" });
|
||||
info.Data.Add(new Employee1 { Department = "Dept1", Position = "Position1", Name = "Employee3" });
|
||||
|
||||
dataList.Clear();
|
||||
Dictionary<string, ((List<string>, List<string>), List<int>)> headers = new()
|
||||
{
|
||||
{ "Идентификатор", ((new List<string> { "Id" }, new List<string> {"Идентификатор"} ), new List<int> { 30 } )},
|
||||
{ "Личные данные", ((new List <string> { "Name", "Surname", "Age" }, new List<string> { "Имя", "Фамилия", "Возраст" }), new List<int> { 25, 25, 25 }) },
|
||||
{ "Статус", ((new List<string> { "Status" }, new List<string> { "Статус"}), new List<int> { 30 } )},
|
||||
{ "Работа", ((new List < string > { "Department", "Position" } , new List < string > { "Отдел", "Должность" }), new List<int> { 25, 25 } )}
|
||||
};
|
||||
ExcelTableInfo<Employee> info = new("C:\\Users\\User\\Documents\\testtable.xlsx", "My Document", data, headers);
|
||||
try
|
||||
{
|
||||
table.GenerateDocument(info);
|
||||
@ -177,32 +139,6 @@ namespace TestComponents
|
||||
MessageBox.Show(ex.Message, "Ошибка",
|
||||
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
}
|
||||
/*ExcelTable table = new();
|
||||
|
||||
var mergeInfo = new List<MergeInfo>
|
||||
{
|
||||
new MergeInfo { Value = "", StartRow = 0, EndRow = 0, StartCol = 0, EndCol = 1 },
|
||||
new MergeInfo { Value = "", StartRow = 1, EndRow = 1, StartCol = 0, EndCol = 1 },
|
||||
new MergeInfo { Value = "Личные данные", StartRow = 2, EndRow = 4, StartCol = 0, EndCol = 0 },
|
||||
new MergeInfo { Value = "Работа", StartRow = 5, EndRow = 6, StartCol = 0, EndCol = 0 }
|
||||
};
|
||||
var headerTitles = new List<string> { "ID", "Status", "Name", "Surname", "Age", "Department", "Position" };
|
||||
var data = new List<object>
|
||||
{
|
||||
new Employees { Id = 1, Status = "Active", Name = "John", Surname = "Doe", Age = "30", Department = "IT", Position = "Manager" },
|
||||
new Employees { Id = 2, Status = "Active", Name = "Jane", Surname = "Smith", Age = "35", Department = "Design", Position = "Senior" },
|
||||
};
|
||||
ExcelTableInfo info = new("C:\\Users\\User\\Documents\\testtable.xlsx", "My Document", mergeInfo, headerTitles, data);
|
||||
try
|
||||
{
|
||||
table.GenerateDocument(info);
|
||||
MessageBox.Show("Сохарнено успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show(ex.Message, "Ошибка",
|
||||
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
}*/
|
||||
}
|
||||
|
||||
public class Employee
|
||||
|
Loading…
Reference in New Issue
Block a user