поберегите слезы для следующих лаб

This commit is contained in:
bulatova_karina 2024-09-30 21:27:55 +04:00
parent b4a8359199
commit 34bf359bc0
2 changed files with 58 additions and 92 deletions

View File

@ -1,26 +1,14 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using DocumentFormat.OpenXml;
using Components.SupportClasses;
using Aspose.Words;
using Document = DocumentFormat.OpenXml.Wordprocessing.Document;
using Paragraph = DocumentFormat.OpenXml.Wordprocessing.Paragraph;
using Run = DocumentFormat.OpenXml.Wordprocessing.Run;
using Body = DocumentFormat.OpenXml.Wordprocessing.Body;
using DocumentFormat.OpenXml;
namespace Components.NonVisualComponents
{
public partial class WordText : Component
{
private WordprocessingDocument? _wordDocument;
private Body? _docBody;
public WordText()
{
InitializeComponent();
@ -29,104 +17,81 @@ namespace Components.NonVisualComponents
public WordText(IContainer container)
{
container.Add(this);
InitializeComponent();
}
public void CreateWordText(LargeText largeText)
{
if (string.IsNullOrEmpty(largeText.FilePath) || string.IsNullOrEmpty(largeText.DocumentTitle) || !CheckData(largeText.TextData))
{
throw new Exception("Не все данные заполнены");
}
_wordDocument = WordprocessingDocument.Create(largeText.FilePath, WordprocessingDocumentType.Document);
//вытаскиваем главную часть из вордовского документа
MainDocumentPart mainPart = _wordDocument.AddMainDocumentPart();
using (var wordDocument = WordprocessingDocument.Create(largeText.FilePath, WordprocessingDocumentType.Document))
{
// Вытаскиваем главную часть из вордовского документа
MainDocumentPart mainPart = wordDocument.AddMainDocumentPart();
mainPart.Document = new Document();
//генерируем тело основной части документа
_docBody = mainPart.Document.AppendChild(new Body());
// Генерируем тело основной части документа
Body docBody = mainPart.Document.AppendChild(new Body());
_wordDocument.Clone();
AddText(largeText);
// Добавляем текст
AddText(docBody, largeText);
}
}
private void AddText(LargeText largeText)
private void AddText(Body docBody, LargeText largeText)
{
using (var document = WordprocessingDocument.Open(largeText.FilePath, true))
{
var doc = document.MainDocumentPart.Document;
#region Создание заголовка
ParagraphProperties paragraphProperties = new();
paragraphProperties.AppendChild(new Justification
{
Val = JustificationValues.Center
});
paragraphProperties.AppendChild(new Indentation());
Paragraph header = new();
header.AppendChild(paragraphProperties);
var docRun = new Run();
var properties = new RunProperties();
properties.AppendChild(new FontSize
{
Val = "48"
});
properties.AppendChild(new Bold());
docRun.AppendChild(properties);
docRun.AppendChild(new Text(largeText.DocumentTitle));
header.AppendChild(docRun);
doc.Body.Append(header);
docBody.Append(header);
#endregion
#region Создание текста
for (int i = 0; i < largeText.TextData.Length; i++)
{
ParagraphProperties paragraphProperties2 = new();
paragraphProperties2.AppendChild(new Justification
{
Val = JustificationValues.Both
});
paragraphProperties2.AppendChild(new Indentation());
Paragraph text = new();
text.AppendChild(paragraphProperties2);
var docRun2 = new Run();
var properties2 = new RunProperties();
properties2.AppendChild(new FontSize
{
Val = "24"
});
docRun2.AppendChild(properties2);
docRun2.AppendChild(new Text(largeText.TextData[i]));
text.AppendChild(docRun2);
doc.Body.Append(text);
docBody.Append(text);
}
#endregion
doc.Save();
}
}
bool CheckData(string[] data)
@ -135,7 +100,6 @@ namespace Components.NonVisualComponents
{
if (string.IsNullOrEmpty(data[i])) return false;
}
return true;
}
}

View File

@ -48,6 +48,7 @@ namespace WinForms
}
}
private void button2_Click(object sender, EventArgs e)
{
List<int[]> mergedColumns = new()
@ -59,10 +60,10 @@ namespace WinForms
List<ColumnDefinition> columnDefinitions = new List<ColumnDefinition>
{
new ColumnDefinition { Header = "Идентификатор", PropertyName = "Id", Weight = 35 },
new ColumnDefinition { Header = "Пол", PropertyName = "Gender", Weight = 35 },
new ColumnDefinition { Header = "Имя", PropertyName = "Name", Weight = 10 },
new ColumnDefinition { Header = "Фамилия", PropertyName = "LastName", Weight = 20 },
new ColumnDefinition { Header = "Работа", PropertyName = "Work", Weight = 35 },
new ColumnDefinition { Header = "", PropertyName = "Education1", Weight = 35 },
new ColumnDefinition { Header = "", PropertyName = "Education2", Weight = 10 },
new ColumnDefinition { Header = "Фамилия", PropertyName = "LastName", Weight = 20 }
};
@ -70,7 +71,8 @@ namespace WinForms
{
new ColumnDefinition { Header = "Возраст", PropertyName = "Age", Weight = 20 },
new ColumnDefinition { Header = "Опыт", PropertyName = "Experience", Weight = 20 },
new ColumnDefinition { Header = "Должность", PropertyName = "Position", Weight = 20 }
new ColumnDefinition { Header = "Должность", PropertyName = "Position", Weight = 20 },
new ColumnDefinition { Header = "Фамилия", PropertyName = "LastName", Weight = 20 }
};
List<Worker> data = new List<Worker>