PIbd-33_Sergunov_M.A._COP_4/Library/CustomComponents/DiagramComponent.cs

99 lines
3.2 KiB
C#
Raw Normal View History

2023-11-16 19:05:42 +04:00
using System.ComponentModel;
using System.IO;
using System.Runtime.InteropServices;
using Excel = Microsoft.Office.Interop.Excel;
namespace CustomComponents
{
public partial class DiagramComponent : Component
{
public DiagramComponent()
{
InitializeComponent();
}
public DiagramComponent(IContainer container)
{
container.Add(this);
InitializeComponent();
}
public void CreateExcel(LineChartConfig config)
{
if (string.IsNullOrEmpty(config.FilePath))
throw new ArgumentException("Файл не задан");
if (string.IsNullOrEmpty(config.Header))
throw new ArgumentException("Название документа не задано");
if (string.IsNullOrEmpty(config.ChartTitle))
throw new ArgumentException("Название диаграммы не задано");
if (config.Values == null || config.Values.Count == 0)
throw new ArgumentException("Значения серий не заданы");
var xlApp = new Excel.Application();
Excel.Workbook xlWorkBook = xlApp.Workbooks.Add();
Excel.Worksheet xlWorkSheet = (Excel.Worksheet)xlWorkBook.Sheets[1];
xlWorkSheet.Cells[1, 1] = config.Header;
// Создание диаграммы
Excel.ChartObjects chartObjs = (Excel.ChartObjects)xlWorkSheet.ChartObjects();
Excel.ChartObject chartObj = chartObjs.Add(5, 50, 300, 300);
Excel.Chart xlChart = chartObj.Chart;
xlChart.ChartType = Excel.XlChartType.xlLine;
// Запись данных в Excel, сохранение Ranges
Excel.Range[] valuesRange = new Excel.Range[config.Values.Count];
int leftTopI = 2, leftTopJ = 1;
for (int i = 0; i < config.Values.Count; i++)
{
string key = config.Values.Keys.ToList()[i];
for (int j = 0; j < config.Values[key].Count; j++)
{
xlWorkSheet.Cells[leftTopI + i, leftTopJ + j] = config.Values[key][j];
}
valuesRange[i] = xlWorkSheet.Range
[xlWorkSheet.Cells[leftTopI + i, leftTopJ],
xlWorkSheet.Cells[leftTopI + i, leftTopJ + config.Values[key].Count - 1]];
}
// Задание данных
Excel.SeriesCollection seriesCollection = (Excel.SeriesCollection)xlChart.SeriesCollection();
for (int i = 0; i < config.Values.Keys.Count; i++)
{
Excel.Series series = seriesCollection.NewSeries();
series.Name = config.Values.Keys.ToList()[i];
series.Values = valuesRange[i];
}
//Задание заголовка
xlChart.HasTitle = true;
xlChart.ChartTitle.Text = config.ChartTitle;
// Задание легенды
xlChart.HasLegend = true;
switch (config.LegendPosition)
{
case LegendPosition.Left:
xlChart.Legend.Position = Excel.XlLegendPosition.xlLegendPositionLeft;
break;
case LegendPosition.Top:
xlChart.Legend.Position = Excel.XlLegendPosition.xlLegendPositionTop;
break;
case LegendPosition.Right:
xlChart.Legend.Position = Excel.XlLegendPosition.xlLegendPositionRight;
break;
case LegendPosition.Botton:
xlChart.Legend.Position = Excel.XlLegendPosition.xlLegendPositionBottom;
break;
}
if (File.Exists(config.FilePath)) { File.Delete(config.FilePath); }
xlApp.Application.ActiveWorkbook.SaveAs(config.FilePath);
xlWorkBook.Close(true);
xlApp.Quit();
Marshal.ReleaseComObject(xlApp);
}
}
}