diff --git a/Diner/DineryBusinessLogic/DineryBusinessLogic.csproj b/Diner/DineryBusinessLogic/DineryBusinessLogic.csproj index 6c5b7ff..6b910ea 100644 --- a/Diner/DineryBusinessLogic/DineryBusinessLogic.csproj +++ b/Diner/DineryBusinessLogic/DineryBusinessLogic.csproj @@ -7,6 +7,7 @@ + diff --git a/Diner/DineryBusinessLogic/OfficePackage/AbstractSaveToPdf.cs b/Diner/DineryBusinessLogic/OfficePackage/AbstractSaveToPdf.cs new file mode 100644 index 0000000..a5ff7c4 --- /dev/null +++ b/Diner/DineryBusinessLogic/OfficePackage/AbstractSaveToPdf.cs @@ -0,0 +1,65 @@ +using DineryBusinessLogic.OfficePackage.HelperEnums; +using DineryBusinessLogic.OfficePackage.HelperModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection.Emit; +using System.Text; +using System.Threading.Tasks; + +namespace DineryBusinessLogic.OfficePackage +{ + public abstract class AbstractSaveToPdf + { + public void CreateDoc(PdfInfo info) { + CreatePdf(info); + CreateParagraph(new PdfParagraph + { + Text = info.Title, + Style = "NormalTitle", + ParagraphAlignment = PdfParagraphAlignmentType.Center + }); + CreateParagraph(new PdfParagraph + { + Text = $"c {info.DateFrom.ToShortDateString()} по {info.DateTo.ToShortDateString()}", + Style = "Normal", + ParagraphAlignment = PdfParagraphAlignmentType.Right + }); + CreateTable(new List { "2cm", "3cm", "6cm", "3cm" }); + CreateRow(new PdfRowParameters + { + Text = new List { "Номер", "Дата заказа", "Изделие", "Сумма" }, + Style = "NormalTittle", + ParagraphAlignment = PdfParagraphAlignmentType.Center + }); + + foreach (var order in info.Orders) { + CreateRow(new PdfRowParameters + { + Text = new List { order.ID.ToString(), order.DateCreate.ToShortTimeString(), + order.ProductName, order.Sum.ToString() }, + Style = "Normal", + ParagraphAlignment = PdfParagraphAlignmentType.Left + }); + } + CreateParagraph(new PdfParagraph + { + Text = $"Итого: {info.Orders.Sum(x => x.Sum)}\t", + Style = "Normal", + ParagraphAlignment = PdfParagraphAlignmentType.Right + }); + + SavePdf(info); + } + // Создание doc-файла + protected abstract void CreatePdf(PdfInfo info); + // Создание параграфа с текстом + protected abstract void CreateParagraph(PdfParagraph paragraph); + // Создание таблицы + protected abstract void CreateTable(List columns); + // Создание и заполнение строки + protected abstract void CreateRow(PdfRowParameters rowParameters); + // Сохранение файла + protected abstract void SavePdf(PdfInfo info); + } +} diff --git a/Diner/DineryBusinessLogic/OfficePackage/Implements/SaveToExcel.cs b/Diner/DineryBusinessLogic/OfficePackage/Implements/SaveToExcel.cs new file mode 100644 index 0000000..9673131 --- /dev/null +++ b/Diner/DineryBusinessLogic/OfficePackage/Implements/SaveToExcel.cs @@ -0,0 +1,131 @@ +using DineryBusinessLogic.OfficePackage.HelperModels; +using DocumentFormat.OpenXml.Packaging; +using DocumentFormat.OpenXml.Spreadsheet; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DineryBusinessLogic.OfficePackage.Implements +{ + internal class SaveToExcel : AbstractSaveToExcel + { + private SpreadsheetDocument? _spreadsheetDocument; + private SharedStringTablePart? _shareStringPart; + private Worksheet? worksheet; + + private static void CreateStyles(WorksheetPart worksheetpart) { + var sp = worksheetpart.AddNewPart(); + sp.Stylesheet = new Stylesheet(); + var fonts = new Fonts() { Count = 2U, KnownFonts = true }; + var fontUsual = new Font(); + fontUsual.Append(new FontSize() { Val = 12D }); + fontUsual.Append(new DocumentFormat.OpenXml.Office2010.Excel.Color() { Theme = 1U }); + fontUsual.Append(new FontName() { Val = "Times New Roman" }); + fontUsual.Append(new FontFamilyNumbering() { Val = 2 }); + fontUsual.Append(new FontScheme() { Val = FontSchemeValues.Minor }); + var fontTittle = new Font(); + fontTittle.Append(new Bold()); + fontTittle.Append(new FontSize() { Val = 14D }); + fontTittle.Append(new DocumentFormat.OpenXml.Office2010.Excel.Color() { Theme = 1U }); + fontTittle.Append(new FontName() { Val = "Times New Roman" }); + fontTittle.Append(new FontFamilyNumbering() { Val = 2 }); + fontTittle.Append(new FontScheme() { Val = FontSchemeValues.Minor }); + fonts.Append(fontUsual); + fonts.Append(fontTittle); + var fills = new Fills() { Count = 2U }; + var fill1 = new Fill(); + fill1.Append(new PatternFill() { PatternType = PatternValues.None }); + var fill2 = new Fill(); + fill2.Append(new PatternFill() { PatternType = PatternValues.Gray125 }); + fills.Append(fill1); + fills.Append(fill2); + var borders = new Borders() { Count = 2U }; + var borderNoBorder = new Border(); + borderNoBorder.Append(new LeftBorder()); + borderNoBorder.Append(new RightBorder()); + borderNoBorder.Append(new TopBorder()); + borderNoBorder.Append(new BottomBorder()); + borderNoBorder.Append(new DiagonalBorder()); + var borderThin = new Border(); + var leftBorder = new LeftBorder() { Style = BorderStyleValues.Thin }; + leftBorder.Append(new DocumentFormat.OpenXml.Office2010.Excel.Color() { Indexed = 64U }); + var rightBorder = new RightBorder() { Style = BorderStyleValues.Thin }; + rightBorder.Append(new DocumentFormat.OpenXml.Office2010.Excel.Color() { Indexed = 64U }); + var topBorder = new TopBorder() { Style = BorderStyleValues.Thin }; + topBorder.Append(new DocumentFormat.OpenXml.Office2010.Excel.Color() { Indexed = 64U }); + var bottomBorder = new BottomBorder() { Style = BorderStyleValues.Thin }; + bottomBorder = new BottomBorder() { Style = BorderStyleValues.Thin }; + bottomBorder.Append(new DocumentFormat.OpenXml.Office2010.Excel.Color() { Indexed = 64U }); + borderThin.Append(leftBorder); + borderThin.Append(rightBorder); + borderThin.Append(topBorder); + borderThin.Append(bottomBorder); + borderThin.Append(new DiagonalBorder()); + borders.Append(borderNoBorder); + borders.Append(borderThin); + var cellStyleFormats = new CellStyleFormats() { Count = 1U }; + var cellFormatStyle = new CellFormat() { NumberFormatId = 0U, FontId = 0U, FillId = 0U, BorderId = 0U }; + cellStyleFormats.Append(cellFormatStyle); + var cellFormat = new CellFormats() { Count = 3U }; + var cellFormatFont = new CellFormat() + { + NumberFormatId = 0U, + FontId = 0U, + FillId = 0U, + BorderId = 0U, + FormatId = 0U, + ApplyFont = true + }; + var cellFormatFontAndBorder = new CellFormat() + { + NumberFormatId = 0U, + FontId = 0U, + FillId = 0U, + BorderId = 1U, + FormatId = 0U, + ApplyFont = true, + ApplyBorder = true + }; + var cellFormatTitle = new CellFormat() + { + NumberFormatId = 0U, + FontId = 0U, + FillId = 1U, + BorderId = 0U, + FormatId = 0U, + Alignment = new Alignment() + { + Vertical = VerticalAlignmentValues.Center, + WrapText = true, + Horizontal = HorizontalAlignmentValues.Center + }, + ApplyFont = true, + }; + cellFormat.Append(cellFormatFont); + cellFormat.Append(cellFormatFontAndBorder); + cellFormat.Append(cellFormatTitle); + } + + protected override void CreateExcel(ExcelInfo info) + { + throw new NotImplementedException(); + } + + protected override void InsertCellInWorksheet(ExcelCellParameters excelParameters) + { + throw new NotImplementedException(); + } + + protected override void MergeCells(ExcelMergeParameters mergeParams) + { + throw new NotImplementedException(); + } + + protected override void SaveExcel(ExcelInfo info) + { + throw new NotImplementedException(); + } + } +} diff --git a/Diner/DineryBusinessLogic/OfficePackage/Implements/SaveToWord.cs b/Diner/DineryBusinessLogic/OfficePackage/Implements/SaveToWord.cs new file mode 100644 index 0000000..2c0b897 --- /dev/null +++ b/Diner/DineryBusinessLogic/OfficePackage/Implements/SaveToWord.cs @@ -0,0 +1,108 @@ +using DineryBusinessLogic.OfficePackage.HelperEnums; +using DineryBusinessLogic.OfficePackage.HelperModels; +using DocumentFormat.OpenXml; +using DocumentFormat.OpenXml.ExtendedProperties; +using DocumentFormat.OpenXml.Packaging; +using DocumentFormat.OpenXml.Wordprocessing; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DineryBusinessLogic.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(WordInfo 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(WordInfo info) + { + if (_docBody == null || _wordDocument == null) { + return; + } + _docBody.AppendChild(CreateSectionProperties()); + _wordDocument.MainDocumentPart!.Document.Save(); + _wordDocument.Clone(); + } + } +}