2 компонент ( ˶°ㅁ°)
This commit is contained in:
parent
3feb33895e
commit
9725f07d76
@ -0,0 +1,11 @@
|
||||
namespace RodionovLibrary.NonVisualComponents.HelperModels
|
||||
{
|
||||
public class ColumnParameters
|
||||
{
|
||||
public string Header { get; set; } = string.Empty;
|
||||
|
||||
public string PropertyName { get; set; } = string.Empty;
|
||||
|
||||
public double Width { get; set; }
|
||||
}
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
namespace RodionovLibrary.NonVisualComponents.HelperModels
|
||||
{
|
||||
public class WordTableInfo<T>
|
||||
{
|
||||
public string FileName { get; set; } = string.Empty;
|
||||
|
||||
public string Title { get; set; } = string.Empty;
|
||||
|
||||
public List<ColumnParameters> FirstRowColumnParameters { get; set; } = new();
|
||||
|
||||
public List<ColumnParameters> SecondRowColumnParameters { get; set; } = new();
|
||||
|
||||
public List<T> Items { get; set; } = new();
|
||||
|
||||
public List<(int, int)> MergedColumns { get; set; } = new();
|
||||
}
|
||||
}
|
@ -28,7 +28,7 @@ namespace RodionovLibrary.NonVisualComponents
|
||||
{
|
||||
if (string.IsNullOrEmpty(textInfo.FileName) || string.IsNullOrEmpty(textInfo.Title) || !CheckData(textInfo.Paragraphs))
|
||||
{
|
||||
throw new Exception("Не все данные заполнены");
|
||||
throw new ArgumentException("Не все данные заполнены");
|
||||
}
|
||||
|
||||
_wordDocument = WordprocessingDocument.Create(textInfo.FileName, WordprocessingDocumentType.Document);
|
||||
|
@ -1,4 +1,8 @@
|
||||
using System.ComponentModel;
|
||||
using DocumentFormat.OpenXml.Packaging;
|
||||
using DocumentFormat.OpenXml.Wordprocessing;
|
||||
using DocumentFormat.OpenXml;
|
||||
using RodionovLibrary.NonVisualComponents.HelperModels;
|
||||
using System.ComponentModel;
|
||||
|
||||
namespace RodionovLibrary.NonVisualComponents
|
||||
{
|
||||
@ -15,5 +19,214 @@ namespace RodionovLibrary.NonVisualComponents
|
||||
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
public void CreateTable<T>(WordTableInfo<T> tableInfo)
|
||||
{
|
||||
ValidateInputData(tableInfo);
|
||||
|
||||
using (WordprocessingDocument wordDoc = WordprocessingDocument.Create(tableInfo.FileName, WordprocessingDocumentType.Document))
|
||||
{
|
||||
MainDocumentPart mainPart = wordDoc.AddMainDocumentPart();
|
||||
mainPart.Document = new Document();
|
||||
Body body = mainPart.Document.AppendChild(new Body());
|
||||
|
||||
ParagraphProperties paragraphProperties = new();
|
||||
paragraphProperties.AppendChild(new Justification { Val = JustificationValues.Center });
|
||||
|
||||
Paragraph titleParagraph = new();
|
||||
titleParagraph.AppendChild(paragraphProperties);
|
||||
|
||||
Run docRun = new();
|
||||
|
||||
RunProperties runProperties = new();
|
||||
runProperties.AppendChild(new FontSize { Val = "48" });
|
||||
runProperties.AppendChild(new Bold());
|
||||
|
||||
docRun.AppendChild(runProperties);
|
||||
docRun.AppendChild(new Text(tableInfo.Title));
|
||||
|
||||
titleParagraph.AppendChild(docRun);
|
||||
body.Append(titleParagraph);
|
||||
|
||||
Table table = new();
|
||||
|
||||
double totalWidthTwips = tableInfo.FirstRowColumnParameters.Sum(column => column.Width) * 566.93;
|
||||
TableProperties tableProperties = new(
|
||||
new TableBorders(
|
||||
new TopBorder { Val = BorderValues.Single, Size = 8 },
|
||||
new BottomBorder { Val = BorderValues.Single, Size = 8 },
|
||||
new LeftBorder { Val = BorderValues.Single, Size = 8 },
|
||||
new RightBorder { Val = BorderValues.Single, Size = 8 },
|
||||
new InsideHorizontalBorder { Val = BorderValues.Single, Size = 8 },
|
||||
new InsideVerticalBorder { Val = BorderValues.Single, Size = 8 }
|
||||
),
|
||||
new TableWidth { Width = totalWidthTwips.ToString("F0"), Type = TableWidthUnitValues.Dxa },
|
||||
new TableJustification { Val = TableRowAlignmentValues.Center }
|
||||
);
|
||||
table.AppendChild(tableProperties);
|
||||
|
||||
AddTableHeaderRow(table, tableInfo.FirstRowColumnParameters);
|
||||
AddTableHeaderRow(table, tableInfo.SecondRowColumnParameters);
|
||||
|
||||
MergeColumns(table, tableInfo.MergedColumns);
|
||||
|
||||
AddTableData(table, tableInfo.Items, tableInfo.SecondRowColumnParameters);
|
||||
|
||||
body.Append(table);
|
||||
|
||||
mainPart.Document.Save();
|
||||
}
|
||||
}
|
||||
|
||||
private void ValidateInputData<T>(WordTableInfo<T> tableInfo)
|
||||
{
|
||||
if (string.IsNullOrEmpty(tableInfo.FileName) || string.IsNullOrEmpty(tableInfo.Title))
|
||||
{
|
||||
throw new ArgumentException("Не все данные заполнены");
|
||||
}
|
||||
|
||||
CheckColumnParameters(tableInfo.FirstRowColumnParameters, false);
|
||||
CheckColumnParameters(tableInfo.SecondRowColumnParameters, true);
|
||||
|
||||
if (tableInfo.Items == null || tableInfo.Items.Count == 0)
|
||||
{
|
||||
throw new ArgumentException("Данные для основной части таблицы не заданы");
|
||||
}
|
||||
|
||||
foreach (var column in tableInfo.SecondRowColumnParameters)
|
||||
{
|
||||
if (typeof(T).GetProperty(column.PropertyName) == null)
|
||||
{
|
||||
throw new ArgumentException($"Свойство '{column.PropertyName}' не найдено в классе {typeof(T).Name}.");
|
||||
}
|
||||
}
|
||||
|
||||
ValidateMergedColumns(tableInfo.MergedColumns, tableInfo.FirstRowColumnParameters.Count);
|
||||
}
|
||||
|
||||
private void CheckColumnParameters(List<ColumnParameters> columnParameters, bool isSecondRow)
|
||||
{
|
||||
if (columnParameters == null || columnParameters.Count == 0)
|
||||
{
|
||||
throw new ArgumentException("Не заданы колонки для таблицы");
|
||||
}
|
||||
|
||||
foreach (var column in columnParameters)
|
||||
{
|
||||
if (string.IsNullOrEmpty(column.Header))
|
||||
{
|
||||
throw new ArgumentException("Заголовок не задан для одной из колонок в таблице");
|
||||
}
|
||||
if (column.Width <= 0)
|
||||
{
|
||||
throw new ArgumentException($"У колонки с заголовком {column.Header} ширина должна быть больше нуля.");
|
||||
}
|
||||
if (isSecondRow && string.IsNullOrEmpty(column.PropertyName))
|
||||
{
|
||||
throw new ArgumentException($"Свойство не задано для заголовка: {column.Header}.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void ValidateMergedColumns(List<(int start, int end)> mergedColumns, int totalColumns)
|
||||
{
|
||||
if (mergedColumns == null || mergedColumns.Count == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var (start, end) in mergedColumns)
|
||||
{
|
||||
if (start < 0 || end >= totalColumns)
|
||||
{
|
||||
throw new ArgumentException("Индексы объединенных колонок выходят за пределы допустимых значений.");
|
||||
}
|
||||
|
||||
var overlappingRanges = mergedColumns
|
||||
.Where(m => m != (start, end))
|
||||
.Any(m => m.start <= end && m.end >= start);
|
||||
|
||||
if (overlappingRanges)
|
||||
{
|
||||
throw new ArgumentException("Объединенные ячейки пересекаются.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void AddTableHeaderRow(Table table, List<ColumnParameters> columnParameters)
|
||||
{
|
||||
TableRow headerRow = new();
|
||||
foreach (var column in columnParameters)
|
||||
{
|
||||
TableCell cell = new(new Paragraph(new Run(new Text(column.Header == "MergeCell" ? "" : column.Header))));
|
||||
TableCellProperties cellProps = new()
|
||||
{
|
||||
TableCellWidth = new TableCellWidth { Type = TableWidthUnitValues.Dxa, Width = (column.Width * 566.93).ToString("F0") },
|
||||
TableCellVerticalAlignment = new TableCellVerticalAlignment { Val = TableVerticalAlignmentValues.Center }
|
||||
};
|
||||
|
||||
ParagraphProperties paragraphProperties = new(new Justification { Val = JustificationValues.Center });
|
||||
RunProperties runProperties = new() { Bold = new Bold() };
|
||||
|
||||
cell.Append(cellProps);
|
||||
cell.Append(paragraphProperties);
|
||||
cell.Elements<Paragraph>().First().Elements<Run>().First().RunProperties = runProperties;
|
||||
|
||||
headerRow.Append(cell);
|
||||
}
|
||||
table.Append(headerRow);
|
||||
}
|
||||
|
||||
private void MergeColumns(Table table, List<(int start, int end)> mergedColumns)
|
||||
{
|
||||
foreach (var (startCellIndex, endCellIndex) in mergedColumns)
|
||||
{
|
||||
for (int i = startCellIndex; i <= endCellIndex; i++)
|
||||
{
|
||||
TableCell cell = table.Elements<TableRow>().ElementAt(0).Elements<TableCell>().ElementAt(i);
|
||||
cell.TableCellProperties.Append(new HorizontalMerge { Val = i == startCellIndex ? MergedCellValues.Restart : MergedCellValues.Continue });
|
||||
}
|
||||
}
|
||||
|
||||
int totalColumns = table.Elements<TableRow>().ElementAt(0).Elements<TableCell>().Count();
|
||||
for (int i = 0; i < totalColumns; i++)
|
||||
{
|
||||
bool isColumnMergedHorizontally = mergedColumns.Any(m => m.start <= i && m.end >= i);
|
||||
|
||||
if (!isColumnMergedHorizontally)
|
||||
{
|
||||
TableCell firstRowCell = table.Elements<TableRow>().ElementAt(0).Elements<TableCell>().ElementAt(i);
|
||||
TableCell secondRowCell = table.Elements<TableRow>().ElementAt(1).Elements<TableCell>().ElementAt(i);
|
||||
|
||||
firstRowCell.TableCellProperties.Append(new VerticalMerge { Val = MergedCellValues.Restart });
|
||||
secondRowCell.TableCellProperties.Append(new VerticalMerge { Val = MergedCellValues.Continue });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void AddTableData<T>(Table table, List<T> items, List<ColumnParameters> columnParameters)
|
||||
{
|
||||
foreach (var item in items)
|
||||
{
|
||||
TableRow dataRow = new();
|
||||
foreach (var column in columnParameters)
|
||||
{
|
||||
TableCell cell = new(new Paragraph(new Run(new Text(item?.GetType().GetProperty(column.PropertyName)?.GetValue(item)?.ToString() ?? string.Empty))));
|
||||
|
||||
TableCellProperties cellProps = new()
|
||||
{
|
||||
TableCellVerticalAlignment = new TableCellVerticalAlignment { Val = TableVerticalAlignmentValues.Center }
|
||||
};
|
||||
|
||||
ParagraphProperties paragraphProperties = new(new Justification { Val = JustificationValues.Center });
|
||||
|
||||
cell.Append(cellProps);
|
||||
cell.Append(paragraphProperties);
|
||||
|
||||
dataRow.Append(cell);
|
||||
}
|
||||
table.Append(dataRow);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
25
COP/WinForms/Employee.cs
Normal file
25
COP/WinForms/Employee.cs
Normal file
@ -0,0 +1,25 @@
|
||||
namespace WinForms
|
||||
{
|
||||
public class Employee
|
||||
{
|
||||
public int Id { get; set; }
|
||||
|
||||
public string Status { get; set; } = string.Empty;
|
||||
|
||||
public string FirstName { get; set; } = string.Empty;
|
||||
|
||||
public string LastName { get; set; } = string.Empty;
|
||||
|
||||
public int Age { get; set; }
|
||||
|
||||
public string Children { get; set; } = string.Empty;
|
||||
|
||||
public string Car { get; set; } = string.Empty;
|
||||
|
||||
public string Department { get; set; } = string.Empty;
|
||||
|
||||
public string Position { get; set; } = string.Empty;
|
||||
|
||||
public double Bonus { get; set; }
|
||||
}
|
||||
}
|
2
COP/WinForms/FormTest.Designer.cs
generated
2
COP/WinForms/FormTest.Designer.cs
generated
@ -46,6 +46,7 @@
|
||||
buttonWordTable = new Button();
|
||||
buttonWordText = new Button();
|
||||
wordLongTextComponent = new RodionovLibrary.NonVisualComponents.WordLongTextComponent(components);
|
||||
wordTableComponent = new RodionovLibrary.NonVisualComponents.WordTableComponent(components);
|
||||
panel1.SuspendLayout();
|
||||
SuspendLayout();
|
||||
//
|
||||
@ -269,5 +270,6 @@
|
||||
private Button buttonWordTable;
|
||||
private Button buttonWordText;
|
||||
private RodionovLibrary.NonVisualComponents.WordLongTextComponent wordLongTextComponent;
|
||||
private RodionovLibrary.NonVisualComponents.WordTableComponent wordTableComponent;
|
||||
}
|
||||
}
|
||||
|
@ -74,7 +74,7 @@ namespace WinForms
|
||||
{
|
||||
wordLongTextComponent.CreateWordText(new WordLongTextInfo()
|
||||
{
|
||||
FileName = AppDomain.CurrentDomain.BaseDirectory + "test.docx",
|
||||
FileName = AppDomain.CurrentDomain.BaseDirectory + "test_long_text.docx",
|
||||
Title = "ГЛАВА 1. Нежданные гости",
|
||||
Paragraphs = new string[] { "Жил-был в норе под землей хоббит. Не в какой-то там мерзкой грязной сырой норе, где со всех сторон торчат хвосты червей и противно пахнет плесенью, но и не в сухой песчаной голой норе, где не на что сесть и нечего съесть. Нет, нора была хоббичья, а значит — благоустроенная.",
|
||||
"Она начиналась идеально круглой, как иллюминатор, дверью, выкрашенной зеленой краской, с сияющей медной ручкой точно посередине. Дверь отворялась внутрь, в длинный коридор, похожий на железнодорожный туннель, но туннель без гари и без дыма и тоже очень благоустроенный: стены там были обшиты панелями, пол выложен плитками и устлан ковром, вдоль стен стояли полированные стулья, и всюду были прибиты крючочки для шляп и пальто, так как хоббит любил гостей.",
|
||||
@ -92,12 +92,82 @@ namespace WinForms
|
||||
|
||||
private void ButtonWordTable_Click(object sender, EventArgs e)
|
||||
{
|
||||
try
|
||||
{
|
||||
var firstRowColumns = new List<ColumnParameters>
|
||||
{
|
||||
new() { Header = "Èäåíò", PropertyName = "", Width = 1.2 },
|
||||
new() { Header = "Ñòàòóñ", PropertyName = "", Width = 1.2 },
|
||||
new() { Header = "Ëè÷íûå äàííûå", PropertyName = "", Width = 1.6 },
|
||||
new() { Header = "MergeCell", PropertyName = "", Width = 1.6 },
|
||||
new() { Header = "MergeCell", PropertyName = "", Width = 1.6 },
|
||||
new() { Header = "Äåòè", PropertyName = "", Width = 1.2 },
|
||||
new() { Header = "Ìàøèíà", PropertyName = "", Width = 1.6 },
|
||||
new() { Header = "Ðàáîòà", PropertyName = "", Width = 2.5 },
|
||||
new() { Header = "MergeCell", PropertyName = "", Width = 2.5 },
|
||||
new() { Header = "Ïðåìèÿ", PropertyName = "", Width = 2 }
|
||||
};
|
||||
|
||||
var secondRowColumns = new List<ColumnParameters>
|
||||
{
|
||||
new() { Header = "Èäåíò", PropertyName = "Id", Width = 1.2 },
|
||||
new() { Header = "Ñòàòóñ", PropertyName = "Status", Width = 1.2 },
|
||||
new() { Header = "Èìÿ", PropertyName = "FirstName", Width = 1.6 },
|
||||
new() { Header = "Ôàìèëèÿ", PropertyName = "LastName", Width = 1.6 },
|
||||
new() { Header = "Âîçðàñò", PropertyName = "Age", Width = 1.6 },
|
||||
new() { Header = "Äåòè", PropertyName = "Children", Width = 1.2 },
|
||||
new() { Header = "Ìàøèíà", PropertyName = "Car", Width = 1.6 },
|
||||
new() { Header = "Ïîäðàçäåëåíèå", PropertyName = "Department", Width = 2.5 },
|
||||
new() { Header = "Äîëæíîñòü", PropertyName = "Position", Width = 2.5 },
|
||||
new() { Header = "Ïðåìèÿ", PropertyName = "Bonus", Width = 2 }
|
||||
};
|
||||
|
||||
var employees = new List<Employee>
|
||||
{
|
||||
new() { Id = 1, Status = "íåò", FirstName = "Èâàí", LastName = "Èâàíîâ", Age = 34, Children = "íåò", Car = "åñòü", Department = "Äåïàðòàìåíò 1", Position = "Èíæåíåð", Bonus = 2000.1 },
|
||||
new() { Id = 2, Status = "íåò", FirstName = "Ïåòð", LastName = "Ïåòðîâ", Age = 44, Children = "åñòü", Car = "åñòü", Department = "Äåïàðòàìåíò 1", Position = "Èíæåíåð", Bonus = 2000.1 },
|
||||
new() { Id = 3, Status = "äà", FirstName = "Ñåðãåé", LastName = "Ñåðãååâ", Age = 55, Children = "íåò", Car = "íåò", Department = "Äåïàðòàìåíò 1", Position = "Ðóêîâîäèòåëü", Bonus = 5000.5 },
|
||||
new() { Id = 4, Status = "íåò", FirstName = "Îëüãà", LastName = "Èâàíîâà", Age = 34, Children = "åñòü", Car = "íåò", Department = "Áóõãàëòåðèÿ", Position = "Áóõãàëòåð", Bonus = 2000.1 },
|
||||
new() { Id = 5, Status = "äà", FirstName = "Òàòüÿíà", LastName = "Ïåòðîâà", Age = 44, Children = "íåò", Car = "íåò", Department = "Áóõãàëòåðèÿ", Position = "Ñòàðøèé áóõãàëòåð", Bonus = 7000.6 }
|
||||
};
|
||||
|
||||
var mergedColumns = new List<(int, int)>
|
||||
{
|
||||
(2, 4),
|
||||
(7, 8)
|
||||
};
|
||||
|
||||
var tableInfo = new WordTableInfo<Employee>
|
||||
{
|
||||
FileName = AppDomain.CurrentDomain.BaseDirectory + "test_table.docx",
|
||||
Title = "Çàãîëîâîê",
|
||||
FirstRowColumnParameters = firstRowColumns,
|
||||
SecondRowColumnParameters = secondRowColumns,
|
||||
Items = employees,
|
||||
MergedColumns = mergedColumns
|
||||
};
|
||||
|
||||
wordTableComponent.CreateTable(tableInfo);
|
||||
|
||||
MessageBox.Show("Ãîòîâî!");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show("Ïðîèçîøëà îøèáêà: " + ex.Message, "Îøèáêà", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
}
|
||||
}
|
||||
|
||||
private void ButtonWordDiagram_Click(object sender, EventArgs e)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
MessageBox.Show("Ãîòîâî!");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show("Ïðîèçîøëà îøèáêà: " + ex.Message, "Îøèáêà", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -120,4 +120,7 @@
|
||||
<metadata name="wordLongTextComponent.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
|
||||
<value>17, 17</value>
|
||||
</metadata>
|
||||
<metadata name="wordTableComponent.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
|
||||
<value>210, 17</value>
|
||||
</metadata>
|
||||
</root>
|
Loading…
Reference in New Issue
Block a user