1

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

bornander
  • 751
  • 8
  • 20

2 Answers2

5

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.

ssabbattini
  • 168
  • 4
  • 8
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.

Han
  • 3,272
  • 3
  • 24
  • 39