From 42b90c4d906f46f464726aeb9fe9fe367a45a044 Mon Sep 17 00:00:00 2001 From: abazov73 <92822431+abazov73@users.noreply.github.com> Date: Fri, 20 Oct 2023 09:34:18 +0400 Subject: [PATCH] Returned excel image info --- AbazovApp/AbazovAppView/FormTest.cs | 10 ++++----- .../LogicalComponents/ExcelImageInfo.cs | 22 +++++++++++++++++++ .../LogicalComponents/ExcelImagesComponent.cs | 8 +++---- 3 files changed, 31 insertions(+), 9 deletions(-) create mode 100644 AbazovApp/AbazovViewComponents/LogicalComponents/ExcelImageInfo.cs diff --git a/AbazovApp/AbazovAppView/FormTest.cs b/AbazovApp/AbazovAppView/FormTest.cs index bfa9301..fbf27ee 100644 --- a/AbazovApp/AbazovAppView/FormTest.cs +++ b/AbazovApp/AbazovAppView/FormTest.cs @@ -138,7 +138,7 @@ namespace AbazovAppView openFileDialog.Dispose(); List images = new List(); string path = AppDomain.CurrentDomain.BaseDirectory + "test.xlsx"; - if (excelImagesComponent.createWithImages(path, "Header", files)) MessageBox.Show("Успех!"); + if (excelImagesComponent.createWithImages(new ExcelImageInfo(path, "Header", files))) MessageBox.Show("Успех!"); else MessageBox.Show("Ошибка, проверьте консоль"); } @@ -149,7 +149,7 @@ namespace AbazovAppView workers.Add(new OfficeWorker(1, "Иванов", "Иван", 20, "Отдел продаж", "Бухгалтер", 25, "+7(834)234-03-49")); workers.Add(new OfficeWorker(1, "Петров", "Петр", 25, "Отдел продаж", "Менеджер", 20, "+7(834)123-03-49")); workers.Add(new OfficeWorker(1, "Сидоров", "Сергей", 27, "Отдел кадров", "HR", 2, "+7(834)593-03-49", true)); - string path = AppDomain.CurrentDomain.BaseDirectory + "test.xlsx"; + string path = AppDomain.CurrentDomain.BaseDirectory + "test2.xlsx"; List<(int, int)> merges = new List<(int, int)>(); merges.Add((1, 3)); merges.Add((4, 6)); @@ -162,7 +162,7 @@ namespace AbazovAppView ("boxNumber", "Номер бокса"), ("phoneNumber", "Телефон"), ("isInVacation", "В отпуске"), }; - if (excelTableComponent.createWithTable(path, "test", merges, heights, headers, workers)) MessageBox.Show("Успех"); + if (excelTableComponent.createWithTable(path, "test2", merges, heights, headers, workers)) MessageBox.Show("Успех"); } private void buttonExcelDiagram_Click(object sender, EventArgs e) @@ -172,8 +172,8 @@ namespace AbazovAppView departments.Add(new Department("Dep 1", 330)); departments.Add(new Department("Dep 2", 500)); departments.Add(new Department("Dep 3", 170)); - string path = AppDomain.CurrentDomain.BaseDirectory + "test.xlsx"; - if (excelDiagramComponent.createWithDiagram(path, "test", "Продажи", DiagramLegendEnum.TopRight, departments, "name", "sells")) MessageBox.Show("Успех"); + string path = AppDomain.CurrentDomain.BaseDirectory + "test3.xlsx"; + if (excelDiagramComponent.createWithDiagram(path, "test3", "Продажи", DiagramLegendEnum.TopRight, departments, "name", "sells")) MessageBox.Show("Успех"); else MessageBox.Show("Fail :("); } } diff --git a/AbazovApp/AbazovViewComponents/LogicalComponents/ExcelImageInfo.cs b/AbazovApp/AbazovViewComponents/LogicalComponents/ExcelImageInfo.cs new file mode 100644 index 0000000..179b035 --- /dev/null +++ b/AbazovApp/AbazovViewComponents/LogicalComponents/ExcelImageInfo.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AbazovViewComponents.LogicalComponents +{ + public class ExcelImageInfo + { + public ExcelImageInfo(string path, string title, string[] imagePaths) + { + this.path = path; + this.title = title; + this.imagePaths = imagePaths; + } + + public string path; + public string title; + public string[] imagePaths; + } +} diff --git a/AbazovApp/AbazovViewComponents/LogicalComponents/ExcelImagesComponent.cs b/AbazovApp/AbazovViewComponents/LogicalComponents/ExcelImagesComponent.cs index 94f3aa3..26cfe6d 100644 --- a/AbazovApp/AbazovViewComponents/LogicalComponents/ExcelImagesComponent.cs +++ b/AbazovApp/AbazovViewComponents/LogicalComponents/ExcelImagesComponent.cs @@ -24,7 +24,7 @@ namespace AbazovViewComponents.LogicalComponents InitializeComponent(); } - public bool createWithImages(string pathToFile, string title, string[] imagePaths) + public bool createWithImages(ExcelImageInfo info) { var excelApp = new Microsoft.Office.Interop.Excel.Application { SheetsInNewWorkbook = 1 }; Workbook workbook = excelApp.Workbooks.Add(Type.Missing); @@ -43,10 +43,10 @@ namespace AbazovViewComponents.LogicalComponents excelcells.RowHeight = 25; excelcells.HorizontalAlignment = Constants.xlCenter; excelcells.VerticalAlignment = Constants.xlCenter; - excelcells.Value2 = title; + excelcells.Value2 = info.title; int topOffset = 25; - foreach (string path in imagePaths) + foreach (string path in info.imagePaths) { Bitmap bm = new Bitmap(path); worksheet.Shapes.AddPicture2(path, MsoTriState.msoFalse, MsoTriState.msoCTrue, 0, topOffset, bm.Width, bm.Height, MsoPictureCompress.msoPictureCompressFalse); @@ -56,7 +56,7 @@ namespace AbazovViewComponents.LogicalComponents //save object missing = System.Reflection.Missing.Value; - workbook.SaveAs(pathToFile, XlFileFormat.xlOpenXMLWorkbook, missing, missing, false, false, XlSaveAsAccessMode.xlNoChange, + workbook.SaveAs(info.path, XlFileFormat.xlOpenXMLWorkbook, missing, missing, false, false, XlSaveAsAccessMode.xlNoChange, XlSaveConflictResolution.xlUserResolution, true, missing, missing, missing); workbook.Close(); excelApp.Quit();