问题
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