тупое 2 задание

This commit is contained in:
Екатерина Рогашова 2023-10-13 20:00:14 +04:00
parent e15e9c2088
commit 8accba9c6f
4 changed files with 69 additions and 19 deletions

View File

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace COPWinForms
{
public class CellMergeInfo
{
public int StartColumnIndex { get; set; }
public int EndColumnIndex { get; set; }
public int RowIndex { get; set; }
public string PropertyName { get; set; }
}
}

View File

@ -8,6 +8,7 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Aspose.Words.Tables; using Aspose.Words.Tables;
using static COPWinForms.ComponentWord2;
namespace COPWinForms namespace COPWinForms
{ {
@ -63,37 +64,49 @@ namespace COPWinForms
builder.InsertCell(); builder.InsertCell();
builder.Write(columnDefinition.Header); builder.Write(columnDefinition.Header);
} }
builder.EndRow();
// Вставка второй строки шапки таблицы // Объединение ячеек в шапке таблицы
foreach (var columnDefinition in tableWord.ColumnDefinitions) foreach (var mergedColumn in tableWord.MergedColumns)
{ {
builder.InsertCell(); int startCellIndex = mergedColumn[0];
builder.Write(columnDefinition.Header); int endCellIndex = mergedColumn[mergedColumn.Length - 1];
for (int i = startCellIndex; i <= endCellIndex; i++)
{
table.FirstRow.Cells[i].CellFormat.HorizontalMerge = CellMerge.First;
}
for (int i = startCellIndex + 1; i <= endCellIndex; i++)
{
builder.CellFormat.HorizontalMerge = CellMerge.Previous;
}
}
builder.EndRow();
foreach (var columnDefinition2 in tableWord.ColumnDefinitions2)
{
builder.InsertCell();
builder.Write(columnDefinition2.Header);
} }
builder.EndRow(); builder.EndRow();
// Вставка данных в таблицу // Вставка данных в таблицу
foreach (var item in tableWord.Data) foreach (var item in tableWord.Data)
{ {
foreach (var columnDefinition2 in tableWord.ColumnDefinitions2)
foreach (var columnDefinition in tableWord.ColumnDefinitions)
{ {
builder.InsertCell(); builder.InsertCell();
// Получение значения свойства/поля объекта по заданному имени // Получение значения свойства/поля объекта по заданному имени
var propertyValue = item.GetType() var propertyValue = item.GetType()
.GetProperty(columnDefinition.PropertyName)? .GetProperty(columnDefinition2.PropertyName)?
.GetValue(item)?.ToString(); .GetValue(item)?.ToString();
// Вставка значения в ячейку // Вставка значения в ячейку
builder.Write(propertyValue ?? ""); builder.Write(propertyValue ?? "");
} }
builder.EndRow(); builder.EndRow();
} }
builder.EndTable(); builder.EndTable();
// Сохранение документа в файл // Сохранение документа в файл
document.Save(tableWord.FilePath); document.Save(tableWord.FilePath);
@ -104,6 +117,14 @@ namespace COPWinForms
public string Header { get; set; } public string Header { get; set; }
public string PropertyName { get; set; } public string PropertyName { get; set; }
} }
public class ColumnDefinition2
{
public string Header { get; set; }
public string PropertyName { get; set; }
}
} }
} }

View File

@ -14,16 +14,19 @@ namespace COPWinForms
public string DocumentTitle = string.Empty; public string DocumentTitle = string.Empty;
public List<ColumnDefinition> ColumnDefinitions; public List<ColumnDefinition> ColumnDefinitions;
public List<ColumnDefinition2> ColumnDefinitions2;
public List<object> Data; public List<object> Data;
public List<int[]> MergedColumns;
public TableWord(string filePath, string documentTitle, List<ColumnDefinition> columnDefinitions, List<object> data) public TableWord(string filePath, string documentTitle, List<ColumnDefinition> columnDefinitions, List<ColumnDefinition2> columnDefinitions2, List<object> data, List<int[]> mergedColumns)
{ {
FilePath = filePath; FilePath = filePath;
DocumentTitle = documentTitle; DocumentTitle = documentTitle;
ColumnDefinitions = columnDefinitions; ColumnDefinitions = columnDefinitions;
Data = data; Data = data;
MergedColumns = mergedColumns;
ColumnDefinitions2 = columnDefinitions2;
} }
} }
} }

View File

@ -159,14 +159,24 @@ namespace WinFormsTest
private void button3_Click(object sender, EventArgs e) private void button3_Click(object sender, EventArgs e)
{ {
ComponentWord2 tableComponent = new ComponentWord2(); List<int[]> mergedColumns = new()
{
new int[] { 0, 1 } // Ïðèìåð îáúåäèíåíèÿ ñòîëáöîâ 0 è 1
};
List<ColumnDefinition> columnDefinitions = new List<ColumnDefinition> List<ColumnDefinition> columnDefinitions = new List<ColumnDefinition>
{ {
new ColumnDefinition { Header = "FIO", PropertyName = "FIO" }, new ColumnDefinition { Header = "Ëè÷íûå äàííûå", PropertyName = "PersonalData" },
new ColumnDefinition { Header = "Group", PropertyName = "Group" },
new ColumnDefinition { Header = "Email", PropertyName = "Email" } new ColumnDefinition { Header = "Email", PropertyName = "Email" }
}; };
List<ColumnDefinition2> columnDefinitions2 = new List<ColumnDefinition2>
{
new ColumnDefinition2 { Header = "FIO", PropertyName = "FIO" },
new ColumnDefinition2 { Header = "Group", PropertyName = "Group" },
new ColumnDefinition2 { Header = "Email", PropertyName = "Email" }
};
List<object> data = new List<object> List<object> data = new List<object>
{ {
new Student { FIO = "John", Group = "ff", Email = "New York" }, new Student { FIO = "John", Group = "ff", Email = "New York" },
@ -183,7 +193,7 @@ namespace WinFormsTest
{ {
try try
{ {
TableWord tableWord = new(dialog.FileName, "Çàäàíèå 2", columnDefinitions, data); TableWord tableWord = new(dialog.FileName, "Çàäàíèå 2", columnDefinitions, columnDefinitions2, data, mergedColumns);
componentWord21.CreateTable(tableWord); componentWord21.CreateTable(tableWord);
MessageBox.Show("Ôàéë ñîçäàí", "Ðåçóëüòàò", MessageBoxButtons.OK, MessageBoxIcon.Information); MessageBox.Show("Ôàéë ñîçäàí", "Ðåçóëüòàò", MessageBoxButtons.OK, MessageBoxIcon.Information);
} }