лаба2 сдано

This commit is contained in:
ityurner02@mail.ru 2023-10-14 10:57:08 +04:00
parent bb355ba8d4
commit ea707ec754
7 changed files with 286 additions and 3 deletions

View File

@ -0,0 +1,25 @@
namespace VisualCompLib.Components.SupportClasses
{
public class BigTable<T>
{
public string FilePath = string.Empty;
public string DocumentTitle = string.Empty;
public List<ColumnDefinition> ColumnDefinitions;
public List<ColumnDefinition> ColumnDefinitions2;
public List<T> Data;
public List<int[]> MergedColumns;
public BigTable(string filePath, string documentTitle, List<ColumnDefinition> columnDefinitions, List<ColumnDefinition> columnDefinitions2, List<T> data, List<int[]> mergedColumns)
{
FilePath = filePath;
DocumentTitle = documentTitle;
ColumnDefinitions = columnDefinitions;
Data = data;
MergedColumns = mergedColumns;
ColumnDefinitions2 = columnDefinitions2;
}
}
}

View File

@ -0,0 +1,9 @@
namespace VisualCompLib.Components.SupportClasses
{
public class ColumnDefinition
{
public string Header;
public string PropertyName;
public double Weight;
}
}

View File

@ -0,0 +1,36 @@
namespace VisualCompLib.Components
{
partial class WordTable
{
/// <summary>
/// Обязательная переменная конструктора.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Освободить все используемые ресурсы.
/// </summary>
/// <param name="disposing">истинно, если управляемый ресурс должен быть удален; иначе ложно.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Код, автоматически созданный конструктором компонентов
/// <summary>
/// Требуемый метод для поддержки конструктора — не изменяйте
/// содержимое этого метода с помощью редактора кода.
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
}
#endregion
}
}

View File

@ -0,0 +1,128 @@
using System.ComponentModel;
using VisualCompLib.Components.SupportClasses;
using Aspose.Words;
using Aspose.Words.Tables;
namespace VisualCompLib.Components
{
public partial class WordTable : Component
{
public WordTable()
{
InitializeComponent();
}
public WordTable(IContainer container)
{
container.Add(this);
InitializeComponent();
}
public void CreateTable<T>(BigTable<T> bigTable)
{
if (bigTable.Data == null)
{
throw new ArgumentException("Не заданы все данные");
}
foreach (var columnDefinition in bigTable.ColumnDefinitions)
{
if (string.IsNullOrEmpty(columnDefinition.PropertyName))
{
throw new ArgumentException($"Не задано свойство столбца: {columnDefinition.Header}");
}
}
Document document = new Document();
DocumentBuilder builder = new DocumentBuilder(document);
Style titleStyle = builder.Document.Styles.Add(StyleType.Paragraph, "Title");
titleStyle.Font.Size = 16;
titleStyle.Font.Bold = true;
builder.ParagraphFormat.Style = titleStyle;
builder.Writeln(bigTable.DocumentTitle);
Table table = builder.StartTable();
foreach (var columnDefinition in bigTable.ColumnDefinitions)
{
builder.InsertCell();
builder.CellFormat.PreferredWidth = PreferredWidth.FromPoints(columnDefinition.Weight);
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
builder.Write(columnDefinition.Header);
}
foreach (var mergedColumn in bigTable.MergedColumns)
{
int startCellIndex = mergedColumn[0];
int endCellIndex = mergedColumn[mergedColumn.Length - 1];
for (int i = startCellIndex; i <= endCellIndex; i++)
{
table.Rows[0].Cells[i].CellFormat.HorizontalMerge = CellMerge.First;
table.Rows[0].Cells[i].CellFormat.VerticalMerge = CellMerge.First;
}
for (int i = startCellIndex + 1; i <= endCellIndex; i++)
{
table.Rows[0].Cells[i].CellFormat.HorizontalMerge = CellMerge.Previous;
table.Rows[0].Cells[i].CellFormat.VerticalMerge = CellMerge.First;
}
}
builder.EndRow();
foreach (var columnDefinition2 in bigTable.ColumnDefinitions2)
{
builder.InsertCell();
builder.CellFormat.PreferredWidth = PreferredWidth.FromPoints(columnDefinition2.Weight);
builder.Write(columnDefinition2.Header);
}
builder.EndRow();
int columnIndex;
foreach (var columnDefinition in bigTable.ColumnDefinitions)
{
string currentPropertyName = columnDefinition.PropertyName;
columnIndex = 0;
foreach (var columnDefinition2 in bigTable.ColumnDefinitions2)
{
string currentPropertyName1 = columnDefinition2.PropertyName;
if (currentPropertyName == currentPropertyName1)
{
table.Rows[0].Cells[columnIndex].CellFormat.VerticalMerge = CellMerge.First;
table.Rows[1].Cells[columnIndex].CellFormat.VerticalMerge = CellMerge.Previous;
}
columnIndex++;
}
}
foreach (var item in bigTable.Data)
{
foreach (var columnDefinition2 in bigTable.ColumnDefinitions2)
{
builder.InsertCell();
var propertyValue = item.GetType()
.GetProperty(columnDefinition2.PropertyName)?
.GetValue(item)?.ToString();
builder.Write(propertyValue ?? "");
}
builder.EndRow();
}
builder.EndTable();
document.Save(bigTable.FilePath);
}
}
}

View File

@ -35,8 +35,12 @@
wordLineChart = new VisualCompLib.Components.WordLineChart(components);
groupBox3 = new GroupBox();
button3 = new Button();
groupBox2 = new GroupBox();
button2 = new Button();
wordTable = new VisualCompLib.Components.WordTable(components);
groupBox1.SuspendLayout();
groupBox3.SuspendLayout();
groupBox2.SuspendLayout();
SuspendLayout();
//
// groupBox1
@ -62,7 +66,7 @@
// groupBox3
//
groupBox3.Controls.Add(button3);
groupBox3.Location = new Point(158, 12);
groupBox3.Location = new Point(331, 12);
groupBox3.Name = "groupBox3";
groupBox3.Size = new Size(120, 80);
groupBox3.TabIndex = 1;
@ -79,17 +83,39 @@
button3.UseVisualStyleBackColor = true;
button3.Click += button3_Click;
//
// groupBox2
//
groupBox2.Controls.Add(button2);
groupBox2.Location = new Point(171, 12);
groupBox2.Name = "groupBox2";
groupBox2.Size = new Size(120, 80);
groupBox2.TabIndex = 1;
groupBox2.TabStop = false;
groupBox2.Text = "Таблица";
//
// button2
//
button2.Location = new Point(6, 45);
button2.Name = "button2";
button2.Size = new Size(94, 29);
button2.TabIndex = 0;
button2.Text = "Создать";
button2.UseVisualStyleBackColor = true;
button2.Click += button2_Click;
//
// FormWord
//
AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(320, 165);
ClientSize = new Size(463, 114);
Controls.Add(groupBox2);
Controls.Add(groupBox3);
Controls.Add(groupBox1);
Name = "FormWord";
Text = "Невизуальные компоненты";
groupBox1.ResumeLayout(false);
groupBox3.ResumeLayout(false);
groupBox2.ResumeLayout(false);
ResumeLayout(false);
}
@ -101,5 +127,8 @@
private VisualCompLib.Components.WordLineChart wordLineChart;
private GroupBox groupBox3;
private Button button3;
private GroupBox groupBox2;
private Button button2;
private VisualCompLib.Components.WordTable wordTable;
}
}

View File

@ -1,6 +1,8 @@
using System.Diagnostics.Metrics;
using DocumentFormat.OpenXml.Wordprocessing;
using VisualCompLib.Components;
using VisualCompLib.Components.SupportClasses;
using VisualCompLib.Components.SupportClasses.Enums;
using VisualCompLib.Object;
namespace WinForms
{
@ -35,6 +37,57 @@ namespace WinForms
}
}
private void button2_Click(object sender, EventArgs e)
{
List<int[]> mergedColumns = new()
{
new int[] { 0, 1, 2 }
};
List<ColumnDefinition> columnDefinitions = new List<ColumnDefinition>
{
new ColumnDefinition { Header = "Образование", PropertyName = "Eduction", Weight = 35 },
new ColumnDefinition { Header = "", PropertyName = "Education1", Weight = 35 },
new ColumnDefinition { Header = "", PropertyName = "Education2", Weight = 10 },
new ColumnDefinition { Header = "Фамилия", PropertyName = "Name", Weight = 20 }
};
List<ColumnDefinition> columnDefinitions2 = new List<ColumnDefinition>
{
new ColumnDefinition { Header = "Группа", PropertyName = "Group", Weight = 35 },
new ColumnDefinition { Header = "Факультатив", PropertyName = "Faculty", Weight = 35 },
new ColumnDefinition { Header = "Курс", PropertyName = "Course", Weight = 10 },
new ColumnDefinition { Header = "Фамилия", PropertyName = "Name", Weight = 20 }
};
List<Student> data = new List<Student>
{
new Student { Group = "ПИбд-32", Faculty = "ФИСТ", Course = 3, Name = "Васильев" },
new Student { Group = "РТбд-11", Faculty = "РТФ", Course = 1, Name = "Иванов" },
new Student { Group = "ЛМККбд-41", Faculty = "ГФ", Course = 4, Name = "Смирнова" }
};
using var dialog = new SaveFileDialog
{
Filter = "docx|*.docx"
};
if (dialog.ShowDialog() == DialogResult.OK)
{
try
{
BigTable<Student> bigTable = new(dialog.FileName, "Задание 2", columnDefinitions, columnDefinitions2, data, mergedColumns);
wordTable.CreateTable(bigTable);
MessageBox.Show("Выполнено", "Успех", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void button3_Click(object sender, EventArgs e)
{
//фильтрация файлов для диалогового окна

View File

@ -123,4 +123,7 @@
<metadata name="wordLineChart.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>134, 17</value>
</metadata>
<metadata name="wordTable.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>286, 17</value>
</metadata>
</root>