PIbd-32_Chubykina_P.P._COP_11/COP/Components/LogicalComponents/ExcelDiagramComponent.cs

118 lines
4.8 KiB
C#
Raw Normal View History

2024-09-30 20:14:34 +04:00
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 Components.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;
}
}
}
}