1 使用插件名称Epplus,多个Sheet页数据应用,Demo为柱状图(Epplus支持多种图表)
2 Epplus 的安装和引用
新建一个工程文件或控制台应用程序 打开 Vs2017 Tools 的Nuget 包管理器,使用命令 install-package Epplus
3 开始在创建的工程中编写代码,实现功能
4 具体实现流程代码如下:
using OfficeOpenXml;
using OfficeOpenXml.Drawing;
using OfficeOpenXml.Drawing.Chart;
using OfficeOpenXml.Style;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace EpplusTest
{
public class Program
{
static void Main(string[] args)
{
FileInfo newFile = new FileInfo(@"d:\test.xlsx");
if (newFile.Exists)
{
newFile.Delete();
newFile = new FileInfo(@"d:\test.xlsx");
} using (ExcelPackage package = new ExcelPackage(newFile))
{
#region 创建多个Sheet页
for (int i = ; i < ; i++)
{
package.Workbook.Worksheets.Add("Demo" + i);
}
ExcelWorksheet worksheet = package.Workbook.Worksheets["Demo0"];
ExcelWorksheet worksheet1 = package.Workbook.Worksheets["Demo1"]; #endregion #region 1 模拟填充数据
worksheet1.Cells[, ].Value = "名称";
worksheet1.Cells[, ].Value = "价格";
worksheet1.Cells[, ].Value = "销量"; worksheet1.Cells[, ].Value = "苹果";
worksheet1.Cells[, ].Value = ;
worksheet1.Cells[, ].Value = ; worksheet1.Cells[, ].Value = "华为";
worksheet1.Cells[, ].Value = ;
worksheet1.Cells[, ].Value = ; worksheet1.Cells[, ].Value = "小米";
worksheet1.Cells[, ].Value = ;
worksheet1.Cells[, ].Value = ; worksheet1.Cells[, ].Value = "OPPO";
worksheet1.Cells[, ].Value = ;
worksheet1.Cells[, ].Value = ;
#endregion #region 2 构造图表
worksheet.Cells.Style.WrapText = true;
worksheet.View.ShowGridLines = false;//去掉sheet的网格线
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(, , ));
} worksheet1.Cells[, ].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(, , ));
worksheet1.Cells[, ].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(, , ));
worksheet1.Cells[, ].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(, , )); worksheet1.Cells[, ].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(, , ));
worksheet1.Cells[, ].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(, , ));
worksheet1.Cells[, ].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(, , )); worksheet1.Cells[, ].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(, , ));
worksheet1.Cells[, ].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(, , ));
worksheet1.Cells[, ].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(, , )); worksheet1.Cells[, ].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(, , ));
worksheet1.Cells[, ].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(, , ));
worksheet1.Cells[, ].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(, , )); worksheet1.Cells[, ].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(, , ));
worksheet1.Cells[, ].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(, , ));
worksheet1.Cells[, ].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(, , )); ExcelChart chart = worksheet.Drawings.AddChart("chart", eChartType.ColumnClustered);
ExcelChartSerie serie = chart.Series.Add(worksheet1.Cells[, , , ], worksheet1.Cells[, , , ]);//引用worksheet1的数据填充图表的X轴和Y轴
serie.HeaderAddress = worksheet1.Cells[, ];
#endregion #region 3 设置图表的样式
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.SystemDash;
chart.Legend.Border.Fill.Color = Color.FromArgb(, , );
#endregion
package.Save();
}
}
}
}
5 效果展示:
6 Demo源码GitHub下载地址:https://github.com/HJ520134/.NetDemo.git