From fd98232cdac4ab9ef6398a8b7328b0fd3d0c508a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D0=B8=D0=BB=20=D0=9F=D1=83=D1=82?= =?UTF-8?q?=D0=B8=D0=BD=D1=86=D0=B5=D0=B2?= Date: Wed, 30 Oct 2024 02:28:36 +0400 Subject: [PATCH] =?UTF-8?q?=D0=93=D0=BE=D1=82=D0=BE=D0=B2=D0=B0=D1=8F=20?= =?UTF-8?q?=D0=BB=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82=D0=BE=D1=80=D0=BD?= =?UTF-8?q?=D0=B0=D1=8F=202?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ComponentExcelWithImage.Designer.cs | 36 ++++ .../ComponentExcelWithImage.cs | 108 +++++++++++ .../ComponentExcelWithPieDiagram.Designer.cs | 36 ++++ .../ComponentExcelWithPieDiagram.cs | 79 ++++++++ .../ComponentExcelWithTable.Designer.cs | 36 ++++ .../ComponentExcelWithTable.cs | 177 ++++++++++++++++++ COP/PutincevLibrary/Enums/LegendPosition.cs | 10 + COP/PutincevLibrary/Info/DataItem.cs | 8 + COP/PutincevLibrary/Info/ExcelChartInfo.cs | 22 +++ COP/PutincevLibrary/Info/ExcelTableInfo.cs | 19 ++ COP/PutincevLibrary/PutincevLibrary.csproj | 29 +++ COP/WinForms/FormNoVisual.Designer.cs | 167 +++++++++++++++++ COP/WinForms/FormNoVisual.cs | 152 +++++++++++++++ COP/WinForms/FormNoVisual.resx | 129 +++++++++++++ COP/WinForms/Program.cs | 2 +- 15 files changed, 1009 insertions(+), 1 deletion(-) create mode 100644 COP/PutincevLibrary/ComponentExcelWithImage.Designer.cs create mode 100644 COP/PutincevLibrary/ComponentExcelWithImage.cs create mode 100644 COP/PutincevLibrary/ComponentExcelWithPieDiagram.Designer.cs create mode 100644 COP/PutincevLibrary/ComponentExcelWithPieDiagram.cs create mode 100644 COP/PutincevLibrary/ComponentExcelWithTable.Designer.cs create mode 100644 COP/PutincevLibrary/ComponentExcelWithTable.cs create mode 100644 COP/PutincevLibrary/Enums/LegendPosition.cs create mode 100644 COP/PutincevLibrary/Info/DataItem.cs create mode 100644 COP/PutincevLibrary/Info/ExcelChartInfo.cs create mode 100644 COP/PutincevLibrary/Info/ExcelTableInfo.cs create mode 100644 COP/WinForms/FormNoVisual.Designer.cs create mode 100644 COP/WinForms/FormNoVisual.cs create mode 100644 COP/WinForms/FormNoVisual.resx diff --git a/COP/PutincevLibrary/ComponentExcelWithImage.Designer.cs b/COP/PutincevLibrary/ComponentExcelWithImage.Designer.cs new file mode 100644 index 0000000..fc6d3e3 --- /dev/null +++ b/COP/PutincevLibrary/ComponentExcelWithImage.Designer.cs @@ -0,0 +1,36 @@ +namespace PutincevLibrary +{ + partial class ComponentExcelWithImage + { + /// + /// Обязательная переменная конструктора. + /// + 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/COP/PutincevLibrary/ComponentExcelWithImage.cs b/COP/PutincevLibrary/ComponentExcelWithImage.cs new file mode 100644 index 0000000..a099c52 --- /dev/null +++ b/COP/PutincevLibrary/ComponentExcelWithImage.cs @@ -0,0 +1,108 @@ +using Excel = Microsoft.Office.Interop.Excel; +using System.ComponentModel; + + +namespace PutincevLibrary +{ + public partial class ComponentExcelWithImage : Component + { + public ComponentExcelWithImage() + { + InitializeComponent(); + } + + public ComponentExcelWithImage(IContainer container) : this() + { + container.Add(this); + } + + private void CheckInputValues(string filePath, string tableTitle, string[] imagePaths) + { + if (string.IsNullOrEmpty(filePath)) + { + throw new ArgumentNullException(nameof(filePath)); + } + if (string.IsNullOrEmpty(tableTitle)) + { + throw new ArgumentNullException(nameof(tableTitle)); + } + if (imagePaths == null) + { + throw new ArgumentNullException(nameof(imagePaths)); + } + foreach (string imagePath in imagePaths) + { + if (string.IsNullOrEmpty(imagePath)) + { + throw new ArgumentNullException("Image path is null"); + } + } + } + + public void CreateExcelWithImages(string filePath, string tableTitle, string[] imagePaths) + { + CheckInputValues(filePath, tableTitle, imagePaths); + + Excel.Application excelApp = new Excel.Application(); + Excel.Workbook workbook = excelApp.Workbooks.Add(); + Excel.Worksheet worksheet = workbook.Sheets[1]; + + worksheet.Cells[1, 1] = tableTitle; + + InsertImagesInSingleCell(worksheet, imagePaths, 2, 1); + + workbook.SaveAs(filePath); + workbook.Close(); + excelApp.Quit(); + + ReleaseObject(worksheet); + ReleaseObject(workbook); + ReleaseObject(excelApp); + } + + private void InsertImagesInSingleCell(Excel.Worksheet worksheet, string[] imagePaths, int rowIndex, int columnIndex) + { + Excel.Range cell = worksheet.Cells[rowIndex, columnIndex]; + double currentTopOffset = 0; + + foreach (string imagePath in imagePaths) + { + if (File.Exists(imagePath)) + { + Excel.Pictures pictures = (Excel.Pictures)worksheet.Pictures(System.Reflection.Missing.Value); + Excel.Picture picture = pictures.Insert(imagePath); + + picture.Left = cell.Left; + picture.Top = cell.Top + currentTopOffset; + + currentTopOffset += picture.Height; + } + else + { + throw new FileNotFoundException("Image not found at: " + imagePath); + } + } + } + + private void ReleaseObject(object obj) + { + try + { + if (obj != null) + { + System.Runtime.InteropServices.Marshal.ReleaseComObject(obj); + obj = null; + } + } + catch (Exception ex) + { + obj = null; + Console.WriteLine("Error releasing object: " + ex.ToString()); + } + finally + { + GC.Collect(); + } + } + } +} \ No newline at end of file diff --git a/COP/PutincevLibrary/ComponentExcelWithPieDiagram.Designer.cs b/COP/PutincevLibrary/ComponentExcelWithPieDiagram.Designer.cs new file mode 100644 index 0000000..09375a3 --- /dev/null +++ b/COP/PutincevLibrary/ComponentExcelWithPieDiagram.Designer.cs @@ -0,0 +1,36 @@ +namespace PutincevLibrary +{ + partial class ComponentExcelWithPieDiagram + { + /// + /// Обязательная переменная конструктора. + /// + 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/COP/PutincevLibrary/ComponentExcelWithPieDiagram.cs b/COP/PutincevLibrary/ComponentExcelWithPieDiagram.cs new file mode 100644 index 0000000..296c801 --- /dev/null +++ b/COP/PutincevLibrary/ComponentExcelWithPieDiagram.cs @@ -0,0 +1,79 @@ +using PutincevLibrary.Info; +using System.ComponentModel; +using OfficeOpenXml; +using LicenseContext = OfficeOpenXml.LicenseContext; +using OfficeOpenXml.Drawing.Chart; + +namespace PutincevLibrary +{ + public partial class ComponentExcelWithPieDiagram : Component + { + public ComponentExcelWithPieDiagram() + { + InitializeComponent(); + } + + public ComponentExcelWithPieDiagram(IContainer container) + { + container.Add(this); + + InitializeComponent(); + } + + public enum LegendPosition + { + Top, + Bottom, + Left, + Right + } + + public void GenerateDocument(ExcelChartInfo info) + { + if (string.IsNullOrEmpty(info.filePath)) + { + throw new ArgumentException("File path is null or empty."); + } + if (string.IsNullOrEmpty(info.chartTitle)) + { + throw new ArgumentException("Chart title is null or empty."); + } + if (string.IsNullOrEmpty(info.documentTitle)) + { + throw new ArgumentException("Document title is null or empty."); + } + if (info.data == null || info.data.Count == 0) + { + throw new ArgumentException("Data is null or empty."); + } + + ExcelPackage.LicenseContext = LicenseContext.NonCommercial; + using ExcelPackage excelPackage = new(); + + ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets.Add("Sheet 1"); + worksheet.Cells["A1"].Value = info.documentTitle; + + int row = 2; + int startCol = 1; + int endCol = 1; + + foreach (var data in info.data) + { + worksheet.Cells[row, endCol].Value = data.Name; + worksheet.Cells[row + 1, endCol].Value = data.Value; + endCol++; + } + + ExcelPieChart? pieChart = worksheet.Drawings.AddChart(info.chartTitle, eChartType.Pie) as ExcelPieChart; + pieChart.Title.Text = info.chartTitle; + pieChart.Series.Add(ExcelCellBase.GetAddress(row + 1, startCol, row + 1, endCol - 1), + ExcelCellBase.GetAddress(row, startCol, row, endCol - 1)); + + pieChart.Legend.Position = (eLegendPosition)(int)info.legendPosition; + pieChart.DataLabel.ShowPercent = true; + pieChart.SetPosition(1, 0, 0, 0); + FileInfo fi = new(info.filePath); + excelPackage.SaveAs(fi); + } + } +} diff --git a/COP/PutincevLibrary/ComponentExcelWithTable.Designer.cs b/COP/PutincevLibrary/ComponentExcelWithTable.Designer.cs new file mode 100644 index 0000000..b6a2e79 --- /dev/null +++ b/COP/PutincevLibrary/ComponentExcelWithTable.Designer.cs @@ -0,0 +1,36 @@ +namespace PutincevLibrary +{ + partial class ComponentExcelWithTable + { + /// + /// Обязательная переменная конструктора. + /// + 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/COP/PutincevLibrary/ComponentExcelWithTable.cs b/COP/PutincevLibrary/ComponentExcelWithTable.cs new file mode 100644 index 0000000..137686d --- /dev/null +++ b/COP/PutincevLibrary/ComponentExcelWithTable.cs @@ -0,0 +1,177 @@ +using NPOI.XSSF.UserModel; +using OfficeOpenXml.Drawing; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using PutincevLibrary.Info; +using NPOI.SS.UserModel; +using NPOI.SS.Util; +using BorderStyle = NPOI.SS.UserModel.BorderStyle; +using HorizontalAlignment = NPOI.SS.UserModel.HorizontalAlignment; + +namespace PutincevLibrary +{ + public partial class ComponentExcelWithTable : Component + { + public ComponentExcelWithTable() + { + InitializeComponent(); + } + + public ComponentExcelWithTable(IContainer container) + { + container.Add(this); + + InitializeComponent(); + } + public void GenerateDocument(ExcelTableInfo info) where T : class + { + if (string.IsNullOrEmpty(info.FilePath)) + { + throw new ArgumentException("File path is null or empty."); + } + if (string.IsNullOrEmpty(info.DocumentTitle)) + { + throw new ArgumentException("Document title is null or empty."); + } + if (info.Data == null || info.Data.Count == 0) + { + throw new ArgumentException("Data is null or empty."); + } + + var workbook = new XSSFWorkbook(); + var sheet = workbook.CreateSheet("Таблица"); + + sheet.CreateRow(0).CreateCell(0).SetCellValue(info.DocumentTitle); + + int curRow = 1; + int curCol = 0; + List headers = new(); + + var dataCellStyle = GetDataCellStyle(workbook); + var headerCellStyle = GetHeaderCellStyle(workbook); + + foreach (var header in info.Headers) + { + headers.Add(header.Key); + + var currentRow = sheet.CreateRow(curRow); + var curCell = currentRow.CreateCell(curCol); + + if (header.Value.Item1.Count > 1) + { + sheet.AddMergedRegion(new CellRangeAddress(curRow, curRow + header.Value.Item1.Count - 1, curCol, curCol)); + curCol++; + foreach (var (key, displayName) in header.Value.Item1) + { + int id = header.Value.Item1.IndexOf((key, displayName)); + var cellValue = currentRow.CreateCell(1); + cellValue.SetCellValue(displayName); + currentRow.Height = (short)(header.Value.Item2[id] * 20); + headers.Add(displayName); + cellValue.CellStyle = headerCellStyle; + currentRow = sheet.CreateRow(currentRow.RowNum + 1); + } + } + else + { + int id = header.Value.Item1.IndexOf(header.Value.Item1[0]); + sheet.AddMergedRegion(new CellRangeAddress(curRow, curRow, curCol, curCol + 1)); + currentRow.Height = (short)(header.Value.Item2[id] * 20); + curRow++; + } + + curCell.SetCellValue(header.Key); + curCell.CellStyle = headerCellStyle; + if (header.Value.Item1.Count > 1) + { + curCol--; + curRow += header.Value.Item1.Count; + } + } + + curCol = 2; + foreach (var data in info.Data) + { + curRow = 1; + + foreach (var header in info.Headers.Values) + { + foreach (var (key, _) in header.Item1) + { + var property = typeof(T).GetProperty(key); + var cellValue = sheet.GetRow(curRow)?.CreateCell(curCol); + if (cellValue != null && property != null) + { + var value = property?.GetValue(data)?.ToString(); + var rowHeight = GetRowHeight(key, info); + var currentRow = sheet.GetRow(curRow); + if (rowHeight != null) + { + currentRow.Height = (short)(rowHeight.Value * 20); + } + cellValue.SetCellValue(value); + cellValue.CellStyle = dataCellStyle; + } + curRow++; + } + } + curCol++; + } + + using var fs = new FileStream(info.FilePath, FileMode.Create, FileAccess.Write); + workbook.Write(fs); + } + + private static int? GetRowHeight(string header, ExcelTableInfo info) where T : class + { + foreach (var item in info.Headers) + { + foreach (var (key, displayName) in item.Value.Item1) + { + int id = item.Value.Item1.IndexOf((key, displayName)); + if (key == header) + return item.Value.Item2[id]; + } + } + return 0; + } + + private static ICellStyle GetHeaderCellStyle(IWorkbook workbook) + { + var style = workbook.CreateCellStyle(); + style.BorderBottom = BorderStyle.Thin; + style.BorderLeft = BorderStyle.Thin; + style.BorderRight = BorderStyle.Thin; + style.BorderTop = BorderStyle.Thin; + + var font = workbook.CreateFont(); + font.Boldweight = (short)FontBoldWeight.Bold; + style.SetFont(font); + style.Alignment = HorizontalAlignment.Center; + style.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center; + + return style; + } + + private static ICellStyle GetDataCellStyle(IWorkbook workbook) + { + var style = workbook.CreateCellStyle(); + style.BorderBottom = BorderStyle.Thin; + style.BorderLeft = BorderStyle.Thin; + style.BorderRight = BorderStyle.Thin; + style.BorderTop = BorderStyle.Thin; + + return style; + } + } + + public class Property + { + public string? Name { get; set; } + } +} diff --git a/COP/PutincevLibrary/Enums/LegendPosition.cs b/COP/PutincevLibrary/Enums/LegendPosition.cs new file mode 100644 index 0000000..015333b --- /dev/null +++ b/COP/PutincevLibrary/Enums/LegendPosition.cs @@ -0,0 +1,10 @@ +namespace PutincevLibrary.Enums +{ + public enum LegendPosition + { + Top, + Bottom, + Left, + Right + } +} diff --git a/COP/PutincevLibrary/Info/DataItem.cs b/COP/PutincevLibrary/Info/DataItem.cs new file mode 100644 index 0000000..d3ddd96 --- /dev/null +++ b/COP/PutincevLibrary/Info/DataItem.cs @@ -0,0 +1,8 @@ +namespace PutincevLibrary.Info +{ + public class DataItem + { + public string Name { get; set; } = string.Empty; + public double Value { get; set; } + } +} diff --git a/COP/PutincevLibrary/Info/ExcelChartInfo.cs b/COP/PutincevLibrary/Info/ExcelChartInfo.cs new file mode 100644 index 0000000..4d1b784 --- /dev/null +++ b/COP/PutincevLibrary/Info/ExcelChartInfo.cs @@ -0,0 +1,22 @@ +using PutincevLibrary.Enums; + +namespace PutincevLibrary.Info +{ + public class ExcelChartInfo + { + public string? filePath { get; set; } = string.Empty; + public string? documentTitle { get; set; } = string.Empty; + public string? chartTitle { get; set; } = string.Empty; + public LegendPosition legendPosition { get; set; } + public List? data { get; set; } + + public ExcelChartInfo(string? filePath, string? documentTitle, string? chartTitle, LegendPosition legendPosition, List? data) + { + this.filePath = filePath; + this.documentTitle = documentTitle; + this.chartTitle = chartTitle; + this.legendPosition = legendPosition; + this.data = data; + } + } +} diff --git a/COP/PutincevLibrary/Info/ExcelTableInfo.cs b/COP/PutincevLibrary/Info/ExcelTableInfo.cs new file mode 100644 index 0000000..718288f --- /dev/null +++ b/COP/PutincevLibrary/Info/ExcelTableInfo.cs @@ -0,0 +1,19 @@ +namespace PutincevLibrary.Info +{ + public class ExcelTableInfo where T: class + { + public string FilePath { get; set; } = string.Empty; + public string DocumentTitle { get; set; } = string.Empty; + + public List? Data; + public Dictionary, List)> Headers { get; set; } + + public ExcelTableInfo (string filePath, string documentTitle, List data, Dictionary, List)> headers) + { + FilePath = filePath; + DocumentTitle = documentTitle; + Data = data; + Headers = headers; + } + } +} diff --git a/COP/PutincevLibrary/PutincevLibrary.csproj b/COP/PutincevLibrary/PutincevLibrary.csproj index db6be70..3684330 100644 --- a/COP/PutincevLibrary/PutincevLibrary.csproj +++ b/COP/PutincevLibrary/PutincevLibrary.csproj @@ -8,4 +8,33 @@ Library + + + tlbimp + 8 + 2 + 2df8d04c-5bfa-101b-bde5-00aa0044de52 + 0 + false + true + + + tlbimp + 9 + 1 + 00020813-0000-0000-c000-000000000046 + 0 + false + true + + + + + + + + + + + diff --git a/COP/WinForms/FormNoVisual.Designer.cs b/COP/WinForms/FormNoVisual.Designer.cs new file mode 100644 index 0000000..d6501e3 --- /dev/null +++ b/COP/WinForms/FormNoVisual.Designer.cs @@ -0,0 +1,167 @@ +namespace WinForms +{ + partial class FormNoVisual + { + /// + /// 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() + { + components = new System.ComponentModel.Container(); + textBoxFilePath = new TextBox(); + buttonSetFilePath = new Button(); + textBoxTitle = new TextBox(); + listBoxImages = new ListBox(); + buttonCreateExcelFile = new Button(); + buttonAddImage = new Button(); + buttonClearImages = new Button(); + buttonCreateExcelWithPieDiagram = new Button(); + componentExcelWithPieDiagram1 = new PutincevLibrary.ComponentExcelWithPieDiagram(components); + componentExcelWithImage1 = new PutincevLibrary.ComponentExcelWithImage(components); + buttonCreateExcelWithTable = new Button(); + componentExcelWithTable1 = new PutincevLibrary.ComponentExcelWithTable(components); + SuspendLayout(); + // + // textBoxFilePath + // + textBoxFilePath.Location = new Point(12, 12); + textBoxFilePath.Name = "textBoxFilePath"; + textBoxFilePath.PlaceholderText = "Путь к файлу"; + textBoxFilePath.ReadOnly = true; + textBoxFilePath.Size = new Size(248, 27); + textBoxFilePath.TabIndex = 0; + // + // buttonSetFilePath + // + buttonSetFilePath.Location = new Point(275, 10); + buttonSetFilePath.Name = "buttonSetFilePath"; + buttonSetFilePath.Size = new Size(94, 29); + buttonSetFilePath.TabIndex = 1; + buttonSetFilePath.Text = "Выбрать"; + buttonSetFilePath.UseVisualStyleBackColor = true; + buttonSetFilePath.Click += buttonSetFilePath_Click; + // + // textBoxTitle + // + textBoxTitle.Location = new Point(12, 45); + textBoxTitle.Name = "textBoxTitle"; + textBoxTitle.PlaceholderText = "Заголовок"; + textBoxTitle.Size = new Size(357, 27); + textBoxTitle.TabIndex = 2; + // + // listBoxImages + // + listBoxImages.FormattingEnabled = true; + listBoxImages.ItemHeight = 20; + listBoxImages.Location = new Point(12, 127); + listBoxImages.Name = "listBoxImages"; + listBoxImages.Size = new Size(357, 104); + listBoxImages.TabIndex = 3; + // + // buttonCreateExcelFile + // + buttonCreateExcelFile.Location = new Point(12, 272); + buttonCreateExcelFile.Name = "buttonCreateExcelFile"; + buttonCreateExcelFile.Size = new Size(357, 29); + buttonCreateExcelFile.TabIndex = 4; + buttonCreateExcelFile.Text = "Создать Excel файл с картинкой"; + buttonCreateExcelFile.UseVisualStyleBackColor = true; + buttonCreateExcelFile.Click += buttonCreateExcelFile_Click; + // + // buttonAddImage + // + buttonAddImage.Location = new Point(12, 237); + buttonAddImage.Name = "buttonAddImage"; + buttonAddImage.Size = new Size(257, 29); + buttonAddImage.TabIndex = 5; + buttonAddImage.Text = "Добавить изображение"; + buttonAddImage.UseVisualStyleBackColor = true; + buttonAddImage.Click += buttonAddImage_Click; + // + // buttonClearImages + // + buttonClearImages.Location = new Point(275, 237); + buttonClearImages.Name = "buttonClearImages"; + buttonClearImages.Size = new Size(94, 29); + buttonClearImages.TabIndex = 6; + buttonClearImages.Text = "Очистить"; + buttonClearImages.UseVisualStyleBackColor = true; + buttonClearImages.Click += buttonClearImages_Click; + // + // buttonCreateExcelWithPieDiagram + // + buttonCreateExcelWithPieDiagram.Location = new Point(401, 12); + buttonCreateExcelWithPieDiagram.Name = "buttonCreateExcelWithPieDiagram"; + buttonCreateExcelWithPieDiagram.Size = new Size(300, 29); + buttonCreateExcelWithPieDiagram.TabIndex = 12; + buttonCreateExcelWithPieDiagram.Text = "Создать Excel файл с диаграммой"; + buttonCreateExcelWithPieDiagram.UseVisualStyleBackColor = true; + buttonCreateExcelWithPieDiagram.Click += buttonCreateExcelWithPieDiagram_Click; + // + // buttonCreateExcelWithTable + // + buttonCreateExcelWithTable.Location = new Point(401, 86); + buttonCreateExcelWithTable.Name = "buttonCreateExcelWithTable"; + buttonCreateExcelWithTable.Size = new Size(300, 28); + buttonCreateExcelWithTable.TabIndex = 13; + buttonCreateExcelWithTable.Text = "Создать Excel файл с таблицей\r\n"; + buttonCreateExcelWithTable.UseVisualStyleBackColor = true; + buttonCreateExcelWithTable.Click += buttonCreateExcelWithTable_Click; + // + // FormNoVisual + // + AutoScaleDimensions = new SizeF(8F, 20F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(1054, 446); + Controls.Add(buttonCreateExcelWithTable); + Controls.Add(buttonCreateExcelWithPieDiagram); + Controls.Add(buttonClearImages); + Controls.Add(buttonAddImage); + Controls.Add(buttonCreateExcelFile); + Controls.Add(listBoxImages); + Controls.Add(textBoxTitle); + Controls.Add(buttonSetFilePath); + Controls.Add(textBoxFilePath); + Name = "FormNoVisual"; + Text = "FormNoVisual"; + ResumeLayout(false); + PerformLayout(); + } + + #endregion + + private TextBox textBoxFilePath; + private Button buttonSetFilePath; + private TextBox textBoxTitle; + private ListBox listBoxImages; + private Button buttonCreateExcelFile; + private Button buttonAddImage; + private Button buttonClearImages; + private Button buttonCreateExcelWithPieDiagram; + private PutincevLibrary.ComponentExcelWithPieDiagram componentExcelWithPieDiagram1; + private PutincevLibrary.ComponentExcelWithImage componentExcelWithImage1; + private Button buttonCreateExcelWithTable; + private PutincevLibrary.ComponentExcelWithTable componentExcelWithTable1; + } +} \ No newline at end of file diff --git a/COP/WinForms/FormNoVisual.cs b/COP/WinForms/FormNoVisual.cs new file mode 100644 index 0000000..22a3617 --- /dev/null +++ b/COP/WinForms/FormNoVisual.cs @@ -0,0 +1,152 @@ +using PutincevLibrary; +using PutincevLibrary.Info; +using static System.Windows.Forms.VisualStyles.VisualStyleElement; +using PutincevLibrary.Enums; +using OfficeOpenXml.Table; + +namespace WinForms +{ + public partial class FormNoVisual : Form + { + + public FormNoVisual() + { + InitializeComponent(); + } + + private void buttonCreateExcelFile_Click(object sender, EventArgs e) + { + string[] list = new string[listBoxImages.Items.Count]; + for (int i = 0; i < listBoxImages.Items.Count; i++) + { + list[i] = listBoxImages.Items[i].ToString(); + } + try + { + componentExcelWithImage1.CreateExcelWithImages(textBoxFilePath.Text, textBoxTitle.Text, list); + MessageBox.Show("Файл успешно создан", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + } + catch (Exception ex) + { + MessageBox.Show($"Ошибка при создании файла:\n{ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void buttonSetFilePath_Click(object sender, EventArgs e) + { + var filePath = string.Empty; + + using (SaveFileDialog saveFileDialog = new SaveFileDialog()) + { + saveFileDialog.InitialDirectory = "d:\\tmp"; + saveFileDialog.Filter = "Excel files (*.xlsx)|*.xlsx|All files (*.*)|*.*"; + saveFileDialog.FilterIndex = 1; + saveFileDialog.RestoreDirectory = true; + + if (saveFileDialog.ShowDialog() == DialogResult.OK) + { + filePath = saveFileDialog.FileName; + } + } + + if (!string.IsNullOrEmpty(filePath)) + { + textBoxFilePath.Text = filePath; + } + else + { + textBoxFilePath.Text = string.Empty; + } + } + + private void buttonAddImage_Click(object sender, EventArgs e) + { + var filePath = string.Empty; + + using (OpenFileDialog openFileDialog = new OpenFileDialog()) + { + openFileDialog.InitialDirectory = "d:\\tmp"; + openFileDialog.Filter = "Image files (*.jpeg;*.jpg;*.png)|*.jpeg;*.jpg;*.png|All files (*.*)|*.*"; + openFileDialog.FilterIndex = 1; + openFileDialog.RestoreDirectory = true; + + if (openFileDialog.ShowDialog() == DialogResult.OK) + { + filePath = openFileDialog.FileName; + } + } + + if (!string.IsNullOrEmpty(filePath)) + { + listBoxImages.Items.Add(filePath); + } + } + + private void buttonClearImages_Click(object sender, EventArgs e) + { + listBoxImages.Items.Clear(); + } + + private void buttonCreateExcelWithPieDiagram_Click(object sender, EventArgs e) + { + ComponentExcelWithPieDiagram chart = new(); + + LegendPosition legend = new(); + var data = new List() + { + new DataItem() { Name = "Собаки", Value = 40 }, + new DataItem() { Name = "Кошки", Value = 30 }, + new DataItem() { Name = "Хомячки", Value = 20 }, + new DataItem() { Name = "Люди", Value = 50 } + }; + ExcelChartInfo info = new("E:\\testchart.xlsx", "My Document", "My Chart", legend, data); + try + { + chart.GenerateDocument(info); + MessageBox.Show("Сохарнено успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void buttonCreateExcelWithTable_Click(object sender, EventArgs e) + { + ComponentExcelWithTable table = new(); + var data = new List + { + new Employee2 { Id = 1, Name = "Даниил", Surname = "Путинцев", Age = "20", Department = "IT", Position = "Уборщик" }, + new Employee2 { Id = 2, Name = "Илья", Surname = "Родионов", Age = "19", Department = "Design", Position = "Работник" }, + }; + Dictionary, List)> headers = new() + { + { "ID", (new List<(string, string)> { ("Id", "Идентификатор") }, new List { 30 }) }, + { "Личные данные", (new List<(string, string)> { ("Name", "Имя"), ("Surname", "Фамилия"), ("Age", "Возраст") }, new List { 25, 25, 25 }) }, + { "Работа", (new List<(string, string)> { ("Department", "Отдел"), ("Position", "Должность") }, new List { 25, 25 }) } + }; + + ExcelTableInfo info = new("E:\\table.xlsx", "My Document", data, headers); + try + { + table.GenerateDocument(info); + MessageBox.Show("Сохарнено успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + public class Employee2 + { + public int? Id { get; set; } + public string? Name { get; set; } = string.Empty; + public string? Surname { get; set; } = string.Empty; + public string? Age { get; set; } = string.Empty; + public string? Department { get; set; } = string.Empty; + public string? Position { get; set; } = string.Empty; + } + } +} diff --git a/COP/WinForms/FormNoVisual.resx b/COP/WinForms/FormNoVisual.resx new file mode 100644 index 0000000..d5d394c --- /dev/null +++ b/COP/WinForms/FormNoVisual.resx @@ -0,0 +1,129 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 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 + + + 17, 17 + + + 300, 17 + + + 547, 17 + + \ No newline at end of file diff --git a/COP/WinForms/Program.cs b/COP/WinForms/Program.cs index 59ed7ae..6b6b749 100644 --- a/COP/WinForms/Program.cs +++ b/COP/WinForms/Program.cs @@ -11,7 +11,7 @@ namespace WinForms // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - Application.Run(new Form1()); + Application.Run(new FormNoVisual()); } } } \ No newline at end of file