Переход на Dictionary.

This commit is contained in:
ElEgEv 2023-10-11 00:09:17 +04:00
parent a60f4cd6ce
commit cfba8354fb
4 changed files with 76 additions and 9 deletions

View File

@ -43,6 +43,8 @@ namespace VisualComponentsForm
Car fifthCar = new("Opel", "Astra", 2100000, 2018); Car fifthCar = new("Opel", "Astra", 2100000, 2018);
Car sixthCar = new("Mercedes", "E-class", 3200500, 2019); Car sixthCar = new("Mercedes", "E-class", 3200500, 2019);
Dictionary<int, ColumnName> colData;
public FormWord() public FormWord()
{ {
InitializeComponent(); InitializeComponent();
@ -51,6 +53,14 @@ namespace VisualComponentsForm
{ {
seventhCar, firstCar, secondCar, thirdCar, fourthCar, fifthCar, sixthCar seventhCar, firstCar, secondCar, thirdCar, fourthCar, fifthCar, sixthCar
}; };
colData = new Dictionary<int, ColumnName>()
{
{ 0, new ColumnName("Марка автомобиля", "Brand") },
{ 1, new ColumnName("Модель", "Model") },
{ 2, new ColumnName("Год выпуска", "YearProduction") },
{ 3, new ColumnName("Цена", "Price") },
};
} }
private void ButtonFirstTask_Click(object sender, EventArgs e) private void ButtonFirstTask_Click(object sender, EventArgs e)
@ -90,7 +100,8 @@ namespace VisualComponentsForm
{ {
try try
{ {
setDataTable = new(dialog.FileName, "Второе задание", new List<double> { 48, 24 }, new List<double> { 2000, 2000, 1500, 1500 }, listCars); setDataTable = new(dialog.FileName, "Второе задание", new List<double> { 48, 24 },
new List<double> { 2000, 2000, 1500, 1500 }, listCars, colData);
componentBigTable.CreateDoc(setDataTable); componentBigTable.CreateDoc(setDataTable);

View File

@ -11,6 +11,7 @@ using System.Threading.Tasks;
using VisualComponentsLib.Components.SupportClasses; using VisualComponentsLib.Components.SupportClasses;
using Spire.Doc.Formatting; using Spire.Doc.Formatting;
using System.CodeDom; using System.CodeDom;
using DocumentFormat.OpenXml.Vml;
namespace VisualComponentsLib.Components namespace VisualComponentsLib.Components
{ {
@ -142,7 +143,8 @@ namespace VisualComponentsLib.Components
int indexHeaderHeigh = 0; int indexHeaderHeigh = 0;
int indexHeaderWidth = 0; int indexHeaderWidth = 0;
foreach (var val in setDataTable.DataList[0].GetType().GetProperties()) //пробегаемся по словарю, чтобы сразу заполнять данные в нужной последовательности
foreach (var item in setDataTable.ColumnsSettings)
{ {
_tr.Append(new TableRowProperties(new TableRowHeight _tr.Append(new TableRowProperties(new TableRowHeight
{ {
@ -158,9 +160,39 @@ namespace VisualComponentsLib.Components
} }
)); ));
if (string.IsNullOrEmpty(val.Name)) if (string.IsNullOrEmpty(setDataTable.ColumnsSettings[indexHeaderWidth].NameField) ||
string.IsNullOrEmpty(setDataTable.ColumnsSettings[indexHeaderWidth].NameColumn))
{ {
throw new Exception("Некорректное заполнение заголовка таблицы"); throw new Exception("Некорректное заполнение информации для шапки таблицы");
}
tc.Append(new Paragraph(new Run(new Text(item.Value.NameColumn))));
_tr.Append(tc);
indexHeaderWidth++;
}
/*foreach (var val in setDataTable.DataList[0].GetType().GetProperties())
{
_tr.Append(new TableRowProperties(new TableRowHeight
{
Val = Convert.ToUInt32(setDataTable.HeightRow[indexHeaderHeigh])
}));
var tc = new TableCell();
tc.Append(new TableCellProperties(new TableCellWidth
{
Type = TableWidthUnitValues.Dxa,
Width = setDataTable.WidthCol[indexHeaderWidth].ToString(),
}
));
if (string.IsNullOrEmpty(setDataTable.ColumnsSettings[indexHeaderWidth].NameField) ||
string.IsNullOrEmpty(setDataTable.ColumnsSettings[indexHeaderWidth].NameColumn))
{
throw new Exception("Некорректное заполнение информации для шапки таблицы");
} }
tc.Append(new Paragraph(new Run(new Text(val.Name)))); tc.Append(new Paragraph(new Run(new Text(val.Name))));
@ -168,7 +200,7 @@ namespace VisualComponentsLib.Components
_tr.Append(tc); _tr.Append(tc);
indexHeaderWidth++; indexHeaderWidth++;
} }*/
table.Append(_tr); table.Append(_tr);

View File

@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace VisualComponentsLib.Components.SupportClasses
{
//для настройки соответствия столбцов полям
public class ColumnName
{
public string NameColumn { get; set; } = string.Empty;
public string NameField { get; set; } = string.Empty;
public ColumnName(string nameColumn, string nameField)
{
NameColumn = nameColumn;
NameField = nameField;
}
}
}

View File

@ -22,16 +22,18 @@ namespace VisualComponentsLib.Components.SupportClasses
public List<T> DataList; public List<T> DataList;
//настройки соответствия столбец-поле
public Dictionary<int, ColumnName> ColumnsSettings;
public SetDataTable(string filePath, string fileHeader, List<double> heightRow, public SetDataTable(string filePath, string fileHeader, List<double> heightRow,
List<double> widthCol, List<T> dataList) List<double> widthCol, List<T> dataList, Dictionary<int, ColumnName> columnsSettings)
{ {
FilePath = filePath; FilePath = filePath;
FileHeader = fileHeader; FileHeader = fileHeader;
HeightRow = heightRow; HeightRow = heightRow;
WidthCol = widthCol; WidthCol = widthCol;
DataList = dataList; DataList = dataList;
ColumnsSettings = columnsSettings;
DataList.GroupBy(field => DataList[0]);
} }
////группировка элементов списка по первому полю ////группировка элементов списка по первому полю