diff --git a/COP/RodionovLibrary/NonVisualComponents/HelperModels/ColumnParameters.cs b/COP/RodionovLibrary/NonVisualComponents/HelperModels/ColumnParameters.cs index bcf659a..0d1278d 100644 --- a/COP/RodionovLibrary/NonVisualComponents/HelperModels/ColumnParameters.cs +++ b/COP/RodionovLibrary/NonVisualComponents/HelperModels/ColumnParameters.cs @@ -2,7 +2,9 @@ { public class ColumnParameters { - public string Header { get; set; } = string.Empty; + public string FirstRowHeader { get; set; } = string.Empty; + + public string SecondRowHeader { get; set; } = string.Empty; public string PropertyName { get; set; } = string.Empty; diff --git a/COP/RodionovLibrary/NonVisualComponents/HelperModels/WordTableInfo.cs b/COP/RodionovLibrary/NonVisualComponents/HelperModels/WordTableInfo.cs index d9e4714..643014a 100644 --- a/COP/RodionovLibrary/NonVisualComponents/HelperModels/WordTableInfo.cs +++ b/COP/RodionovLibrary/NonVisualComponents/HelperModels/WordTableInfo.cs @@ -6,9 +6,7 @@ public string Title { get; set; } = string.Empty; - public List FirstRowColumnParameters { get; set; } = new(); - - public List SecondRowColumnParameters { get; set; } = new(); + public List ColumnParameters { get; set; } = new(); public List Items { get; set; } = new(); diff --git a/COP/RodionovLibrary/NonVisualComponents/WordTableComponent.cs b/COP/RodionovLibrary/NonVisualComponents/WordTableComponent.cs index 503c037..6a8ddd7 100644 --- a/COP/RodionovLibrary/NonVisualComponents/WordTableComponent.cs +++ b/COP/RodionovLibrary/NonVisualComponents/WordTableComponent.cs @@ -50,7 +50,7 @@ namespace RodionovLibrary.NonVisualComponents Table table = new(); - double totalWidthTwips = tableInfo.FirstRowColumnParameters.Sum(column => column.Width) * 566.93; + double totalWidthTwips = tableInfo.ColumnParameters.Sum(column => column.Width) * 566.93; TableProperties tableProperties = new( new TableBorders( new TopBorder { Val = BorderValues.Single, Size = 8 }, @@ -65,12 +65,12 @@ namespace RodionovLibrary.NonVisualComponents ); table.AppendChild(tableProperties); - AddTableHeaderRow(table, tableInfo.FirstRowColumnParameters); - AddTableHeaderRow(table, tableInfo.SecondRowColumnParameters); + AddTableHeaderRow(table, tableInfo.ColumnParameters, true); + AddTableHeaderRow(table, tableInfo.ColumnParameters, false); MergeColumns(table, tableInfo.MergedColumns); - AddTableData(table, tableInfo.Items, tableInfo.SecondRowColumnParameters); + AddTableData(table, tableInfo.Items, tableInfo.ColumnParameters); body.Append(table); @@ -85,15 +85,14 @@ namespace RodionovLibrary.NonVisualComponents throw new ArgumentException("Не все данные заполнены"); } - CheckColumnParameters(tableInfo.FirstRowColumnParameters, false); - CheckColumnParameters(tableInfo.SecondRowColumnParameters, true); + CheckColumnParameters(tableInfo.ColumnParameters); if (tableInfo.Items == null || tableInfo.Items.Count == 0) { throw new ArgumentException("Данные для основной части таблицы не заданы"); } - foreach (var column in tableInfo.SecondRowColumnParameters) + foreach (var column in tableInfo.ColumnParameters) { if (typeof(T).GetProperty(column.PropertyName) == null) { @@ -101,10 +100,10 @@ namespace RodionovLibrary.NonVisualComponents } } - ValidateMergedColumns(tableInfo.MergedColumns, tableInfo.FirstRowColumnParameters.Count); + ValidateMergedColumns(tableInfo.MergedColumns, tableInfo.ColumnParameters.Count); } - private void CheckColumnParameters(List columnParameters, bool isSecondRow) + private void CheckColumnParameters(List columnParameters) { if (columnParameters == null || columnParameters.Count == 0) { @@ -113,17 +112,17 @@ namespace RodionovLibrary.NonVisualComponents foreach (var column in columnParameters) { - if (string.IsNullOrEmpty(column.Header)) + if (string.IsNullOrEmpty(column.FirstRowHeader) && string.IsNullOrEmpty(column.SecondRowHeader)) { throw new ArgumentException("Заголовок не задан для одной из колонок в таблице"); } if (column.Width <= 0) { - throw new ArgumentException($"У колонки с заголовком {column.Header} ширина должна быть больше нуля."); + throw new ArgumentException($"У колонки ширина должна быть больше нуля."); } - if (isSecondRow && string.IsNullOrEmpty(column.PropertyName)) + if (string.IsNullOrEmpty(column.PropertyName)) { - throw new ArgumentException($"Свойство не задано для заголовка: {column.Header}."); + throw new ArgumentException($"Свойство не задано для одной из колонок"); } } } @@ -153,12 +152,13 @@ namespace RodionovLibrary.NonVisualComponents } } - private void AddTableHeaderRow(Table table, List columnParameters) + private void AddTableHeaderRow(Table table, List columnParameters, bool isFirstRow) { TableRow headerRow = new(); foreach (var column in columnParameters) { - TableCell cell = new(new Paragraph(new Run(new Text(column.Header)))); + var header = isFirstRow ? column.FirstRowHeader : column.SecondRowHeader; + TableCell cell = new(new Paragraph(new Run(new Text(header)))); TableCellProperties cellProps = new() { TableCellWidth = new TableCellWidth { Type = TableWidthUnitValues.Dxa, Width = (column.Width * 566.93).ToString("F0") }, diff --git a/COP/WinForms/FormTest.cs b/COP/WinForms/FormTest.cs index 44f31a0..48235ef 100644 --- a/COP/WinForms/FormTest.cs +++ b/COP/WinForms/FormTest.cs @@ -1,5 +1,4 @@ using RodionovLibrary.NonVisualComponents.HelperModels; -using System.Windows.Forms; namespace WinForms { @@ -70,90 +69,89 @@ namespace WinForms private void ButtonWordText_Click(object sender, EventArgs e) { - try + using var dialog = new SaveFileDialog { - wordLongTextComponent.CreateWordText(new WordLongTextInfo() + Filter = "docx|*.docx" + }; + if (dialog.ShowDialog() == DialogResult.OK) + { + try { - FileName = AppDomain.CurrentDomain.BaseDirectory + "test_long_text.docx", - Title = " 1. ", - Paragraphs = new string[] { "- . - , , , . , , .", + wordLongTextComponent.CreateWordText(new WordLongTextInfo() + { + FileName = dialog.FileName, + Title = " 1. ", + Paragraphs = new string[] { "- . - , , , . , , .", " , , , , . , , , : , , , , .", " , , . - . : , , , ( ), ( ), , , , .", " , , ."} - }); - MessageBox.Show("!"); - } - catch (Exception ex) - { - MessageBox.Show(" : " + ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error); + }); + MessageBox.Show("!"); + } + catch (Exception ex) + { + MessageBox.Show(" : " + ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error); + } } } private void ButtonWordTable_Click(object sender, EventArgs e) { - try + using var dialog = new SaveFileDialog { - var firstRowColumns = new List - { - new() { Header = "", PropertyName = "", Width = 1.2 }, - new() { Header = "", PropertyName = "", Width = 1.2 }, - new() { Header = " ", PropertyName = "", Width = 1.6 }, - new() { Header = " ", PropertyName = "", Width = 1.6 }, - new() { Header = " ", PropertyName = "", Width = 1.6 }, - new() { Header = "", PropertyName = "", Width = 1.2 }, - new() { Header = "", PropertyName = "", Width = 1.6 }, - new() { Header = "", PropertyName = "", Width = 2.5 }, - new() { Header = "", PropertyName = "", Width = 2.5 }, - new() { Header = "", PropertyName = "", Width = 2 } - }; - - var secondRowColumns = new List - { - new() { Header = "", PropertyName = "Id", Width = 1.2 }, - new() { Header = "", PropertyName = "Status", Width = 1.2 }, - new() { Header = "", PropertyName = "FirstName", Width = 1.6 }, - new() { Header = "", PropertyName = "LastName", Width = 1.6 }, - new() { Header = "", PropertyName = "Age", Width = 1.6 }, - new() { Header = "", PropertyName = "Children", Width = 1.2 }, - new() { Header = "", PropertyName = "Car", Width = 1.6 }, - new() { Header = "", PropertyName = "Department", Width = 2.5 }, - new() { Header = "", PropertyName = "Position", Width = 2.5 }, - new() { Header = "", PropertyName = "Bonus", Width = 2 } - }; - - var employees = new List - { - new() { Id = 1, Status = "", FirstName = "", LastName = "", Age = 34, Children = "", Car = "", Department = " 1", Position = "", Bonus = 2000.1 }, - new() { Id = 2, Status = "", FirstName = "", LastName = "", Age = 44, Children = "", Car = "", Department = " 1", Position = "", Bonus = 2000.1 }, - new() { Id = 3, Status = "", FirstName = "", LastName = "", Age = 55, Children = "", Car = "", Department = " 1", Position = "", Bonus = 5000.5 }, - new() { Id = 4, Status = "", FirstName = "", LastName = "", Age = 34, Children = "", Car = "", Department = "", Position = "", Bonus = 2000.1 }, - new() { Id = 5, Status = "", FirstName = "", LastName = "", Age = 44, Children = "", Car = "", Department = "", Position = " ", Bonus = 7000.6 } - }; - - var mergedColumns = new List<(int, int)> - { - (2, 4), - (7, 8) - }; - - var tableInfo = new WordTableInfo - { - FileName = AppDomain.CurrentDomain.BaseDirectory + "test_table.docx", - Title = "", - FirstRowColumnParameters = firstRowColumns, - SecondRowColumnParameters = secondRowColumns, - Items = employees, - MergedColumns = mergedColumns - }; - - wordTableComponent.CreateTable(tableInfo); - - MessageBox.Show("!"); - } - catch (Exception ex) + Filter = "docx|*.docx" + }; + if (dialog.ShowDialog() == DialogResult.OK) { - MessageBox.Show(" : " + ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error); + try + { + var columns = new List + { + new() { FirstRowHeader = "", PropertyName = "Id", Width = 1.2 }, + new() { FirstRowHeader = "", PropertyName = "Status", Width = 1.2 }, + new() { FirstRowHeader = " ", SecondRowHeader = "", PropertyName = "FirstName", Width = 1.6 }, + new() { FirstRowHeader = " ", SecondRowHeader = "", PropertyName = "LastName", Width = 1.6 }, + new() { FirstRowHeader = " ", SecondRowHeader = "", PropertyName = "Age", Width = 1.6 }, + new() { FirstRowHeader = "", PropertyName = "Children", Width = 1.2 }, + new() { FirstRowHeader = "", PropertyName = "Car", Width = 1.6 }, + new() { FirstRowHeader = "", SecondRowHeader = "", PropertyName = "Department", Width = 2.5 }, + new() { FirstRowHeader = "", SecondRowHeader = "", PropertyName = "Position", Width = 2.5 }, + new() { FirstRowHeader = "", PropertyName = "Bonus", Width = 2 } + }; + + var employees = new List + { + new() { Id = 1, Status = "", FirstName = "", LastName = "", Age = 34, Children = "", Car = "", Department = " 1", Position = "", Bonus = 2000.1 }, + new() { Id = 2, Status = "", FirstName = "", LastName = "", Age = 44, Children = "", Car = "", Department = " 1", Position = "", Bonus = 2000.1 }, + new() { Id = 3, Status = "", FirstName = "", LastName = "", Age = 55, Children = "", Car = "", Department = " 1", Position = "", Bonus = 5000.5 }, + new() { Id = 4, Status = "", FirstName = "", LastName = "", Age = 34, Children = "", Car = "", Department = "", Position = "", Bonus = 2000.1 }, + new() { Id = 5, Status = "", FirstName = "", LastName = "", Age = 44, Children = "", Car = "", Department = "", Position = " ", Bonus = 7000.6 } + }; + + var mergedColumns = new List<(int, int)> + { + (2, 4), + (7, 8) + }; + + var tableInfo = new WordTableInfo + { + FileName = dialog.FileName, + Title = "", + ColumnParameters = columns, + Items = employees, + MergedColumns = mergedColumns + }; + + wordTableComponent.CreateTable(tableInfo); + + MessageBox.Show("!"); + } + catch (Exception ex) + { + MessageBox.Show(" : " + ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error); + } } }