From 8364484ce7effa489d0d07b4ceedf0521d3b5637 Mon Sep 17 00:00:00 2001 From: abazov73 <92822431+abazov73@users.noreply.github.com> Date: Sun, 15 Oct 2023 15:18:36 +0400 Subject: [PATCH] Added excel image component --- AbazovApp/AbazovAppView/FormTest.Designer.cs | 25 +++++++ AbazovApp/AbazovAppView/FormTest.cs | 15 ++++ AbazovApp/AbazovAppView/FormTest.resx | 6 ++ .../AbazovViewComponents.csproj | 23 ++++++ .../LogicalComponents/ExcelImageInfo.cs | 22 ++++++ .../ExcelImagesComponent.Designer.cs | 36 +++++++++ .../LogicalComponents/ExcelImagesComponent.cs | 74 +++++++++++++++++++ 7 files changed, 201 insertions(+) create mode 100644 AbazovApp/AbazovViewComponents/LogicalComponents/ExcelImageInfo.cs create mode 100644 AbazovApp/AbazovViewComponents/LogicalComponents/ExcelImagesComponent.Designer.cs create mode 100644 AbazovApp/AbazovViewComponents/LogicalComponents/ExcelImagesComponent.cs diff --git a/AbazovApp/AbazovAppView/FormTest.Designer.cs b/AbazovApp/AbazovAppView/FormTest.Designer.cs index d8bb050..1bbe141 100644 --- a/AbazovApp/AbazovAppView/FormTest.Designer.cs +++ b/AbazovApp/AbazovAppView/FormTest.Designer.cs @@ -28,6 +28,7 @@ /// private void InitializeComponent() { + this.components = new System.ComponentModel.Container(); this.abazovCheckedListBox = new AbazovViewComponents.Components.AbazovCheckedListBox(); this.buttonAdd = new System.Windows.Forms.Button(); this.buttonClear = new System.Windows.Forms.Button(); @@ -42,6 +43,9 @@ this.buttonGetValue = new System.Windows.Forms.Button(); this.buttonGetIndex = new System.Windows.Forms.Button(); this.buttonSetIndex = new System.Windows.Forms.Button(); + this.excelImagesComponent = new AbazovViewComponents.LogicalComponents.ExcelImagesComponent(this.components); + this.buttonExcelImages = new System.Windows.Forms.Button(); + this.openFileDialog = new System.Windows.Forms.OpenFileDialog(); this.SuspendLayout(); // // abazovCheckedListBox @@ -91,6 +95,7 @@ this.abazovDatePicker.Name = "abazovDatePicker"; this.abazovDatePicker.Size = new System.Drawing.Size(275, 48); this.abazovDatePicker.TabIndex = 4; + this.abazovDatePicker.Value = null; this.abazovDatePicker.DateChange += new System.Action(this.abazovDatePicker_DateChange); this.abazovDatePicker.AutoSizeChanged += new System.EventHandler(this.abazovDatePicker_DateChange); // @@ -182,11 +187,28 @@ this.buttonSetIndex.UseVisualStyleBackColor = true; this.buttonSetIndex.Click += new System.EventHandler(this.buttonSetIndex_Click); // + // buttonExcelImages + // + this.buttonExcelImages.Location = new System.Drawing.Point(12, 327); + this.buttonExcelImages.Name = "buttonExcelImages"; + this.buttonExcelImages.Size = new System.Drawing.Size(150, 29); + this.buttonExcelImages.TabIndex = 14; + this.buttonExcelImages.Text = "Excel (картинки)"; + this.buttonExcelImages.UseVisualStyleBackColor = true; + this.buttonExcelImages.Click += new System.EventHandler(this.buttonExcelImages_Click); + // + // openFileDialog + // + this.openFileDialog.Filter = "Изображения|*.png;*.jpg;"; + this.openFileDialog.Multiselect = true; + this.openFileDialog.Title = "Выберте изображения"; + // // FormTest // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(850, 450); + this.Controls.Add(this.buttonExcelImages); this.Controls.Add(this.buttonSetIndex); this.Controls.Add(this.buttonGetIndex); this.Controls.Add(this.buttonGetValue); @@ -223,5 +245,8 @@ private Button buttonGetValue; private Button buttonGetIndex; private Button buttonSetIndex; + private AbazovViewComponents.LogicalComponents.ExcelImagesComponent excelImagesComponent; + private Button buttonExcelImages; + private OpenFileDialog openFileDialog; } } \ No newline at end of file diff --git a/AbazovApp/AbazovAppView/FormTest.cs b/AbazovApp/AbazovAppView/FormTest.cs index 8362f57..d95fefc 100644 --- a/AbazovApp/AbazovAppView/FormTest.cs +++ b/AbazovApp/AbazovAppView/FormTest.cs @@ -1,11 +1,13 @@ using AbazovViewComponents.Components; using AbazovViewComponents.Exceptions; +using AbazovViewComponents.LogicalComponents; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; +using System.Reflection; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; @@ -127,5 +129,18 @@ namespace AbazovAppView { abazovTreeView.SelectedNodeIndex = 0; } + + private void buttonExcelImages_Click(object sender, EventArgs e) + { + var res = openFileDialog.ShowDialog(this); + if (res != DialogResult.OK) return; + var files = openFileDialog.FileNames; + openFileDialog.Dispose(); + List images = new List(); + string path = AppDomain.CurrentDomain.BaseDirectory + "test.xlsx"; + MessageBox.Show(path); + if (excelImagesComponent.createWithImages(new ExcelImageInfo(path, "Header", files))) MessageBox.Show("Успех!"); + else MessageBox.Show("Ошибка, проверьте консоль"); + } } } diff --git a/AbazovApp/AbazovAppView/FormTest.resx b/AbazovApp/AbazovAppView/FormTest.resx index f298a7b..97e2fb7 100644 --- a/AbazovApp/AbazovAppView/FormTest.resx +++ b/AbazovApp/AbazovAppView/FormTest.resx @@ -57,4 +57,10 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + 17, 17 + + + 233, 17 + \ No newline at end of file diff --git a/AbazovApp/AbazovViewComponents/AbazovViewComponents.csproj b/AbazovApp/AbazovViewComponents/AbazovViewComponents.csproj index 060aa1c..37c6f6a 100644 --- a/AbazovApp/AbazovViewComponents/AbazovViewComponents.csproj +++ b/AbazovApp/AbazovViewComponents/AbazovViewComponents.csproj @@ -7,4 +7,27 @@ enable + + + tlbimp + 8 + 2 + 2df8d04c-5bfa-101b-bde5-00aa0044de52 + 0 + false + True + true + + + tlbimp + 9 + 1 + 00020813-0000-0000-c000-000000000046 + 0 + false + True + true + + + 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.Designer.cs b/AbazovApp/AbazovViewComponents/LogicalComponents/ExcelImagesComponent.Designer.cs new file mode 100644 index 0000000..44ea952 --- /dev/null +++ b/AbazovApp/AbazovViewComponents/LogicalComponents/ExcelImagesComponent.Designer.cs @@ -0,0 +1,36 @@ +namespace AbazovViewComponents.LogicalComponents +{ + partial class ExcelImagesComponent + { + /// + /// Обязательная переменная конструктора. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Освободить все используемые ресурсы. + /// + /// истинно, если управляемый ресурс должен быть удален; иначе ложно. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Код, автоматически созданный конструктором компонентов + + /// + /// Требуемый метод для поддержки конструктора — не изменяйте + /// содержимое этого метода с помощью редактора кода. + /// + private void InitializeComponent() + { + components = new System.ComponentModel.Container(); + } + + #endregion + } +} diff --git a/AbazovApp/AbazovViewComponents/LogicalComponents/ExcelImagesComponent.cs b/AbazovApp/AbazovViewComponents/LogicalComponents/ExcelImagesComponent.cs new file mode 100644 index 0000000..26cfe6d --- /dev/null +++ b/AbazovApp/AbazovViewComponents/LogicalComponents/ExcelImagesComponent.cs @@ -0,0 +1,74 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.Office.Interop.Excel; +using Microsoft.Office.Core; + +namespace AbazovViewComponents.LogicalComponents +{ + public partial class ExcelImagesComponent : Component + { + public ExcelImagesComponent() + { + InitializeComponent(); + } + + public ExcelImagesComponent(IContainer container) + { + container.Add(this); + + InitializeComponent(); + } + + public bool createWithImages(ExcelImageInfo info) + { + var excelApp = new Microsoft.Office.Interop.Excel.Application { SheetsInNewWorkbook = 1 }; + Workbook workbook = excelApp.Workbooks.Add(Type.Missing); + try + { + //create + Worksheet worksheet = (Worksheet)workbook.Worksheets.get_Item(1); + + //header + var excelcells = worksheet.get_Range("A1", "D1"); + excelcells.Merge(Type.Missing); + excelcells.Font.Bold = true; + excelcells.Font.Size = 14; + excelcells.Font.Name = "Times New Roman"; + excelcells.ColumnWidth = 8; + excelcells.RowHeight = 25; + excelcells.HorizontalAlignment = Constants.xlCenter; + excelcells.VerticalAlignment = Constants.xlCenter; + excelcells.Value2 = info.title; + + int topOffset = 25; + 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); + topOffset += bm.Height; + bm.Dispose(); + } + + //save + object missing = System.Reflection.Missing.Value; + workbook.SaveAs(info.path, XlFileFormat.xlOpenXMLWorkbook, missing, missing, false, false, XlSaveAsAccessMode.xlNoChange, + XlSaveConflictResolution.xlUserResolution, true, missing, missing, missing); + workbook.Close(); + excelApp.Quit(); + + return true; + } + catch (Exception) + { + workbook.Close(); + excelApp.Quit(); + return false; + } + } + } +}