From 3c02e6902a7e80d067d316d264bcb1c69527fe8c Mon Sep 17 00:00:00 2001 From: prodigygirl Date: Sun, 26 Mar 2023 09:42:55 +0400 Subject: [PATCH] =?UTF-8?q?Excel:=20=D1=81=D0=BF=D0=B8=D1=81=D0=BE=D0=BA?= =?UTF-8?q?=20=D0=BC=D0=B0=D0=B3=D0=B0=D0=B7=D0=B8=D0=BD=D0=BE=D0=B2=20?= =?UTF-8?q?=D1=81=20=D0=B8=D0=B7=D0=B4=D0=B5=D0=BB=D0=B8=D1=8F=D0=BC=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../FurnitureAssembly/FormMain.Designer.cs | 1 + .../FurnitureAssembly/FormMain.cs | 9 ++ .../FormReportShopFurnitures.Designer.cs | 104 ++++++++++++++++ .../FormReportShopFurnitures.cs | 87 ++++++++++++++ .../FormReportShopFurnitures.resx | 78 ++++++++++++ .../FurnitureAssembly/Program.cs | 1 + .../OfficePackage/AbstractSaveToExcel.cs | 113 +++++++++++++++++- .../OfficePackage/HelperModels/ExcelInfo.cs | 7 +- .../HelperModels/ExcelInfoFurnitures.cs | 18 +++ .../HelperModels/ExcelInfoShops.cs | 18 +++ .../ReportLogic.cs | 38 +++++- .../BusinessLogicsContarcts/IReportLogic.cs | 3 + .../ReportShopFurnituresViewModel.cs | 15 +++ 13 files changed, 481 insertions(+), 11 deletions(-) create mode 100644 FurnitureAssembly/FurnitureAssembly/FormReportShopFurnitures.Designer.cs create mode 100644 FurnitureAssembly/FurnitureAssembly/FormReportShopFurnitures.cs create mode 100644 FurnitureAssembly/FurnitureAssembly/FormReportShopFurnitures.resx create mode 100644 FurnitureAssembly/FurnitureAssemblyBusinessLogic/OfficePackage/HelperModels/ExcelInfoFurnitures.cs create mode 100644 FurnitureAssembly/FurnitureAssemblyBusinessLogic/OfficePackage/HelperModels/ExcelInfoShops.cs create mode 100644 FurnitureAssembly/FurnitureAssemblyContracts/ViewModels/ReportShopFurnituresViewModel.cs diff --git a/FurnitureAssembly/FurnitureAssembly/FormMain.Designer.cs b/FurnitureAssembly/FurnitureAssembly/FormMain.Designer.cs index f841985..849bbff 100644 --- a/FurnitureAssembly/FurnitureAssembly/FormMain.Designer.cs +++ b/FurnitureAssembly/FurnitureAssembly/FormMain.Designer.cs @@ -207,6 +207,7 @@ this.ShopsFurnituresToolStripMenuItem.Name = "ShopsFurnituresToolStripMenuItem"; this.ShopsFurnituresToolStripMenuItem.Size = new System.Drawing.Size(216, 22); this.ShopsFurnituresToolStripMenuItem.Text = "Изделия в магазинах"; + this.ShopsFurnituresToolStripMenuItem.Click += new System.EventHandler(this.ShopsFurnituresToolStripMenuItem_Click); // // OrderDateToolStripMenuItem // diff --git a/FurnitureAssembly/FurnitureAssembly/FormMain.cs b/FurnitureAssembly/FurnitureAssembly/FormMain.cs index eee9fe1..14d6c1d 100644 --- a/FurnitureAssembly/FurnitureAssembly/FormMain.cs +++ b/FurnitureAssembly/FurnitureAssembly/FormMain.cs @@ -234,5 +234,14 @@ namespace FurnitureAssembly MessageBoxIcon.Information); } } + + private void ShopsFurnituresToolStripMenuItem_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormReportShopFurnitures)); + if (service is FormReportShopFurnitures form) + { + form.ShowDialog(); + } + } } } diff --git a/FurnitureAssembly/FurnitureAssembly/FormReportShopFurnitures.Designer.cs b/FurnitureAssembly/FurnitureAssembly/FormReportShopFurnitures.Designer.cs new file mode 100644 index 0000000..c07d679 --- /dev/null +++ b/FurnitureAssembly/FurnitureAssembly/FormReportShopFurnitures.Designer.cs @@ -0,0 +1,104 @@ +namespace FurnitureAssembly +{ + partial class FormReportShopFurnitures + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.ButtonSaveToExcel = new System.Windows.Forms.Button(); + this.dataGridView = new System.Windows.Forms.DataGridView(); + this.Shop = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.Furniture = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.Count = new System.Windows.Forms.DataGridViewTextBoxColumn(); + ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit(); + this.SuspendLayout(); + // + // ButtonSaveToExcel + // + this.ButtonSaveToExcel.Location = new System.Drawing.Point(12, 22); + this.ButtonSaveToExcel.Name = "ButtonSaveToExcel"; + this.ButtonSaveToExcel.Size = new System.Drawing.Size(166, 27); + this.ButtonSaveToExcel.TabIndex = 1; + this.ButtonSaveToExcel.Text = "Сохранить в Excel"; + this.ButtonSaveToExcel.UseVisualStyleBackColor = true; + this.ButtonSaveToExcel.Click += new System.EventHandler(this.ButtonSaveToExcel_Click); + // + // dataGridView + // + this.dataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; + this.dataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] { + this.Shop, + this.Furniture, + this.Count}); + this.dataGridView.Location = new System.Drawing.Point(12, 80); + this.dataGridView.Name = "dataGridView"; + this.dataGridView.RowTemplate.Height = 25; + this.dataGridView.Size = new System.Drawing.Size(600, 358); + this.dataGridView.TabIndex = 2; + // + // Shop + // + this.Shop.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill; + this.Shop.HeaderText = "Магазин"; + this.Shop.Name = "Shop"; + // + // Furniture + // + this.Furniture.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill; + this.Furniture.HeaderText = "Изделие"; + this.Furniture.Name = "Furniture"; + // + // Count + // + this.Count.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.ColumnHeader; + this.Count.HeaderText = "Количество"; + this.Count.Name = "Count"; + this.Count.Width = 97; + // + // FormReportShopFurnitures + // + this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(627, 450); + this.Controls.Add(this.dataGridView); + this.Controls.Add(this.ButtonSaveToExcel); + this.Name = "FormReportShopFurnitures"; + this.Text = "Магазины с изделиями"; + this.Load += new System.EventHandler(this.FormReportShopFurnitures_Load); + ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit(); + this.ResumeLayout(false); + + } + + #endregion + + private Button ButtonSaveToExcel; + private DataGridView dataGridView; + private DataGridViewTextBoxColumn Shop; + private DataGridViewTextBoxColumn Furniture; + private DataGridViewTextBoxColumn Count; + } +} \ No newline at end of file diff --git a/FurnitureAssembly/FurnitureAssembly/FormReportShopFurnitures.cs b/FurnitureAssembly/FurnitureAssembly/FormReportShopFurnitures.cs new file mode 100644 index 0000000..e4b3d87 --- /dev/null +++ b/FurnitureAssembly/FurnitureAssembly/FormReportShopFurnitures.cs @@ -0,0 +1,87 @@ +using FurnitureAssemblyContracts.BindingModels; +using FurnitureAssemblyContracts.BusinessLogicsContarcts; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace FurnitureAssembly +{ + public partial class FormReportShopFurnitures : Form + { + private readonly ILogger _logger; + private readonly IReportLogic _logic; + public FormReportShopFurnitures(ILogger logger, IReportLogic reportLogic) + { + InitializeComponent(); + _logger = logger; + _logic = reportLogic; + } + + private void ButtonSaveToExcel_Click(object sender, EventArgs e) + { + using var dialog = new SaveFileDialog + { + Filter = "xlsx|*.xlsx" + }; + if (dialog.ShowDialog() == DialogResult.OK) + { + try + { + _logic.SaveShopFurnituresToExcelFile(new + ReportBindingModel + { + FileName = dialog.FileName + }); + _logger.LogInformation("Сохранение списка магазинов с изделиями в них"); + + MessageBox.Show("Выполнено", "Успех", MessageBoxButtons.OK, + MessageBoxIcon.Information); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка сохранения списка магазинов с изделиями в них"); + + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, + MessageBoxIcon.Error); + } + } + } + + private void FormReportShopFurnitures_Load(object sender, EventArgs e) + { + try + { + var dict = _logic.GetShopFurnitures(); + if (dict != null) + { + dataGridView.Rows.Clear(); + foreach (var elem in dict) + { + dataGridView.Rows.Add(new object[] { elem.ShopName, "", "" }); + foreach (var listElem in elem.Furnitures) + { + dataGridView.Rows.Add(new object[] { "", listElem.Item1, listElem.Item2 }); + } + dataGridView.Rows.Add(new object[] { "Итого", "", elem.TotalCount }); + dataGridView.Rows.Add(Array.Empty()); + } + } + _logger.LogInformation("Загрузка списка магазинов с изделиями в них"); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка загрузки списка магазинов с изделиями в них"); + + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, + MessageBoxIcon.Error); + } + } + } +} diff --git a/FurnitureAssembly/FurnitureAssembly/FormReportShopFurnitures.resx b/FurnitureAssembly/FurnitureAssembly/FormReportShopFurnitures.resx new file mode 100644 index 0000000..7f75b9f --- /dev/null +++ b/FurnitureAssembly/FurnitureAssembly/FormReportShopFurnitures.resx @@ -0,0 +1,78 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + True + + + True + + + True + + + True + + + True + + + True + + \ No newline at end of file diff --git a/FurnitureAssembly/FurnitureAssembly/Program.cs b/FurnitureAssembly/FurnitureAssembly/Program.cs index 520c635..7044ee0 100644 --- a/FurnitureAssembly/FurnitureAssembly/Program.cs +++ b/FurnitureAssembly/FurnitureAssembly/Program.cs @@ -64,6 +64,7 @@ namespace FurnitureAssembly services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); } } } \ No newline at end of file diff --git a/FurnitureAssembly/FurnitureAssemblyBusinessLogic/OfficePackage/AbstractSaveToExcel.cs b/FurnitureAssembly/FurnitureAssemblyBusinessLogic/OfficePackage/AbstractSaveToExcel.cs index 4e1cdf3..0acd7c9 100644 --- a/FurnitureAssembly/FurnitureAssemblyBusinessLogic/OfficePackage/AbstractSaveToExcel.cs +++ b/FurnitureAssembly/FurnitureAssemblyBusinessLogic/OfficePackage/AbstractSaveToExcel.cs @@ -1,4 +1,5 @@ -using FurnitureAssemblyBusinessLogic.OfficePackage.HelperEnums; +using DocumentFormat.OpenXml.EMMA; +using FurnitureAssemblyBusinessLogic.OfficePackage.HelperEnums; using FurnitureAssemblyBusinessLogic.OfficePackage.HelperModels; using System; using System.Collections.Generic; @@ -30,6 +31,64 @@ namespace FurnitureAssemblyBusinessLogic.OfficePackage CellToName = "C1" }); uint rowIndex = 2; + if (info is ExcelInfoFurnitures infoFurnitures) + { + CreateExcelCellsFurnitures(infoFurnitures, rowIndex); + } + else if (info is ExcelInfoShops infoShops) + { + CreateExcelCellsShops(infoShops, rowIndex); + } + /* foreach (var pc in info.FurnitureComponents) + { + InsertCellInWorksheet(new ExcelCellParameters + { + ColumnName = "A", + RowIndex = rowIndex, + Text = pc.FurnitureName, + StyleInfo = ExcelStyleInfoType.Text + }); + rowIndex++; + foreach (var product in pc.Components) + { + InsertCellInWorksheet(new ExcelCellParameters + { + ColumnName = "B", + RowIndex = rowIndex, + Text = product.Item1, + StyleInfo = + ExcelStyleInfoType.TextWithBroder + }); + InsertCellInWorksheet(new ExcelCellParameters + { + ColumnName = "C", + RowIndex = rowIndex, + Text = product.Item2.ToString(), + StyleInfo = + ExcelStyleInfoType.TextWithBroder + }); + rowIndex++; + } + InsertCellInWorksheet(new ExcelCellParameters + { + ColumnName = "A", + RowIndex = rowIndex, + Text = "Итого", + StyleInfo = ExcelStyleInfoType.Text + }); + InsertCellInWorksheet(new ExcelCellParameters + { + ColumnName = "C", + RowIndex = rowIndex, + Text = pc.TotalCount.ToString(), + StyleInfo = ExcelStyleInfoType.Text + }); + rowIndex++; + }*/ + SaveExcel(info); + } + private void CreateExcelCellsFurnitures(ExcelInfoFurnitures info, uint rowIndex) + { foreach (var pc in info.FurnitureComponents) { InsertCellInWorksheet(new ExcelCellParameters @@ -76,8 +135,58 @@ namespace FurnitureAssemblyBusinessLogic.OfficePackage }); rowIndex++; } - SaveExcel(info); } + + private void CreateExcelCellsShops(ExcelInfoShops info, uint rowIndex) + { + foreach (var sF in info.ShopFurnitures) + { + InsertCellInWorksheet(new ExcelCellParameters + { + ColumnName = "A", + RowIndex = rowIndex, + Text = sF.ShopName, + StyleInfo = ExcelStyleInfoType.Text + }); + rowIndex++; + foreach (var furniture in sF.Furnitures) + { + InsertCellInWorksheet(new ExcelCellParameters + { + ColumnName = "B", + RowIndex = rowIndex, + Text = furniture.Item1, + StyleInfo = + ExcelStyleInfoType.TextWithBroder + }); + InsertCellInWorksheet(new ExcelCellParameters + { + ColumnName = "C", + RowIndex = rowIndex, + Text = furniture.Item2.ToString(), + StyleInfo = + ExcelStyleInfoType.TextWithBroder + }); + rowIndex++; + } + InsertCellInWorksheet(new ExcelCellParameters + { + ColumnName = "A", + RowIndex = rowIndex, + Text = "Итого", + StyleInfo = ExcelStyleInfoType.Text + }); + InsertCellInWorksheet(new ExcelCellParameters + { + ColumnName = "C", + RowIndex = rowIndex, + Text = sF.TotalCount.ToString(), + StyleInfo = ExcelStyleInfoType.Text + }); + rowIndex++; + } + } + /// /// Создание excel-файла /// diff --git a/FurnitureAssembly/FurnitureAssemblyBusinessLogic/OfficePackage/HelperModels/ExcelInfo.cs b/FurnitureAssembly/FurnitureAssemblyBusinessLogic/OfficePackage/HelperModels/ExcelInfo.cs index 8ba52a0..668f5cd 100644 --- a/FurnitureAssembly/FurnitureAssemblyBusinessLogic/OfficePackage/HelperModels/ExcelInfo.cs +++ b/FurnitureAssembly/FurnitureAssemblyBusinessLogic/OfficePackage/HelperModels/ExcelInfo.cs @@ -5,11 +5,6 @@ namespace FurnitureAssemblyBusinessLogic.OfficePackage.HelperModels public class ExcelInfo { public string FileName { get; set; } = string.Empty; - public string Title { get; set; } = string.Empty; - public List FurnitureComponents - { - get; - set; - } = new(); + public string Title { get; set; } = string.Empty; } } diff --git a/FurnitureAssembly/FurnitureAssemblyBusinessLogic/OfficePackage/HelperModels/ExcelInfoFurnitures.cs b/FurnitureAssembly/FurnitureAssemblyBusinessLogic/OfficePackage/HelperModels/ExcelInfoFurnitures.cs new file mode 100644 index 0000000..697b1db --- /dev/null +++ b/FurnitureAssembly/FurnitureAssemblyBusinessLogic/OfficePackage/HelperModels/ExcelInfoFurnitures.cs @@ -0,0 +1,18 @@ +using FurnitureAssemblyContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FurnitureAssemblyBusinessLogic.OfficePackage.HelperModels +{ + public class ExcelInfoFurnitures : ExcelInfo + { + public List FurnitureComponents + { + get; + set; + } = new(); + } +} diff --git a/FurnitureAssembly/FurnitureAssemblyBusinessLogic/OfficePackage/HelperModels/ExcelInfoShops.cs b/FurnitureAssembly/FurnitureAssemblyBusinessLogic/OfficePackage/HelperModels/ExcelInfoShops.cs new file mode 100644 index 0000000..d3f0dd0 --- /dev/null +++ b/FurnitureAssembly/FurnitureAssemblyBusinessLogic/OfficePackage/HelperModels/ExcelInfoShops.cs @@ -0,0 +1,18 @@ +using FurnitureAssemblyContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FurnitureAssemblyBusinessLogic.OfficePackage.HelperModels +{ + public class ExcelInfoShops : ExcelInfo + { + public List ShopFurnitures + { + get; + set; + } = new(); + } +} diff --git a/FurnitureAssembly/FurnitureAssemblyBusinessLogic/ReportLogic.cs b/FurnitureAssembly/FurnitureAssemblyBusinessLogic/ReportLogic.cs index b93f9a6..9f11769 100644 --- a/FurnitureAssembly/FurnitureAssemblyBusinessLogic/ReportLogic.cs +++ b/FurnitureAssembly/FurnitureAssemblyBusinessLogic/ReportLogic.cs @@ -84,7 +84,7 @@ namespace FurnitureAssemblyBusinessLogic /// public void SaveFurnituresToWordFile(ReportBindingModel model) { - _saveToWord.CreateDoc(new WordInfo + _saveToWord.CreateDoc(new WordInfoFurnitures { FileName = model.FileName, Title = "Список изделий", @@ -97,7 +97,7 @@ namespace FurnitureAssemblyBusinessLogic /// public void SaveFurnitureComponentToExcelFile(ReportBindingModel model) { - _saveToExcel.CreateReport(new ExcelInfo + _saveToExcel.CreateReport(new ExcelInfoFurnitures { FileName = model.FileName, Title = "Список изделий", @@ -125,12 +125,44 @@ namespace FurnitureAssemblyBusinessLogic /// public void SaveShopsToWordFile(ReportBindingModel model) { - _saveToWord.CreateTableDoc(new WordInfo + _saveToWord.CreateTableDoc(new WordInfoShops { FileName = model.FileName, Title = "Список магазинов", Shops = _shopStorage.GetFullList() }); } + + public void SaveShopFurnituresToExcelFile(ReportBindingModel model) + { + _saveToExcel.CreateReport(new ExcelInfoShops + { + FileName = model.FileName, + Title = "Список магазинов с изделиями", + ShopFurnitures = GetShopFurnitures() + }); + } + + public List GetShopFurnitures() + { + var shops = _shopStorage.GetFullList(); + var list = new List(); + foreach (var shop in shops) + { + var record = new ReportShopFurnituresViewModel + { + ShopName = shop.ShopName, + Furnitures = new List>(), + TotalCount = 0 + }; + foreach (var furnitureCount in shop.Furnitures.Values) + { + record.Furnitures.Add(new Tuple(furnitureCount.Item1.FurnitureName, furnitureCount.Item2)); + record.TotalCount += furnitureCount.Item2; + } + list.Add(record); + } + return list; + } } } diff --git a/FurnitureAssembly/FurnitureAssemblyContracts/BusinessLogicsContarcts/IReportLogic.cs b/FurnitureAssembly/FurnitureAssemblyContracts/BusinessLogicsContarcts/IReportLogic.cs index eff1f58..d3b108f 100644 --- a/FurnitureAssembly/FurnitureAssemblyContracts/BusinessLogicsContarcts/IReportLogic.cs +++ b/FurnitureAssembly/FurnitureAssemblyContracts/BusinessLogicsContarcts/IReportLogic.cs @@ -32,5 +32,8 @@ namespace FurnitureAssemblyContracts.BusinessLogicsContarcts /// void SaveOrdersToPdfFile(ReportBindingModel model); void SaveShopsToWordFile(ReportBindingModel model); + void SaveShopFurnituresToExcelFile(ReportBindingModel model); + + List GetShopFurnitures(); } } diff --git a/FurnitureAssembly/FurnitureAssemblyContracts/ViewModels/ReportShopFurnituresViewModel.cs b/FurnitureAssembly/FurnitureAssemblyContracts/ViewModels/ReportShopFurnituresViewModel.cs new file mode 100644 index 0000000..079b791 --- /dev/null +++ b/FurnitureAssembly/FurnitureAssemblyContracts/ViewModels/ReportShopFurnituresViewModel.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FurnitureAssemblyContracts.ViewModels +{ + public class ReportShopFurnituresViewModel + { + public string ShopName { get; set; } = string.Empty; + public int TotalCount { get; set; } + public List> Furnitures { get; set; } = new(); + } +}