导出Excel之Epplus使用教程3(图表设置)

时间:2023-12-03 14:21:26

导出Excel之Epplus使用教程1(基本介绍)

导出Excel之Epplus使用教程2(样式设置)

导出Excel之Epplus使用教程3(图表设置)

导出Excel之Epplus使用教程4(其他设置)

Epplus的图表实现是很简单的,它支持的图表类型也很多,基本上能满足我们的需求。创建图表分为三步(以柱状图举例):

1、创建图表

ExcelChart chart = worksheet.Drawings.AddChart("chart", eChartType.ColumnClustered);//eChartType中可以选择图表类型

2、选择数据

这一步是很关键的一步,chart.Series.Add()方法所需参数为:chart.Series.Add(Y轴数据区,X轴数据区)

ExcelChartSerie serie = chart.Series.Add(worksheet.Cells[2, 3, 5, 3], worksheet.Cells[2, 1, 5, 1]);//设置图表的x轴和y轴
serie.HeaderAddress = worksheet.Cells[1, 3];//设置图表的图例

3、设置图表样式

chart.SetPosition(150, 10);//设置位置
chart.SetSize(500, 300);//设置大小
chart.Title.Text = "销量走势";//设置图表的标题
chart.Title.Font.Color = Color.FromArgb(89, 89, 89);//设置标题的颜色
chart.Title.Font.Size = 15;//标题的大小
chart.Title.Font.Bold = true;//标题的粗体
chart.Style = eChartStyle.Style15;//设置图表的样式
chart.Legend.Border.LineStyle = eLineStyle.Solid;
chart.Legend.Border.Fill.Color = Color.FromArgb(217, 217, 217);//设置图例的样式

基本上生成图表就这么些东西了,不过不同的图表属性可能略有差异,得根据具体图表具体分析。

下面是例子的全部代码:

 FileInfo newFile = new FileInfo(@"d:\test.xlsx");
if (newFile.Exists)
{
newFile.Delete();
newFile = new FileInfo(@"d:\test.xlsx");
}
using (ExcelPackage package = new ExcelPackage(newFile))
{
ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("test"); worksheet.Cells.Style.WrapText = true;
worksheet.View.ShowGridLines = false;//去掉sheet的网格线 worksheet.Cells[, ].Value = "名称";
worksheet.Cells[, ].Value = "价格";
worksheet.Cells[, ].Value = "销量"; worksheet.Cells[, ].Value = "大米";
worksheet.Cells[, ].Value = ;
worksheet.Cells[, ].Value = ; worksheet.Cells[, ].Value = "玉米";
worksheet.Cells[, ].Value = ;
worksheet.Cells[, ].Value = ; worksheet.Cells[, ].Value = "小米";
worksheet.Cells[, ].Value = ;
worksheet.Cells[, ].Value = ; worksheet.Cells[, ].Value = "糯米";
worksheet.Cells[, ].Value = ;
worksheet.Cells[, ].Value = ; using (ExcelRange range = worksheet.Cells[, , , ])
{
range.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
range.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
} using (ExcelRange range = worksheet.Cells[, , , ])
{
range.Style.Font.Bold = true;
range.Style.Font.Color.SetColor(Color.White);
range.Style.Font.Name = "微软雅黑";
range.Style.Font.Size = ;
range.Style.Fill.PatternType = ExcelFillStyle.Solid;
range.Style.Fill.BackgroundColor.SetColor(Color.FromArgb(, , ));
} worksheet.Cells[, ].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(, , ));
worksheet.Cells[, ].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(, , ));
worksheet.Cells[, ].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(, , )); worksheet.Cells[, ].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(, , ));
worksheet.Cells[, ].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(, , ));
worksheet.Cells[, ].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(, , )); worksheet.Cells[, ].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(, , ));
worksheet.Cells[, ].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(, , ));
worksheet.Cells[, ].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(, , )); worksheet.Cells[, ].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(, , ));
worksheet.Cells[, ].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(, , ));
worksheet.Cells[, ].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(, , )); worksheet.Cells[, ].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(, , ));
worksheet.Cells[, ].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(, , ));
worksheet.Cells[, ].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(, , )); ExcelChart chart = worksheet.Drawings.AddChart("chart", eChartType.ColumnClustered); ExcelChartSerie serie = chart.Series.Add(worksheet.Cells[, , , ], worksheet.Cells[, , , ]);
serie.HeaderAddress = worksheet.Cells[, ]; chart.SetPosition(, );
chart.SetSize(, );
chart.Title.Text = "销量走势";
chart.Title.Font.Color = Color.FromArgb(, , );
chart.Title.Font.Size = ;
chart.Title.Font.Bold = true;
chart.Style = eChartStyle.Style15;
chart.Legend.Border.LineStyle = eLineStyle.Solid;
chart.Legend.Border.Fill.Color = Color.FromArgb(, , ); package.Save();
}