Added excel diagram component

This commit is contained in:
abazov73 2023-10-19 22:07:56 +04:00
parent 9431261a10
commit d302d4644b
7 changed files with 220 additions and 1 deletions

View File

@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AbazovAppView
{
public class Department
{
public string name;
public int sells;
public Department(string name, int sells)
{
this.name = name;
this.sells = sells;
}
}
}

View File

@ -48,6 +48,8 @@
this.openFileDialog = new System.Windows.Forms.OpenFileDialog();
this.buttonExcelTable = new System.Windows.Forms.Button();
this.excelTableComponent = new AbazovViewComponents.LogicalComponents.ExcelTableComponent(this.components);
this.excelDiagramComponent = new AbazovViewComponents.LogicalComponents.ExcelDiagramComponent(this.components);
this.buttonExcelDiagram = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// abazovCheckedListBox
@ -215,11 +217,22 @@
this.buttonExcelTable.UseVisualStyleBackColor = true;
this.buttonExcelTable.Click += new System.EventHandler(this.buttonExcelTable_Click);
//
// buttonExcelDiagram
//
this.buttonExcelDiagram.Location = new System.Drawing.Point(12, 372);
this.buttonExcelDiagram.Name = "buttonExcelDiagram";
this.buttonExcelDiagram.Size = new System.Drawing.Size(150, 29);
this.buttonExcelDiagram.TabIndex = 16;
this.buttonExcelDiagram.Text = "Excel (Диаграмма)";
this.buttonExcelDiagram.UseVisualStyleBackColor = true;
this.buttonExcelDiagram.Click += new System.EventHandler(this.buttonExcelDiagram_Click);
//
// 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.buttonExcelDiagram);
this.Controls.Add(this.buttonExcelTable);
this.Controls.Add(this.buttonExcelImages);
this.Controls.Add(this.buttonSetIndex);
@ -263,5 +276,7 @@
private OpenFileDialog openFileDialog;
private Button buttonExcelTable;
private AbazovViewComponents.LogicalComponents.ExcelTableComponent excelTableComponent;
private AbazovViewComponents.LogicalComponents.ExcelDiagramComponent excelDiagramComponent;
private Button buttonExcelDiagram;
}
}

View File

@ -138,7 +138,6 @@ namespace AbazovAppView
openFileDialog.Dispose();
List<Bitmap> images = new List<Bitmap>();
string path = AppDomain.CurrentDomain.BaseDirectory + "test.xlsx";
MessageBox.Show(path);
if (excelImagesComponent.createWithImages(new ExcelImageInfo(path, "Header", files))) MessageBox.Show("Успех!");
else MessageBox.Show("Ошибка, проверьте консоль");
}
@ -165,5 +164,17 @@ namespace AbazovAppView
if (excelTableComponent.createWithTable(path, "test", merges, heights, headers, workers)) MessageBox.Show("Успех");
}
private void buttonExcelDiagram_Click(object sender, EventArgs e)
{
List<Department> departments = new List<Department>();
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("Успех");
else MessageBox.Show("Fail :(");
}
}
}

View File

@ -66,4 +66,7 @@
<metadata name="excelTableComponent.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>392, 17</value>
</metadata>
<metadata name="excelDiagramComponent.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>596, 17</value>
</metadata>
</root>

View File

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AbazovViewComponents.LogicalComponents
{
public enum DiagramLegendEnum
{
TopLeft = 0,
TopRight = 1,
BottomRight = 2,
BottomLeft = 3,
}
}

View File

@ -0,0 +1,36 @@
namespace AbazovViewComponents.LogicalComponents
{
partial class ExcelDiagramComponent
{
/// <summary>
/// Обязательная переменная конструктора.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Освободить все используемые ресурсы.
/// </summary>
/// <param name="disposing">истинно, если управляемый ресурс должен быть удален; иначе ложно.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Код, автоматически созданный конструктором компонентов
/// <summary>
/// Требуемый метод для поддержки конструктора — не изменяйте
/// содержимое этого метода с помощью редактора кода.
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
}
#endregion
}
}

View File

@ -0,0 +1,118 @@
using Microsoft.Office.Interop.Excel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace AbazovViewComponents.LogicalComponents
{
public partial class ExcelDiagramComponent : Component
{
public ExcelDiagramComponent()
{
InitializeComponent();
}
public ExcelDiagramComponent(IContainer container)
{
container.Add(this);
InitializeComponent();
}
public bool createWithDiagram<T>(string path, string title, string diagramTitle, DiagramLegendEnum diagramLegendAnchor, List<T> data, string seriesNameField, string valueField)
{
var excelApp = new Microsoft.Office.Interop.Excel.Application { SheetsInNewWorkbook = 1 };
Workbook workbook = excelApp.Workbooks.Add(Type.Missing);
try
{
Worksheet worksheet = (Worksheet)workbook.Worksheets.get_Item(1);
FieldInfo? seriesName = typeof(T).GetField(seriesNameField);
FieldInfo? value = typeof(T).GetField(valueField);
if (seriesName == null || value == null) throw new ArgumentException("Переданного поля не существует");
int columnCount = 2;
foreach(var item in data)
{
var cell = worksheet.get_Range("A" + columnCount, "A" + columnCount);
cell.Font.Size = 14;
cell.Font.Name = "Times New Roman";
cell.ColumnWidth = 8;
cell.RowHeight = 25;
cell.HorizontalAlignment = Constants.xlCenter;
cell.VerticalAlignment = Constants.xlCenter;
cell.Value2 = seriesName.GetValue(item);
cell = worksheet.get_Range("B" + columnCount, "B" + columnCount);
cell.Font.Size = 14;
cell.Font.Name = "Times New Roman";
cell.ColumnWidth = 8;
cell.RowHeight = 25;
cell.HorizontalAlignment = Constants.xlCenter;
cell.VerticalAlignment = Constants.xlCenter;
cell.Value2 = value.GetValue(item);
columnCount++;
}
//header
var excelcells = worksheet.get_Range("A1", "A1");
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 = title;
var charts = worksheet.ChartObjects() as ChartObjects;
int chartWidth = 300;
int chartHeight = 300;
var chartObject = charts.Add(250, 10, chartWidth, chartHeight);
var chart = chartObject.Chart;
var range = worksheet.get_Range($"A2", $"B{columnCount - 1}");
chart.SetSourceData(range);
chart.ChartType = XlChartType.xlPie;
switch (diagramLegendAnchor)
{
case DiagramLegendEnum.TopLeft:
chart.Legend.Top = 0;
chart.Legend.Left = 0;
break;
case DiagramLegendEnum.TopRight:
chart.Legend.Top = 0;
chart.Legend.Left = chartWidth - chart.Legend.Width;
break;
case DiagramLegendEnum.BottomLeft:
chart.Legend.Top = chartHeight - chart.Legend.Height;
chart.Legend.Left = 0;
break;
case DiagramLegendEnum.BottomRight:
chart.Legend.Top = chartHeight - chart.Legend.Height;
chart.Legend.Left = chartWidth - chart.Legend.Width;
break;
}
chart.ChartWizard(Source: range, Title: diagramTitle);
object missing = System.Reflection.Missing.Value;
workbook.SaveAs(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();
throw;
}
}
}
}