тупое 2 задание
This commit is contained in:
parent
e15e9c2088
commit
8accba9c6f
16
COPWinForms/CellMergeInfo.cs
Normal file
16
COPWinForms/CellMergeInfo.cs
Normal 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; }
|
||||
}
|
||||
}
|
@ -8,6 +8,7 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Aspose.Words.Tables;
|
||||
using static COPWinForms.ComponentWord2;
|
||||
|
||||
namespace COPWinForms
|
||||
{
|
||||
@ -63,37 +64,49 @@ namespace COPWinForms
|
||||
builder.InsertCell();
|
||||
builder.Write(columnDefinition.Header);
|
||||
}
|
||||
builder.EndRow();
|
||||
|
||||
// Вставка второй строки шапки таблицы
|
||||
foreach (var columnDefinition in tableWord.ColumnDefinitions)
|
||||
|
||||
// Объединение ячеек в шапке таблицы
|
||||
foreach (var mergedColumn in tableWord.MergedColumns)
|
||||
{
|
||||
builder.InsertCell();
|
||||
builder.Write(columnDefinition.Header);
|
||||
int startCellIndex = mergedColumn[0];
|
||||
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();
|
||||
|
||||
// Вставка данных в таблицу
|
||||
foreach (var item in tableWord.Data)
|
||||
{
|
||||
|
||||
foreach (var columnDefinition in tableWord.ColumnDefinitions)
|
||||
foreach (var columnDefinition2 in tableWord.ColumnDefinitions2)
|
||||
{
|
||||
builder.InsertCell();
|
||||
// Получение значения свойства/поля объекта по заданному имени
|
||||
var propertyValue = item.GetType()
|
||||
.GetProperty(columnDefinition.PropertyName)?
|
||||
.GetValue(item)?.ToString();
|
||||
.GetProperty(columnDefinition2.PropertyName)?
|
||||
.GetValue(item)?.ToString();
|
||||
|
||||
// Вставка значения в ячейку
|
||||
builder.Write(propertyValue ?? "");
|
||||
|
||||
}
|
||||
|
||||
builder.EndRow();
|
||||
}
|
||||
|
||||
builder.EndTable();
|
||||
builder.EndTable();
|
||||
|
||||
// Сохранение документа в файл
|
||||
document.Save(tableWord.FilePath);
|
||||
@ -104,6 +117,14 @@ namespace COPWinForms
|
||||
public string Header { get; set; }
|
||||
public string PropertyName { get; set; }
|
||||
}
|
||||
|
||||
public class ColumnDefinition2
|
||||
{
|
||||
public string Header { get; set; }
|
||||
public string PropertyName { get; set; }
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -14,16 +14,19 @@ namespace COPWinForms
|
||||
public string DocumentTitle = string.Empty;
|
||||
|
||||
public List<ColumnDefinition> ColumnDefinitions;
|
||||
public List<ColumnDefinition2> ColumnDefinitions2;
|
||||
|
||||
public List<object> Data;
|
||||
|
||||
|
||||
public TableWord(string filePath, string documentTitle, List<ColumnDefinition> columnDefinitions, List<object> data)
|
||||
public List<int[]> MergedColumns;
|
||||
public TableWord(string filePath, string documentTitle, List<ColumnDefinition> columnDefinitions, List<ColumnDefinition2> columnDefinitions2, List<object> data, List<int[]> mergedColumns)
|
||||
{
|
||||
FilePath = filePath;
|
||||
DocumentTitle = documentTitle;
|
||||
ColumnDefinitions = columnDefinitions;
|
||||
Data = data;
|
||||
MergedColumns = mergedColumns;
|
||||
ColumnDefinitions2 = columnDefinitions2;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -159,14 +159,24 @@ namespace WinFormsTest
|
||||
|
||||
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>
|
||||
{
|
||||
new ColumnDefinition { Header = "FIO", PropertyName = "FIO" },
|
||||
new ColumnDefinition { Header = "Group", PropertyName = "Group" },
|
||||
new ColumnDefinition { Header = "Ëè÷íûå äàííûå", PropertyName = "PersonalData" },
|
||||
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>
|
||||
{
|
||||
new Student { FIO = "John", Group = "ff", Email = "New York" },
|
||||
@ -183,7 +193,7 @@ namespace WinFormsTest
|
||||
{
|
||||
try
|
||||
{
|
||||
TableWord tableWord = new(dialog.FileName, "Çàäàíèå 2", columnDefinitions, data);
|
||||
TableWord tableWord = new(dialog.FileName, "Çàäàíèå 2", columnDefinitions, columnDefinitions2, data, mergedColumns);
|
||||
componentWord21.CreateTable(tableWord);
|
||||
MessageBox.Show("Ôàéë ñîçäàí", "Ðåçóëüòàò", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user