Таблица генерится.

This commit is contained in:
ElEgEv 2023-10-09 22:54:08 +04:00
parent 056880251c
commit e79648acd7
6 changed files with 126 additions and 21 deletions

View File

@ -35,8 +35,12 @@
groupBox2 = new GroupBox(); groupBox2 = new GroupBox();
buttonThirdTask = new Button(); buttonThirdTask = new Button();
componentWordHistogram = new VisualComponentsLib.Components.ComponentWordHistogram(components); componentWordHistogram = new VisualComponentsLib.Components.ComponentWordHistogram(components);
componentBigTable = new VisualComponentsLib.Components.ComponentBigTable(components);
groupBox3 = new GroupBox();
buttonStartSecond = new Button();
groupBox1.SuspendLayout(); groupBox1.SuspendLayout();
groupBox2.SuspendLayout(); groupBox2.SuspendLayout();
groupBox3.SuspendLayout();
SuspendLayout(); SuspendLayout();
// //
// buttonFirstTask // buttonFirstTask
@ -62,7 +66,7 @@
// groupBox2 // groupBox2
// //
groupBox2.Controls.Add(buttonThirdTask); groupBox2.Controls.Add(buttonThirdTask);
groupBox2.Location = new Point(154, 12); groupBox2.Location = new Point(270, 12);
groupBox2.Name = "groupBox2"; groupBox2.Name = "groupBox2";
groupBox2.Size = new Size(129, 79); groupBox2.Size = new Size(129, 79);
groupBox2.TabIndex = 2; groupBox2.TabIndex = 2;
@ -79,17 +83,39 @@
buttonThirdTask.UseVisualStyleBackColor = true; buttonThirdTask.UseVisualStyleBackColor = true;
buttonThirdTask.Click += ButtonThirdTask_Click; buttonThirdTask.Click += ButtonThirdTask_Click;
// //
// groupBox3
//
groupBox3.Controls.Add(buttonStartSecond);
groupBox3.Location = new Point(138, 12);
groupBox3.Name = "groupBox3";
groupBox3.Size = new Size(126, 79);
groupBox3.TabIndex = 3;
groupBox3.TabStop = false;
groupBox3.Text = "Второе задание";
//
// buttonStartSecond
//
buttonStartSecond.Location = new Point(24, 32);
buttonStartSecond.Name = "buttonStartSecond";
buttonStartSecond.Size = new Size(75, 23);
buttonStartSecond.TabIndex = 0;
buttonStartSecond.Text = "Создать";
buttonStartSecond.UseVisualStyleBackColor = true;
buttonStartSecond.Click += ButtonStartSecond_Click;
//
// FormWord // FormWord
// //
AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font; AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(295, 103); ClientSize = new Size(411, 103);
Controls.Add(groupBox3);
Controls.Add(groupBox2); Controls.Add(groupBox2);
Controls.Add(groupBox1); Controls.Add(groupBox1);
Name = "FormWord"; Name = "FormWord";
Text = "Невизуальные компоненты"; Text = "Невизуальные компоненты";
groupBox1.ResumeLayout(false); groupBox1.ResumeLayout(false);
groupBox2.ResumeLayout(false); groupBox2.ResumeLayout(false);
groupBox3.ResumeLayout(false);
ResumeLayout(false); ResumeLayout(false);
} }
@ -101,5 +127,8 @@
private GroupBox groupBox2; private GroupBox groupBox2;
private Button buttonThirdTask; private Button buttonThirdTask;
private VisualComponentsLib.Components.ComponentWordHistogram componentWordHistogram; private VisualComponentsLib.Components.ComponentWordHistogram componentWordHistogram;
private VisualComponentsLib.Components.ComponentBigTable componentBigTable;
private GroupBox groupBox3;
private Button buttonStartSecond;
} }
} }

View File

@ -17,6 +17,10 @@ namespace VisualComponentsForm
{ {
SimpleTable table; SimpleTable table;
SimpleHistogram histogram;
SetDataTable<Car> setDataTable;
string[,] first = { string[,] first = {
{ "Первый", "Второй" } { "Первый", "Второй" }
}; };
@ -29,11 +33,24 @@ namespace VisualComponentsForm
{ "Пятый", "Шестой" } { "Пятый", "Шестой" }
}; };
SimpleHistogram histogram; List<Car> listCars;
Car seventhCar = new("Audi", "TT", 3500000, 2005);
Car firstCar = new ("Lada", "XRay", 1566000, 2016);
Car secondCar = new("Opel", "Astra", 1320000, 2017);
Car thirdCar = new("Lada", "Granta", 430000, 2009);
Car fourthCar = new("Lada", "Vesta", 1220000, 2022);
Car fifthCar = new("Opel", "Astra", 2100000, 2018);
Car sixthCar = new("Mercedes", "E-class", 3200500, 2019);
public FormWord() public FormWord()
{ {
InitializeComponent(); InitializeComponent();
listCars = new()
{
seventhCar, firstCar, secondCar, thirdCar, fourthCar, fifthCar, sixthCar
};
} }
private void ButtonFirstTask_Click(object sender, EventArgs e) private void ButtonFirstTask_Click(object sender, EventArgs e)
@ -61,6 +78,31 @@ namespace VisualComponentsForm
} }
} }
private void ButtonStartSecond_Click(object sender, EventArgs e)
{
//фильтрация файлов для диалогового окна
using var dialog = new SaveFileDialog
{
Filter = "docx|*.docx"
};
if (dialog.ShowDialog() == DialogResult.OK)
{
try
{
setDataTable = new(dialog.FileName, "Второе задание", new List<double> { 48, 24 }, new List<double> { 2000, 2000, 1500, 1500 }, listCars);
componentBigTable.CreateDoc(setDataTable);
MessageBox.Show("Выполнено", "Успех", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void ButtonThirdTask_Click(object sender, EventArgs e) private void ButtonThirdTask_Click(object sender, EventArgs e)
{ {
//фильтрация файлов для диалогового окна //фильтрация файлов для диалогового окна

View File

@ -123,4 +123,7 @@
<metadata name="componentWordHistogram.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"> <metadata name="componentWordHistogram.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>162, 17</value> <value>162, 17</value>
</metadata> </metadata>
<metadata name="componentBigTable.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>363, 17</value>
</metadata>
</root> </root>

View File

@ -9,6 +9,7 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using VisualComponentsLib.Components.SupportClasses; using VisualComponentsLib.Components.SupportClasses;
using Spire.Doc.Formatting;
namespace VisualComponentsLib.Components namespace VisualComponentsLib.Components
{ {
@ -51,10 +52,10 @@ namespace VisualComponentsLib.Components
//создание таблицы //создание таблицы
private void AddTable<T> (SetDataTable<T> setDataTable) private void AddTable<T> (SetDataTable<T> setDataTable)
{ {
if (!CheckData(setDataTable.DataList)) /*if (!CheckData(setDataTable.DataList))
{ {
throw new Exception("Не все ячейки заполнены"); throw new Exception("Не все ячейки заполнены");
} }*/
using (var document = WordprocessingDocument.Open(setDataTable.FilePath, true)) using (var document = WordprocessingDocument.Open(setDataTable.FilePath, true))
{ {
@ -160,20 +161,22 @@ namespace VisualComponentsLib.Components
_tr.Append(tc); _tr.Append(tc);
table.Append(_tr);
indexHeaderWidth++; indexHeaderWidth++;
} }
table.Append(_tr);
//увеличиваем счётчик на 1, т. к. в списке только два значения //увеличиваем счётчик на 1, т. к. в списке только два значения
indexHeaderHeigh++; indexHeaderHeigh++;
indexHeaderWidth = 0;
//генерация тела таблицы //генерация тела таблицы
for (var i = 1; i < setDataTable.DataList.Count; i++) for (var i = 1; i < setDataTable.DataList.Count; i++)
{ {
var tr = new TableRow(); var tr = new TableRow();
foreach (var val in setDataTable.DataList[i].GetType().GetFields()) foreach (var val in setDataTable.DataList[i].GetType().GetProperties())
{ {
tr.Append(new TableRowProperties(new TableRowHeight tr.Append(new TableRowProperties(new TableRowHeight
{ {
@ -189,12 +192,10 @@ namespace VisualComponentsLib.Components
} }
)); ));
tc.Append(new Paragraph(new Run(new Text(val.GetValue(setDataTable.DataList[i].GetType()).ToString())))); tc.Append(new Paragraph(new Run(new Text(val.GetValue(setDataTable.DataList[i]).ToString()))));
tr.Append(tc); tr.Append(tc);
table.Append(tr);
indexHeaderWidth++; indexHeaderWidth++;
} }
@ -205,6 +206,8 @@ namespace VisualComponentsLib.Components
#endregion #endregion
doc.Body.Append(header); doc.Body.Append(header);
doc.Body.Append(table); doc.Body.Append(table);
@ -218,7 +221,7 @@ namespace VisualComponentsLib.Components
{ {
foreach(var data in dataList) foreach(var data in dataList)
{ {
foreach (var value in data.GetType().GetFields()) foreach (var value in data.GetType().GetProperties())
{ {
//для простоты проверки приводим значение каждого поля к типу string //для простоты проверки приводим значение каждого поля к типу string
if(string.IsNullOrEmpty(dataList.GetType().GetField(value.Name).GetValue(data).ToString())) if(string.IsNullOrEmpty(dataList.GetType().GetField(value.Name).GetValue(data).ToString()))

View File

@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace VisualComponentsLib.Components.SupportClasses
{
public class Car
{
public string Brand { get; set; } = string.Empty;
public string Model { get; set; } = string.Empty;
public int Price { get; set; }
public int YearProduction { get; set; }
public Car(string brand, string model, int price, int yearProduction)
{
Brand = brand;
Model = model;
Price = price;
YearProduction = yearProduction;
}
}
}

View File

@ -31,15 +31,15 @@ namespace VisualComponentsLib.Components.SupportClasses
WidthCol = widthCol; WidthCol = widthCol;
DataList = dataList; DataList = dataList;
DataList = GroupValue(DataList); //DataList = DataList.GroupBy(field => field.GetType().GetProperties().First().Name).Select(field => field.ToList());
} }
//группировка элементов списка по первому полю ////группировка элементов списка по первому полю
private static List<T> GroupValue<T>(List<T> data) //private static List<T> GroupValue<T>(List<T> data)
{ //{
var mainField = data[0].GetType().GetProperties().First().Name; // var mainField = data[0].GetType().GetProperties().First().Name;
return (List<T>)data.GroupBy(field => field.GetType().GetProperties().First().Name).Select(field => field.ToList()); // return data.GroupBy(field => field.GetType().GetProperties().First().Name).Select(field => field.ToList());
} //}
} }
} }