From 8197103098d89522f6fb41a33a1bf7407a7b239e Mon Sep 17 00:00:00 2001 From: Factorino73 Date: Fri, 6 Sep 2024 11:10:37 +0400 Subject: [PATCH] Fixes --- .../HelperModels/ColumnInfo.cs | 42 +++++++++++++++++++ .../NonVisualComponents/TableComponent.cs | 27 ++++-------- .../WinForms/FormNonVisualComponents.cs | 25 ++++++----- 3 files changed, 63 insertions(+), 31 deletions(-) create mode 100644 Components/Components/NonVisualComponents/HelperModels/ColumnInfo.cs diff --git a/Components/Components/NonVisualComponents/HelperModels/ColumnInfo.cs b/Components/Components/NonVisualComponents/HelperModels/ColumnInfo.cs new file mode 100644 index 0000000..9830c50 --- /dev/null +++ b/Components/Components/NonVisualComponents/HelperModels/ColumnInfo.cs @@ -0,0 +1,42 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Components.NonVisualComponents.HelperModels +{ + /// + /// Вспомогательный класс для настройки колонки Excel + /// + public class ColumnInfo + { + /// + /// Название поля объекта + /// + public string FieldName { get; set; } = string.Empty; + + /// + /// Заголовок колонки + /// + public string Header { get; set; } = string.Empty; + + /// + /// Ширина колонки + /// + public int Width { get; set; } + + /// + /// Конструктор + /// + /// + /// + /// + public ColumnInfo(string fieldName, string header, int width) + { + FieldName = fieldName; + Header = header; + Width = width; + } + } +} diff --git a/Components/Components/NonVisualComponents/TableComponent.cs b/Components/Components/NonVisualComponents/TableComponent.cs index b05e88e..0423006 100644 --- a/Components/Components/NonVisualComponents/TableComponent.cs +++ b/Components/Components/NonVisualComponents/TableComponent.cs @@ -52,8 +52,7 @@ namespace Components.NonVisualComponents /// /// public void CreateDocument(string filepath, string title, - List mergeCells, List columnsWidth, - List columns, List headers, + List mergeCells, List columns, List data) where T : class, new() { if (string.IsNullOrEmpty(filepath)) @@ -64,18 +63,10 @@ namespace Components.NonVisualComponents { throw new ArgumentNullException("Не указан заголовок документа!"); } - if (mergeCells == null || mergeCells.Count == 0 || columnsWidth == null || columnsWidth.Count == 0) + if (mergeCells == null || mergeCells.Count == 0 || columns == null || columns.Count == 0) { throw new ArgumentNullException("Не заполнена информация по колонкам!"); } - if (columns == null || columns.Count == 0 || headers == null || headers.Count == 0 || columns.Count != headers.Count) - { - throw new ArgumentNullException("Не заполнена информация по заголовкам!"); - } - if (columnsWidth.Count != columns.Count) - { - throw new Exception("Количество заголовков не соответствует количеству размеров столбцов!"); - } if (data == null) { throw new ArgumentNullException("Данные не заполнены!"); @@ -88,17 +79,17 @@ namespace Components.NonVisualComponents worksheet.Cells[1, 1] = title; // Заголовки таблицы - for (int i = 1; i <= headers.Count; i++) + for (int i = 1; i <= columns.Count; i++) { - if (string.IsNullOrEmpty(headers[i - 1])) + if (string.IsNullOrEmpty(columns[i - 1].Header)) { throw new Exception("Заголовок не имеет данных!"); } - worksheet.Cells[2, i] = headers[i - 1]; + worksheet.Cells[2, i] = columns[i - 1].Header; Excel.Range column = (Excel.Range)worksheet.Columns[i]; - column.ColumnWidth = columnsWidth[i - 1]; + column.ColumnWidth = columns[i - 1].Width; Excel.Range cell = (Excel.Range)worksheet.Cells[2, i]; cell.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter; @@ -126,7 +117,7 @@ namespace Components.NonVisualComponents } // Объединение ячеек по строкам, которые не объединяются по столбцам - for (int i = 1; i <= headers.Count; i++) + for (int i = 1; i <= columns.Count; i++) { if (!mergeIndexes.Contains(i - 1)) { @@ -157,7 +148,7 @@ namespace Components.NonVisualComponents foreach (var column in columns) { - if (column == property.Name) + if (column.FieldName == property.Name) { columnIndex = columns.IndexOf(column) + 1; break; @@ -176,7 +167,7 @@ namespace Components.NonVisualComponents // Границы таблицы for (int i = 2; i <= (data.Count() + 3); i++) { - for (int j = 1; j <= headers.Count(); j++) + for (int j = 1; j <= columns.Count(); j++) { Excel.Range cell = (Excel.Range)worksheet.Cells[i, j]; cell.BorderAround(true); diff --git a/Components/WinForms/FormNonVisualComponents.cs b/Components/WinForms/FormNonVisualComponents.cs index 9efc0ae..031f536 100644 --- a/Components/WinForms/FormNonVisualComponents.cs +++ b/Components/WinForms/FormNonVisualComponents.cs @@ -61,18 +61,18 @@ namespace WinForms new MergeCells("Личные данные", new int[] { 2, 3, 4 }), new MergeCells("Работа", new int[] { 7, 8 }) }; - List columnsWidth = new List() + List columns = new List() { - 10, 10, 20, 20, 20, 20, 20, 30, 30, 10 - }; - - List columns = new List() - { - "Id", "Status", "Name", "Surname", "Age", "Kids", "Car", "Department", "Post", "Prize" - }; - List headers = new List() - { - "Идент.", "Статус", "Имя", "Фамилия", "Возраст", "Дети", "Машина", "Подразделение", "Должность", "Премия" + new ColumnInfo("Id", "Идент.", 10), + new ColumnInfo("Status", "Статус", 10), + new ColumnInfo("Name", "Имя", 20), + new ColumnInfo("Surname", "Фамилия", 20), + new ColumnInfo("Age", "Возраст", 20), + new ColumnInfo("Kids", "Дети", 20), + new ColumnInfo("Car", "Машина", 20), + new ColumnInfo("Department", "Подразделение", 30), + new ColumnInfo("Post", "Должность", 30), + new ColumnInfo("Prize", "Премия", 10) }; List data = new List() @@ -85,8 +85,7 @@ namespace WinForms }; tableComponent1.CreateDocument(filepath, title, - mergeCells, columnsWidth, - columns, headers, + mergeCells, columns, data); }