Заполнение Implements и создание AbstractSaveToExcel, AbstractSaveToPdf, AbstractSaveToWord, подключение пакетов

This commit is contained in:
FLARJ 2023-04-08 12:05:39 +04:00
parent 2354a0e16f
commit f3703bfff3
12 changed files with 444 additions and 16 deletions

View File

@ -8,9 +8,9 @@ namespace FurnitureContracts.ViewModel
{
public class ReportSalesSalonViewModel
{
public string SalesSalpnTitle { get; set; } = string.Empty;
public string SalesSalonTitle { get; set; } = string.Empty;
public string OrderTitle { get; set; } = string.Empty;
public DateTime Date { get; set; }
public DateTime DateCreateOrder { get; set; }
public string HeadsetTitle { get; set; } = string.Empty;
}
}

View File

@ -0,0 +1,89 @@
using FurnitureFactoryBusinessLogic.OfficePackage.HelperEnums;
using FurnitureFactoryBusinessLogic.OfficePackage.HelperModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FurnitureFactoryBusinessLogic.OfficePackage
{
public abstract class AbstractSaveToExcelManager
{
public void CreateReport(ExcelInfoManager info)
{
CreateExcel(info);
InsertCellInWorksheet(new ExcelCellParameters
{
ColumnName = "A",
RowIndex = 1,
Text = info.Title,
StyleInfo = ExcelStyleInfoType.Title
});
MergeCells(new ExcelMergeParameters
{
CellFromName = "A1",
CellToName = "C1"
});
uint rowIndex = 2;
foreach (var pc in info.SalesSalonsFurniture)
{
InsertCellInWorksheet(new ExcelCellParameters
{
ColumnName = "A",
RowIndex = rowIndex,
Text = pc.Name,
StyleInfo = ExcelStyleInfoType.Text
});
rowIndex++;
foreach (var furniture in pc.Furniture)
{
InsertCellInWorksheet(new ExcelCellParameters
{
ColumnName = "B",
RowIndex = rowIndex,
Text = furniture,
StyleInfo = ExcelStyleInfoType.TextWithBroder
});
rowIndex++;
}
rowIndex++;
}
SaveExcel(info);
}
/// <summary>
/// Создание excel-файла
/// </summary>
/// <param name="info"></param>
protected abstract void CreateExcel(ExcelInfoManager info);
/// <summary>
/// Добавляем новую ячейку в лист
/// </summary>
/// <param name="cellParameters"></param>
protected abstract void InsertCellInWorksheet(ExcelCellParameters excelParams);
/// <summary>
/// Объединение ячеек
/// </summary>
/// <param name="mergeParameters"></param>
protected abstract void MergeCells(ExcelMergeParameters excelParams);
/// <summary>
/// Сохранение файла
/// </summary>
/// <param name="info"></param>
protected abstract void SaveExcel(ExcelInfoManager info);
}
}

View File

@ -4,24 +4,23 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FurnitureFactoryBusinessLogic.OfficePackage.HelperEnums;
using FurnitureFactoryBusinessLogic.OfficePackage.HelperModels;
namespace FurnitureFactoryBusinessLogic.OfficePackage.Implements
{
internal class AbstractSaveToPdfManager
public abstract class AbstractSaveToPdfManager
{
public void CreateDoc(PdfInfoManager info)
{
CreatePdf(info);
CreateParagraph(new PdfParagraph { Text = info.Title, Style = "NormalTitle", ParagraphAlignment = PdfParagraphAlignmentType.Center });
//CreateParagraph(new PdfParagraph { Text = $"с {info.DateFrom.ToShortDateString()} по {info.DateTo.ToShortDateString()}", Style = "Normal", ParagraphAlignment = PdfParagraphAlignmentType.Center });
CreateParagraph(new PdfParagraph { Text = $"с {info.DateFrom.ToShortDateString()} по {info.DateTo.ToShortDateString()}", Style = "Normal", ParagraphAlignment = PdfParagraphAlignmentType.Center });
CreateTable(new List<string> { "2cm", "3cm", "6cm", "3cm", "3cm", "6cm", "6cm" });
CreateRow(new PdfRowParameters
{
Texts = new List<string> { "Интерес", "Изделие", "Дата", "Поделка", "Дата" },
Texts = new List<string> { "Салоны продаж", "Заказы", "Дата", "Гарнитур" },
Style = "NormalTitle",
ParagraphAlignment = PdfParagraphAlignmentType.Center
});
@ -30,7 +29,7 @@ namespace FurnitureFactoryBusinessLogic.OfficePackage.Implements
{
CreateRow(new PdfRowParameters
{
Texts = new List<string> { interest.InterestTitle, interest.ProductTitle, interest.DateCreateProduct.ToShortDateString(), interest.DiyTitle, interest.DateCreateDiy.ToShortDateString() },
Texts = new List<string> { interest.SalesSalonTitle, interest.OrderTitle, interest.DateCreateOrder.ToShortDateString(), interest.HeadsetTitle },
Style = "Normal",
ParagraphAlignment = PdfParagraphAlignmentType.Left
});
@ -44,7 +43,7 @@ namespace FurnitureFactoryBusinessLogic.OfficePackage.Implements
/// Создание doc-файла
/// </summary>
/// <param name="info"></param>
protected abstract void CreatePdf(PdfInfoStudent info);
protected abstract void CreatePdf(PdfInfoManager info);
/// <summary>
/// Создание параграфа с текстом
@ -70,6 +69,6 @@ namespace FurnitureFactoryBusinessLogic.OfficePackage.Implements
/// Сохранение файла
/// </summary>
/// <param name="info"></param>
protected abstract void SavePdf(PdfInfoStudent info);
protected abstract void SavePdf(PdfInfoManager info);
}
}

View File

@ -0,0 +1,76 @@
using FurnitureFactoryBusinessLogic.OfficePackage.HelperEnums;
using FurnitureFactoryBusinessLogic.OfficePackage.HelperModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FurnitureFactoryBusinessLogic.OfficePackage
{
public abstract class AbstractSaveToWordManager
{
public void CreateDoc(WordInfoManager info)
{
CreateWord(info);
CreateParagraph(new WordParagraph
{
Texts = new List<(string, WordTextProperties)> { (info.Title, new WordTextProperties { Bold = true, Size = "24", }) },
TextProperties = new WordTextProperties
{
Size = "24",
JustificationType = WordJustificationType.Center
}
});
foreach (var interest in info.SalesSalonsFurniture)
{
CreateParagraph(new WordParagraph
{
Texts = new List<(string, WordTextProperties)> { (interest.Name, new WordTextProperties { Size = "24", Bold = true, }) },
TextProperties = new WordTextProperties
{
Size = "24",
JustificationType = WordJustificationType.Both
}
});
foreach (var lesson in interest.Furniture)
{
CreateParagraph(new WordParagraph
{
Texts = new List<(string, WordTextProperties)> { (lesson, new WordTextProperties { Size = "20", Bold = false, }) },
TextProperties = new WordTextProperties
{
Size = "24",
JustificationType = WordJustificationType.Both
}
});
}
}
SaveWord(info);
}
/// <summary>
/// Создание doc-файла
/// </summary>
/// <param name="info"></param>
protected abstract void CreateWord(WordInfoManager info);
/// <summary>
/// Создание абзаца с текстом
/// </summary>
/// <param name="paragraph"></param>
/// <returns></returns>
protected abstract void CreateParagraph(WordParagraph paragraph);
/// <summary>
/// Сохранение файла
/// </summary>
/// <param name="info"></param>
protected abstract void SaveWord(WordInfoManager info);
}
}

View File

@ -8,7 +8,7 @@ using FurnitureFactoryBusinessLogic.OfficePackage.HelperEnums;
namespace FurnitureFactoryBusinessLogic.OfficePackage.HelperModels
{
internal class ExcelCellParameters
public class ExcelCellParameters
{
public string ColumnName { get; set; } = string.Empty;

View File

@ -11,7 +11,7 @@ using System.ComponentModel;
namespace FurnitureFactoryBusinessLogic.OfficePackage.HelperModels
{
internal class ExcelInfoManager
public class ExcelInfoManager
{
public string FileName { get; set; } = string.Empty;

View File

@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace FurnitureFactoryBusinessLogic.OfficePackage.HelperEnums
{
internal class ExcelMergeParameters
public class ExcelMergeParameters
{
public string CellFromName { get; set; } = string.Empty;

View File

@ -7,7 +7,7 @@ using FurnitureContracts.ViewModel;
namespace FurnitureFactoryBusinessLogic.OfficePackage.HelperModels
{
internal class WordInfoManager
public class WordInfoManager
{
public string FileName { get; set; } = string.Empty;

View File

@ -7,7 +7,7 @@ using System.Threading.Tasks;
namespace FurnitureFactoryBusinessLogic.OfficePackage.HelperModels
{
internal class WordTextProperties
public class WordTextProperties
{
public string Size { get; set; } = string.Empty;

View File

@ -10,7 +10,7 @@ using DocumentFormat.OpenXml.Spreadsheet;
namespace FurnitureFactoryBusinessLogic.OfficePackage.Implements
{
internal class SaveToExcelManager
public class SaveToExcelManager
{
private SpreadsheetDocument? _spreadsheetDocument;
@ -152,7 +152,7 @@ namespace FurnitureFactoryBusinessLogic.OfficePackage.Implements
};
}
protected override void CreateExcel(ExcelInfoStudent info)
protected override void CreateExcel(ExcelInfoManager info)
{
_spreadsheetDocument = SpreadsheetDocument.Create(info.FileName, SpreadsheetDocumentType.Workbook);

View File

@ -0,0 +1,124 @@
using FurnitureFactoryBusinessLogic.OfficePackage.HelperEnums;
using MigraDoc.DocumentObjectModel;
using MigraDoc.Rendering;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FurnitureFactoryBusinessLogic.OfficePackage.HelperModels;
using static System.Net.Mime.MediaTypeNames;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Office2010.Excel;
using DocumentFormat.OpenXml.Office2013.Excel;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
namespace FurnitureFactoryBusinessLogic.OfficePackage.Implements
{
internal class SaveToPdfManager
{
private Document? _document;
private Section? _section;
private Table? _table;
private static ParagraphAlignment GetParagraphAlignment(PdfParagraphAlignmentType type)
{
return type switch
{
PdfParagraphAlignmentType.Center => ParagraphAlignment.Center,
PdfParagraphAlignmentType.Left => ParagraphAlignment.Left,
PdfParagraphAlignmentType.Rigth => ParagraphAlignment.Right,
_ => ParagraphAlignment.Justify,
};
}
/// <summary>
/// Создание стилей для документа
/// </summary>
/// <param name="document"></param>
private static void DefineStyles(Document document)
{
var style = document.Styles["Normal"];
style.Font.Name = "Times New Roman";
style.Font.Size = 14;
style = document.Styles.AddStyle("NormalTitle", "Normal");
style.Font.Bold = true;
}
protected override void CreatePdf(PdfInfoManager info)
{
_document = new Document();
DefineStyles(_document);
_section = _document.AddSection();
}
protected override void CreateParagraph(PdfParagraph pdfParagraph)
{
if (_section == null)
{
return;
}
var paragraph = _section.AddParagraph(pdfParagraph.Text);
paragraph.Format.SpaceAfter = "1cm";
paragraph.Format.Alignment = GetParagraphAlignment(pdfParagraph.ParagraphAlignment);
paragraph.Style = pdfParagraph.Style;
}
protected override void CreateTable(List<string> columns)
{
if (_document == null)
{
return;
}
_table = _document.LastSection.AddTable();
foreach (var elem in columns)
{
_table.AddColumn(elem);
}
}
protected override void CreateRow(PdfRowParameters rowParameters)
{
if (_table == null)
{
return;
}
var row = _table.AddRow();
for (int i = 0; i < rowParameters.Texts.Count; ++i)
{
row.Cells[i].AddParagraph(rowParameters.Texts[i]);
if (!string.IsNullOrEmpty(rowParameters.Style))
{
row.Cells[i].Style = rowParameters.Style;
}
Unit borderWidth = 0.5;
row.Cells[i].Borders.Left.Width = borderWidth;
row.Cells[i].Borders.Right.Width = borderWidth;
row.Cells[i].Borders.Top.Width = borderWidth;
row.Cells[i].Borders.Bottom.Width = borderWidth;
row.Cells[i].Format.Alignment = GetParagraphAlignment(rowParameters.ParagraphAlignment);
row.Cells[i].VerticalAlignment = VerticalAlignment.Center;
}
}
protected override void SavePdf(PdfInfoManager info)
{
var renderer = new PdfDocumentRenderer(true)
{
Document = _document
};
renderer.RenderDocument();
renderer.PdfDocument.Save(info.FileName);
}
}
}

View File

@ -0,0 +1,140 @@
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using DocumentFormat.OpenXml;
using FurnitureFactoryBusinessLogic.OfficePackage.HelperEnums;
using FurnitureFactoryBusinessLogic.OfficePackage.HelperModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FurnitureFactoryBusinessLogic.OfficePackage.Implements
{
public class SaveToWordManager
{
private WordprocessingDocument? _wordDocument;
private Body? _docBody;
/// <summary>
/// Получение типа выравнивания
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
private static JustificationValues GetJustificationValues(WordJustificationType type)
{
return type switch
{
WordJustificationType.Both => JustificationValues.Both,
WordJustificationType.Center => JustificationValues.Center,
_ => JustificationValues.Left,
};
}
/// <summary>
/// Настройки страницы
/// </summary>
/// <returns></returns>
private static SectionProperties CreateSectionProperties()
{
var properties = new SectionProperties();
var pageSize = new PageSize
{
Orient = PageOrientationValues.Portrait
};
properties.AppendChild(pageSize);
return properties;
}
/// <summary>
/// Задание форматирования для абзаца
/// </summary>
/// <param name="paragraphProperties"></param>
/// <returns></returns>
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 CreateWord(WordInfoManager 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 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 SaveWord(WordInfoManager info)
{
if (_docBody == null || _wordDocument == null)
{
return;
}
_docBody.AppendChild(CreateSectionProperties());
_wordDocument.MainDocumentPart!.Document.Save();
_wordDocument.Close();
}
}
}