80 lines
2.5 KiB
C#
80 lines
2.5 KiB
C#
|
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);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|