EPPlus graph in separate sheet

回眸只為那壹抹淺笑 提交于 2019-12-08 06:38:33

问题


How do I create a graph as a separate worksheet rather than as a drawing in an existing worksheet using EPPLus?


回答1:


In version 4.0.4.0 (download the code from codeplex, for correct a bug in saving file), you can use:

workbook.Worksheets.AddChart(name, type);

this will create a sheet with only the chart.




回答2:


You should do what you want. That is the chart you want to plot, plot it in the sheet you want.

ExcelPackage pck = new ExcelPackage();
ExcelRange r1, r2;

var sheet1 = pck.Workbook.Worksheets.Add("data_sheet");
var sheet2 = pck.Workbook.Worksheets.Add("chart_sheet");
var chart = (OfficeOpenXml.Drawing.Chart.ExcelBarChart)sheet2.Drawings.AddChart("some_name", OfficeOpenXml.Drawing.Chart.eChartType.ColumnClustered);
chart.Legend.Position = OfficeOpenXml.Drawing.Chart.eLegendPosition.Right;
chart.Legend.Add();
chart.SetPosition(1, 0, 1, 0);
chart.SetSize(600, 400);
chart.DataLabel.ShowValue = true;

r1 = sheet1.Cells["A3:A10"];
r2 = sheet1.Cells["B3:B10"];
chart.Series.Add(r2, r1);

chart.Style = OfficeOpenXml.Drawing.Chart.eChartStyle.Style21;
chart.Title.Text = "Some title";
chart.XAxis.Title.Text = "X axis name";
chart.YAxis.Title.Text = "Y axis name";

In this example, chart is plotted in sheet2 but data is in sheet1. Hope this is helpful.



来源:https://stackoverflow.com/questions/12686552/epplus-graph-in-separate-sheet

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!