diff --git a/ComputerShopProvider/ComputerShopBusinessLogic/BusinessLogics/ReportLogic.cs b/ComputerShopProvider/ComputerShopBusinessLogic/BusinessLogics/ReportLogic.cs index 6639d3b..c3cd0d7 100644 --- a/ComputerShopProvider/ComputerShopBusinessLogic/BusinessLogics/ReportLogic.cs +++ b/ComputerShopProvider/ComputerShopBusinessLogic/BusinessLogics/ReportLogic.cs @@ -83,6 +83,7 @@ namespace ComputerShopBusinessLogic.BusinessLogics public void SaveReceivingComponentsToWordFile(ReportBindingModel model) { + if (model.Ids != null) _saveToWord.CreateDoc(new WordInfoProvider { FileName = model.FileName, diff --git a/ComputerShopProvider/ComputerShopBusinessLogic/ComputerShopBusinessLogic.csproj b/ComputerShopProvider/ComputerShopBusinessLogic/ComputerShopBusinessLogic.csproj index 9983faa..0d03bb4 100644 --- a/ComputerShopProvider/ComputerShopBusinessLogic/ComputerShopBusinessLogic.csproj +++ b/ComputerShopProvider/ComputerShopBusinessLogic/ComputerShopBusinessLogic.csproj @@ -7,6 +7,7 @@ + diff --git a/ComputerShopProvider/ComputerShopBusinessLogic/OfficePackage/AbstractSaveToWord.cs b/ComputerShopProvider/ComputerShopBusinessLogic/OfficePackage/AbstractSaveToWord.cs index 58e1792..8e0b585 100644 --- a/ComputerShopProvider/ComputerShopBusinessLogic/OfficePackage/AbstractSaveToWord.cs +++ b/ComputerShopProvider/ComputerShopBusinessLogic/OfficePackage/AbstractSaveToWord.cs @@ -8,13 +8,13 @@ using System.Threading.Tasks; namespace ComputerShopBusinessLogic.OfficePackage { - public class AbstractSaveToWord + public abstract class AbstractSaveToWord { public void CreateDoc(WordInfoProvider info) { - /* - + + CreateWord(info); CreateParagraph(new WordParagraph @@ -27,12 +27,12 @@ namespace ComputerShopBusinessLogic.OfficePackage } }); - foreach (var mc in info.MemberConferences) + foreach (var cr in info.ComponentReceivings) { CreateParagraph(new WordParagraph { Texts = new List<(string, WordTextProperties)> - { (mc.MemberFIO, new WordTextProperties { Size = "20", Bold=true})}, + { (cr.ComponentName, new WordTextProperties { Size = "20", Bold=true})}, TextProperties = new WordTextProperties { Size = "24", @@ -40,13 +40,11 @@ namespace ComputerShopBusinessLogic.OfficePackage } }); - foreach (var conference in mc.Conferences) + foreach (var receiving in cr.Receivings) { CreateParagraph(new WordParagraph { - Texts = new List<(string, WordTextProperties)> - { (conference.Item1 + " - ", new WordTextProperties { Size = "16", Bold=false}), - (conference.Item2.ToShortDateString(), new WordTextProperties { Size = "16", Bold=false})}, + Texts = new List<(string, WordTextProperties)> { (receiving, new WordTextProperties { Size = "16", Bold = false }) }, TextProperties = new WordTextProperties { Size = "24", @@ -55,17 +53,13 @@ namespace ComputerShopBusinessLogic.OfficePackage }); } } - SaveWord(info); + SaveWord(info); - */ + } - /* - - protected abstract void CreateWord(WordInfoOrganiser info); + protected abstract void CreateWord(WordInfoProvider info); protected abstract void CreateParagraph(WordParagraph paragraph); - protected abstract void SaveWord(WordInfoOrganiser info); - - */ + protected abstract void SaveWord(WordInfoProvider info); } } diff --git a/ComputerShopProvider/ComputerShopBusinessLogic/OfficePackage/HelperEnums/WordTextProperties.cs b/ComputerShopProvider/ComputerShopBusinessLogic/OfficePackage/HelperEnums/WordTextProperties.cs new file mode 100644 index 0000000..f9dda16 --- /dev/null +++ b/ComputerShopProvider/ComputerShopBusinessLogic/OfficePackage/HelperEnums/WordTextProperties.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputerShopBusinessLogic.OfficePackage.HelperEnums +{ + public class WordTextProperties + { + public string Size { get; set; } = string.Empty; + public bool Bold { get; set; } + public WordJustificationType JustificationType { get; set; } + } +} diff --git a/ComputerShopProvider/ComputerShopBusinessLogic/OfficePackage/HelperModels/WordParagraph.cs b/ComputerShopProvider/ComputerShopBusinessLogic/OfficePackage/HelperModels/WordParagraph.cs new file mode 100644 index 0000000..2797e58 --- /dev/null +++ b/ComputerShopProvider/ComputerShopBusinessLogic/OfficePackage/HelperModels/WordParagraph.cs @@ -0,0 +1,15 @@ +using ComputerShopBusinessLogic.OfficePackage.HelperEnums; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputerShopBusinessLogic.OfficePackage.HelperModels +{ + public class WordParagraph + { + public List<(string, WordTextProperties)> Texts { get; set; } = new(); + public WordTextProperties? TextProperties { get; set; } + } +} diff --git a/ComputerShopProvider/ComputerShopBusinessLogic/OfficePackage/Implements/SaveToWord.cs b/ComputerShopProvider/ComputerShopBusinessLogic/OfficePackage/Implements/SaveToWord.cs index dca87b5..ab35647 100644 --- a/ComputerShopProvider/ComputerShopBusinessLogic/OfficePackage/Implements/SaveToWord.cs +++ b/ComputerShopProvider/ComputerShopBusinessLogic/OfficePackage/Implements/SaveToWord.cs @@ -1,4 +1,9 @@ -using System; +using ComputerShopBusinessLogic.OfficePackage.HelperEnums; +using ComputerShopBusinessLogic.OfficePackage.HelperModels; +using DocumentFormat.OpenXml; +using DocumentFormat.OpenXml.Packaging; +using DocumentFormat.OpenXml.Wordprocessing; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -8,6 +13,116 @@ namespace ComputerShopBusinessLogic.OfficePackage.Implements { public class SaveToWord : AbstractSaveToWord { + private WordprocessingDocument? _wordDocument; + private Body? _docBody; + private static JustificationValues GetJustificationValues(WordJustificationType type) + { + return type switch + { + WordJustificationType.Both => JustificationValues.Both, + WordJustificationType.Center => JustificationValues.Center, + _ => JustificationValues.Left, + }; + } + private static SectionProperties CreateSectionProperties() + { + var properties = new SectionProperties(); + var pageSize = new PageSize + { + Orient = PageOrientationValues.Portrait + }; + properties.AppendChild(pageSize); + return properties; + } + + private static ParagraphProperties? CreateParagraphProperties(WordTextProperties? paragraphProperties) + { + if (paragraphProperties == null) + { + return null; + } + + var properties = new ParagraphProperties(); + + properties.AppendChild(new Justification() + { + Val = GetJustificationValues(paragraphProperties.JustificationType) + }); + + properties.AppendChild(new SpacingBetweenLines + { + LineRule = LineSpacingRuleValues.Auto + }); + + properties.AppendChild(new Indentation()); + var paragraphMarkRunProperties = new ParagraphMarkRunProperties(); + + if (!string.IsNullOrEmpty(paragraphProperties.Size)) + { + paragraphMarkRunProperties.AppendChild(new FontSize + { + Val = paragraphProperties.Size + }); + } + + properties.AppendChild(paragraphMarkRunProperties); + + return properties; + } + protected override void CreateParagraph(WordParagraph paragraph) + { + if (_docBody == null || paragraph == null) + { + return; + } + + var docParagraph = new Paragraph(); + + docParagraph.AppendChild(CreateParagraphProperties(paragraph.TextProperties)); + + foreach (var run in paragraph.Texts) + { + var docRun = new Run(); + var properties = new RunProperties(); + properties.AppendChild(new FontSize { Val = run.Item2.Size }); + + if (run.Item2.Bold) + { + properties.AppendChild(new Bold()); + } + + docRun.AppendChild(properties); + + docRun.AppendChild(new Text + { + Text = run.Item1, + Space = SpaceProcessingModeValues.Preserve + }); + + docParagraph.AppendChild(docRun); + } + _docBody.AppendChild(docParagraph); + } + + protected override void CreateWord(WordInfoProvider info) + { + _wordDocument = WordprocessingDocument.Create(info.FileName, WordprocessingDocumentType.Document); + MainDocumentPart mainPart = _wordDocument.AddMainDocumentPart(); + mainPart.Document = new Document(); + _docBody = mainPart.Document.AppendChild(new Body()); + } + + protected override void SaveWord(WordInfoProvider info) + { + if (_docBody == null || _wordDocument == null) + { + return; + } + + _docBody.AppendChild(CreateSectionProperties()); + _wordDocument.MainDocumentPart!.Document.Save(); + _wordDocument.Close(); + } } } diff --git a/ComputerShopProvider/ComputerShopDatabaseImplement/ComputerShopDatabase.cs b/ComputerShopProvider/ComputerShopDatabaseImplement/ComputerShopDatabase.cs index 5d393aa..3448938 100644 --- a/ComputerShopProvider/ComputerShopDatabaseImplement/ComputerShopDatabase.cs +++ b/ComputerShopProvider/ComputerShopDatabaseImplement/ComputerShopDatabase.cs @@ -15,7 +15,7 @@ namespace ComputerShopDatabaseImplement { if (optionsBuilder.IsConfigured == false) { - optionsBuilder.UseSqlServer(@"Data Source=DESKTOP-QA8P9OJ;Initial Catalog=ComputerShopDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + optionsBuilder.UseSqlServer(@"Data Source=DESKTOP-IRUVF5S\SQLEXPRESS;Initial Catalog=ComputerShopDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); } base.OnConfiguring(optionsBuilder); }