Список магазинов в Word

This commit is contained in:
prodigygirl 2023-03-26 08:49:42 +04:00
parent d5bfbdfd58
commit ca519fe8c8
6 changed files with 138 additions and 3 deletions

View File

@ -1,4 +1,5 @@
using FurnitureAssemblyBusinessLogic.OfficePackage.HelperEnums;
using DocumentFormat.OpenXml.EMMA;
using FurnitureAssemblyBusinessLogic.OfficePackage.HelperEnums;
using FurnitureAssemblyBusinessLogic.OfficePackage.HelperModels;
@ -6,6 +7,28 @@ namespace FurnitureAssemblyBusinessLogic.OfficePackage
{
public abstract class AbstractSaveToWord
{
public void CreateTableDoc(WordInfo wordInfo) {
CreateWord(wordInfo);
var list = new List<string>();
foreach (var shop in wordInfo.Shops) {
list.Add(shop.ShopName);
list.Add(shop.Address);
list.Add(shop.DateOpening.ToString());
}
//list.AddRange(wordInfo.Shops.Select(x => (x.ShopName, new WordTextProperties { Bold = true, Size = "24" })));
var wordTable = new WordTable
{
Headers = new List<string> {
"Название",
"Адрес",
"Дата открытия"},
Texts = list
};
CreateTable(wordTable);
SaveWord(wordInfo);
}
public void CreateDoc(WordInfo info)
{
CreateWord(info);
@ -38,6 +61,7 @@ namespace FurnitureAssemblyBusinessLogic.OfficePackage
/// </summary>
/// <param name="info"></param>
protected abstract void CreateWord(WordInfo info);
protected abstract void CreateTable(WordTable info);
/// <summary>
/// Создание абзаца с текстом
/// </summary>

View File

@ -7,5 +7,6 @@ namespace FurnitureAssemblyBusinessLogic.OfficePackage.HelperModels
public string FileName { get; set; } = string.Empty;
public string Title { get; set; } = string.Empty;
public List<FurnitureViewModel> Furnitures { get; set; } = new();
public List<ShopViewModel> Shops { get; set; } = new();
}
}

View File

@ -0,0 +1,12 @@

using FurnitureAssemblyContracts.ViewModels;
namespace FurnitureAssemblyBusinessLogic.OfficePackage.HelperModels
{
public class WordTable
{
public List<string> Headers { get; set; } = new();
public List<string> Texts { get; set; } = new ();
}
}

View File

@ -3,6 +3,7 @@ using FurnitureAssemblyBusinessLogic.OfficePackage.HelperModels;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using System.Xml.Linq;
namespace FurnitureAssemblyBusinessLogic.OfficePackage.Implements
{
@ -122,5 +123,86 @@ namespace FurnitureAssemblyBusinessLogic.OfficePackage.Implements
_wordDocument.MainDocumentPart!.Document.Save();
_wordDocument.Close();
}
protected override void CreateTable(WordTable table)
{
if (_docBody == null || table == null)
{
return;
}
Table tab = new Table();
TableProperties props = new TableProperties(
new TableBorders(
new TopBorder
{
Val = new EnumValue<BorderValues>(BorderValues.Single),
Size = 12
},
new BottomBorder
{
Val = new EnumValue<BorderValues>(BorderValues.Single),
Size = 12
},
new LeftBorder
{
Val = new EnumValue<BorderValues>(BorderValues.Single),
Size = 12
},
new RightBorder
{
Val = new EnumValue<BorderValues>(BorderValues.Single),
Size = 12
},
new InsideHorizontalBorder
{
Val = new EnumValue<BorderValues>(BorderValues.Single),
Size = 12
},
new InsideVerticalBorder
{
Val = new EnumValue<BorderValues>(BorderValues.Single),
Size = 12
}
)
);
tab.AppendChild<TableProperties>(props);
// разметка сетки таблицы
TableGrid tableGrid = new TableGrid();
for (int i = 0; i < table.Headers.Count; i++) {
tableGrid.AppendChild(new GridColumn ());
}
tab.AppendChild(tableGrid);
TableRow tableRow = new TableRow();
// заполнение заголовков
foreach (var text in table.Headers)
{
tableRow.AppendChild(CreateTableCell(text));
}
tab.AppendChild(tableRow);
// заполнение содержимым
int height = table.Texts.Count / table.Headers.Count;
int width = table.Headers.Count;
for (int i = 0; i < height; i++) {
tableRow = new TableRow();
for (int j = 0; j < width; j++) {
var element = table.Texts[i * table.Headers.Count + j];
tableRow.AppendChild(CreateTableCell(element));
}
tab.AppendChild(tableRow);
}
_docBody.AppendChild(tab);
}
private TableCell CreateTableCell(string element) {
var tableParagraph = new Paragraph();
var run = new Run();
run.AppendChild(new Text { Text = element });
tableParagraph.AppendChild(run);
var tableCell = new TableCell();
tableCell.AppendChild(tableParagraph);
return tableCell;
}
}
}

View File

@ -17,17 +17,19 @@ namespace FurnitureAssemblyBusinessLogic
{
private readonly IFurnitureStorage _furnitureStorage;
private readonly IOrderStorage _orderStorage;
private readonly IShopStorage _shopStorage;
private readonly AbstractSaveToExcel _saveToExcel;
private readonly AbstractSaveToWord _saveToWord;
private readonly AbstractSaveToPdf _saveToPdf;
public ReportLogic(IFurnitureStorage furnitureStorage, IOrderStorage orderStorage,
AbstractSaveToExcel saveToExcel, AbstractSaveToWord saveToWord, AbstractSaveToPdf saveToPdf)
AbstractSaveToExcel saveToExcel, AbstractSaveToWord saveToWord, AbstractSaveToPdf saveToPdf, IShopStorage shopStorage)
{
_furnitureStorage = furnitureStorage;
_orderStorage = orderStorage;
_saveToExcel = saveToExcel;
_saveToWord = saveToWord;
_saveToPdf = saveToPdf;
_shopStorage = shopStorage;
}
/// <summary>
/// Получение списка компонент с указанием, в каких изделиях используются
@ -88,7 +90,7 @@ namespace FurnitureAssemblyBusinessLogic
Title = "Список изделий",
Furnitures = _furnitureStorage.GetFullList()
});
}
}
/// <summary>
/// Сохранение компонент с указаеним продуктов в файл-Excel
/// </summary>
@ -117,5 +119,18 @@ namespace FurnitureAssemblyBusinessLogic
Orders = GetOrders(model)
});
}
/// <summary>
/// Сохранение магазинов в файл-Word
/// </summary>
/// <param name="model"></param>
public void SaveShopsToWordFile(ReportBindingModel model)
{
_saveToWord.CreateTableDoc(new WordInfo
{
FileName = model.FileName,
Title = "Список магазинов",
Shops = _shopStorage.GetFullList()
});
}
}
}

View File

@ -31,5 +31,6 @@ namespace FurnitureAssemblyContracts.BusinessLogicsContarcts
/// </summary>
/// <param name="model"></param>
void SaveOrdersToPdfFile(ReportBindingModel model);
void SaveShopsToWordFile(ReportBindingModel model);
}
}