使用EPPlus在Excel工作表中创建饼图 [英] Creating a pie chart in excel sheet using EPPlus

查看:224
本文介绍了使用EPPlus在Excel工作表中创建饼图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我在项目中安装了EPPLus库,我是新手。我想根据工作表中输入的数据创建一个饼图。但是我无法这样做。

这是我的代码:

< pre lang =   c# >  

ExcelPackage pck = ExcelPackage();

pck.Workbook.Properties.Title = 使用Excel的图表;
// pck.Workbook.Properties.Company =***;
// pck.Workbook.Properties.Author =***报告;

var ws = pck.Workbook.Worksheets.Add( ***);
var cell = ws.Cells [ A1\" ];
随机r = Random();
ws.View.ShowGridLines = false ;
var range = ws.Cells [ A];
var table = ws.Tables.First();
int datarows = table.Address.End.Row;

ws.Cells [ 1 1 ]。值= ;
ws.Cells [ 1 1 ]。值= ;
ws.Cells [ 1 2 ]。值= Student1;
ws.Cells [ 1 3 ]。值= Student2;
ws.Cells [ 1 4 ]。值= Student3;

ws.Cells [ 2 1 ]。值= Term1;
ws.Cells [ 2 2 ]。值= 80;
ws.Cells [ 2 3 ]。值= 65;
ws.Cells [ 2 4 ]。值= 45;

ws.Cells [ 3 1 ]。值= Term2;
ws.Cells [ 3 2 ]。值= 78;
ws.Cells [ 3 3 ]。值= 72;
ws.Cells [ 3 4 ]。Value = 60;

ws.Cells [ 4 1 ]。值= Term3;
ws.Cells [ 4 2 ]。值= 82;
ws.Cells [ 4 3 ]。值= 80;
ws.Cells [ 4 4 ]。值= 65;

ws.Cells [ 5 1 ]。值= Term4;
ws.Cells [ 5 2 ]。值= 75;
ws.Cells [ 5 3 ]。值= 82;
ws.Cells [ 5 4 ]。值= 68;
range = ws.SelectedRange;




OfficeOpenXml.Drawing.Chart.ExcelPieChart EC =(OfficeOpenXml.Drawing.Chart.ExcelPieChart)ws.Drawings.AddChart(<跨度类=代码-string>
chart_1,OfficeOpenXml.Drawing.Chart.eChartType.Pie3D);
ec.SetPosition( 1 0 3 0 );
ec.SetSize( 500 200 );
// ec.Legend.Add();
ec.Series。添加( A1 < span class =code-string> D5
);




(OfficeOpenXml.Drawing.Chart.ExcelPieChartSerie)ec.Series.Add(ran1,ran2);
// serie1.Header = ws.Cells [A1]。Value.ToString();

// ran1 = ws.Cells [B2:B101];
// var serie2 = ec.Series.Add(ran1,ran2);
// serie2.Header = ws.Cells [B1]。Value.ToString() ;



string path = @ D:\test1.xlsx;
File.WriteAllBytes(path,pck.GetAsByteArray());
pck.Dispose();

Console.WriteLine( 完成 - 路径:{0},路径);
Console.ReadLine();



我知道我失去了什么,我从代码plex的例子中尝试了很多但是我不确定是什么'是它的解决方案。此外,如何获得epplus的范围。

请帮助我。我将非常感谢你。

解决方案

EPPlus OfficeOpenXml 是两个不同的东西,尝试此页面的图表示例:

http:/ /epplus.codeplex.com/wikipage?title=StatisticsSheetExample [ ^ ]

Hi all,

I have installed EPPLus library in my project and I am new to it. I want to create a pie chart from the data entered in the sheet. However I am not able to do so.
Here is my code :

<pre lang="c#">
          
            ExcelPackage pck = new ExcelPackage();
            
            pck.Workbook.Properties.Title = "Charts With Excel";
            // pck.Workbook.Properties.Company = "***";
            //pck.Workbook.Properties.Author = "*** Reporting";

            var ws = pck.Workbook.Worksheets.Add("***");
            var cell = ws.Cells["A1"];
            Random r = new Random();
            ws.View.ShowGridLines = false;
            var range = ws.Cells["A"];
            var table = ws.Tables.First();
            int datarows = table.Address.End.Row;

            ws.Cells[1, 1].Value = "";
            ws.Cells[1, 1].Value = "";
            ws.Cells[1, 2].Value = "Student1";
            ws.Cells[1, 3].Value = "Student2";
            ws.Cells[1, 4].Value = "Student3";

            ws.Cells[2, 1].Value = "Term1";
            ws.Cells[2, 2].Value = "80";
            ws.Cells[2, 3].Value = "65";
            ws.Cells[2, 4].Value = "45";

            ws.Cells[3, 1].Value = "Term2";
            ws.Cells[3, 2].Value = "78";
            ws.Cells[3, 3].Value = "72";
            ws.Cells[3, 4].Value = "60";

            ws.Cells[4, 1].Value = "Term3";
            ws.Cells[4, 2].Value = "82";
            ws.Cells[4, 3].Value = "80";
            ws.Cells[4, 4].Value = "65";

            ws.Cells[5, 1].Value = "Term4";
            ws.Cells[5, 2].Value = "75";
            ws.Cells[5, 3].Value = "82";
            ws.Cells[5, 4].Value = "68";
            range = ws.SelectedRange;

            
            

            OfficeOpenXml.Drawing.Chart.ExcelPieChart  ec = (OfficeOpenXml.Drawing.Chart.ExcelPieChart)ws.Drawings.AddChart("chart_1", OfficeOpenXml.Drawing.Chart.eChartType.Pie3D);
            ec.SetPosition(1, 0, 3, 0);
            ec.SetSize(500, 200);
            //ec.Legend.Add();
            ec.Series.Add("A1", "D5");


                          

           (OfficeOpenXml.Drawing.Chart.ExcelPieChartSerie)ec.Series.Add(ran1, ran2);
            //serie1.Header = ws.Cells["A1"].Value.ToString();

            //ran1 = ws.Cells["B2:B101"];
            //var serie2 = ec.Series.Add(ran1, ran2);
            //serie2.Header = ws.Cells["B1"].Value.ToString();

           

            string path = @"D:\test1.xlsx";
            File.WriteAllBytes(path, pck.GetAsByteArray());
            pck.Dispose();

            Console.WriteLine("Done - Path: {0}", path);
            Console.ReadLine();


I know m missing smething and I have tried a lot finding from the examples of code plex but I am not sure what''s the solution to it. Moreover how to get range in epplus.
Please help me. I shall be highly thankgull to you.

解决方案

EPPlus and OfficeOpenXml are two different things, try this page for a charting sample:
http://epplus.codeplex.com/wikipage?title=StatisticsSheetExample[^]


这篇关于使用EPPlus在Excel工作表中创建饼图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆