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

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

View File

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