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

35 篇文章 0 订阅
29 篇文章 0 订阅

     导出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[1, 1].Value = "名称";
	worksheet.Cells[1, 2].Value = "价格";
	worksheet.Cells[1, 3].Value = "销量";

	worksheet.Cells[2, 1].Value = "大米";
	worksheet.Cells[2, 2].Value = 56;
	worksheet.Cells[2, 3].Value = 100;

	worksheet.Cells[3, 1].Value = "玉米";
	worksheet.Cells[3, 2].Value = 45;
	worksheet.Cells[3, 3].Value = 150;

	worksheet.Cells[4, 1].Value = "小米";
	worksheet.Cells[4, 2].Value = 38;
	worksheet.Cells[4, 3].Value = 130;

	worksheet.Cells[5, 1].Value = "糯米";
	worksheet.Cells[5, 2].Value = 22;
	worksheet.Cells[5, 3].Value = 200;

	using (ExcelRange range = worksheet.Cells[1, 1, 5, 3])
	{
		range.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
		range.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
	}

	using (ExcelRange range = worksheet.Cells[1, 1, 1, 3])
	{
		range.Style.Font.Bold = true;
		range.Style.Font.Color.SetColor(Color.White);
		range.Style.Font.Name = "微软雅黑";
		range.Style.Font.Size = 12;
		range.Style.Fill.PatternType = ExcelFillStyle.Solid;
		range.Style.Fill.BackgroundColor.SetColor(Color.FromArgb(128, 128, 128));
	}

	worksheet.Cells[1, 1].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191));
	worksheet.Cells[1, 2].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191));
	worksheet.Cells[1, 3].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191));

	worksheet.Cells[2, 1].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191));
	worksheet.Cells[2, 2].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191));
	worksheet.Cells[2, 3].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191));

	worksheet.Cells[3, 1].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191));
	worksheet.Cells[3, 2].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191));
	worksheet.Cells[3, 3].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191));

	worksheet.Cells[4, 1].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191));
	worksheet.Cells[4, 2].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191));
	worksheet.Cells[4, 3].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191));

	worksheet.Cells[5, 1].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191));
	worksheet.Cells[5, 2].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191));
	worksheet.Cells[5, 3].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191));

	ExcelChart chart = worksheet.Drawings.AddChart("chart", eChartType.ColumnClustered);

	ExcelChartSerie serie = chart.Series.Add(worksheet.Cells[2, 3, 5, 3], worksheet.Cells[2, 1, 5, 1]);
	serie.HeaderAddress = worksheet.Cells[1, 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);

	package.Save();
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Epplus 简介:Epplus是一个使用Open Office XML(Xlsx)文件格式,能读写Excel 2007/2010文件的开源组件 功效:支持对excel文档的汇入汇出,图表excel自带的图表基本都可以实现)的列印 使用:首先应该下载Epplus的dll文件 1> 添加dll文件至工程bin文件中 2>在程式中添加引用 using OfficeOpenXml; using OfficeOpenXml.Drawing; using OfficeOpenXml.Drawing.Chart; using OfficeOpenXml.Style; 3>所有的操作语句需要放置在下面的using中 using (ExcelPackage package = new ExcelPackage()) { } 4.添加新的sheet var worksheet = package.Workbook.Worksheets.Add(“sheet1"); 5.单元格赋值,这里多说一句,NPOI必须先创建单元格,然后再给单元格赋值,而Epplus不需要,直接找到单元格进行赋值就可以了. worksheet.Cells[int row, int col].Value = “”; 或者 worksheet.Cells["A1"].Value = “”; 6.合并单元格 worksheet.Cells[int fromRow, fromCol, int toRow,int toCol].Merge = true; 7.获取某一个区域 var rangeData= worksheet.Cells[fromRow, fromCol, toRow, toCol]; 8.设置字体 worksheet.Cells.Style.Font.Name= “正楷”; worksheet.Cells.Style.Font.Color worksheet.Cells.Style.Font.Size 9.设置边框的属性 worksheet.Cells.Style.Border.Left.Style= ExcelBorderStyle.Thin ; worksheet.Cells.Style.Border.Right.Style= ExcelBorderStyle.Thin; worksheet.Cells.Style.Border.Top.Style= ExcelBorderStyle.Thin; worksheet.Cells.Style.Border.Bottom.Style= ExcelBorderStyle.Thin; 10.对齐方式 worksheet.Cells.Style.HorizontalAlignment=ExcelHorizontalAlignment.Center; worksheet.Cells.Style.VerticalAlignment = ExcelVerticalAlignment.Bottom; 11. 设置整个sheet的背景色 worksheet.Cells.Style.Fill.PatternType= ExcelFillStyle.Solid; worksheet.Cells.Style.Fill.BackgroundColor.SetColor(Color.LightBlue); 12.折行显示 worksheet.Cells.Style.WrapText= true; 13.单元格自动适应大小 worksheet.Cells.Style.ShrinkToFit= true; 14.格式化单元格value值 worksheet.Cells.Style.Numberformat.Format= "0.00"; 15.锁定 worksheet.Cells["A1"].Style.Locked= true; 注:此处锁定某一个单元格的时候,只有在整个sheet被锁定的情况下才可以被锁定,不然加上锁定属性也是不起作用的~~ 二.Epplus另一个出色的地方就是支持图表的列印.功能的實現很簡單,難點在于需求比較細的點上,epplus可能不好實現,但是總的來說是比較好的一個列印圖表的工具 1.简单介绍一下可以实现的图表类型: 直條圖、折綫圖、圓形圖、橫條圖、散佈圖、區域圖 等類型的圖表 2.使用:分为三步, 第一步是将需要显示在图表中的 数据列印到excel中. 第二步是创建所需要的图表类型(折线图为例) var chart = (worksheet.Drawings.AddChart("LineChart", eChartType.Line) as ExcelLineChart); 第三步为图表添加第一步列印的数据区间就可以了 chart.Series.Add(Y軸顯示的數據源,X軸顯示的數據源) 3.图表的功能就这样实现了,很简单吧
很高兴为您解答这个".net mvc epplus导出excel添加图表"的问题。首先,您需要使用EPPlus导出Excel文件,并添加一个图表。您可以按照以下步骤进行操作: 1. 创建一个ExcelPackage对象,将数据添加到工作表中。 2. 创建一个Chart对象,并将其添加到工作表中。 3. 设置图表类型以及图表的数据范围。 4. 将图表添加到单元格中,并设置图表的位置和大小。 下面是一份ASP.NET MVC中使用EPPlus添加图表的示例代码: ``` public ActionResult ExportExcel() { ExcelPackage.LicenseContext = LicenseContext.NonCommercial; ExcelPackage pck = new ExcelPackage(); var ws = pck.Workbook.Worksheets.Add("Sample"); // Add some data ws.Cells["A1"].Value = "Sample Excel With Chart"; ws.Cells["A2"].Value = "Hello"; ws.Cells["B2"].Value = "World!"; ws.Cells["A3"].Value = 100; ws.Cells["B3"].Value = 200; // Create the chart var chart = ws.Drawings.AddChart("Chart", eChartType.Line); chart.SetPosition(1, 0, 4, 0); chart.SetSize(600, 400); var series = chart.Series.Add("B3:B4", "A3:A4"); chart.Title.Text = "Sample Chart"; chart.Legend.Remove(); // Stream the Excel package to the client MemoryStream stream = new MemoryStream(); pck.SaveAs(stream); string fileName = "SampleExcelWithChart.xlsx"; string mimeType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; return File(stream.ToArray(), mimeType, fileName); } ``` 在这个示例代码中,我们创建了一个名为"Sample"的工作表,并在它的A1、A2、B2、A3和B3单元格中添加了一些数据。接下来,我们创建了一个Line类型的图表,并将其添加到工作表中。我们使用SetPosition和SetSize方法设置图表在工作表中的位置和大小。接着,我们添加了一个数据系列到图表中,并设置了它的标题和图例。最后,我们将Excel文件以流的形式返回给客户端。 感谢您对ChitGPT的提问,希望我的回答能对您有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值