I want to make an excel chart, using EPPlus, from a list of single cells. Say I want a pie with three pieces getting their values from cells C4, C6 and C8. How?
These two attempts is among those that does not work:
ExcelChart chart = ExcelWorksheet.Drawings.AddChart(myTitle, eChartType.Pie);
ExcelAddress values = myWorkSheet.Cells["C4;C6;C8"]; // => 'Invalid Address format'
ExcelAddress values = myWorkSheet.Cells["C4,C6,C8"]; // => No chart is made
So, is it possible? If so, what's the syntax?
The line with cell names separated by commas is the format you want. Did you specify both the x and y axis range? Maybe show your code where you actually add the series.
You should be using ExcelRange
not Address. Like this:
[TestMethod]
public void Chart_From_Cells_Test()
{
//http://stackoverflow.com/questions/29207020/epplus-chart-from-list-of-single-excel-cells-how
var existingFile = new FileInfo(@"c:\temp\temp.xlsx");
if (existingFile.Exists)
existingFile.Delete();
using (var pck = new ExcelPackage(existingFile))
{
var myWorkSheet = pck.Workbook.Worksheets.Add("Content");
var ExcelWorksheet = pck.Workbook.Worksheets.Add("Chart");
//Some data
myWorkSheet.Cells["A1"].Value = "A";
myWorkSheet.Cells["A2"].Value = 100; myWorkSheet.Cells["A3"].Value = 400; myWorkSheet.Cells["A4"].Value = 200; myWorkSheet.Cells["A5"].Value = 300; myWorkSheet.Cells["A6"].Value = 600; myWorkSheet.Cells["A7"].Value = 500;
myWorkSheet.Cells["B1"].Value = "B";
myWorkSheet.Cells["B2"].Value = 300; myWorkSheet.Cells["B3"].Value = 200; myWorkSheet.Cells["B4"].Value = 1000; myWorkSheet.Cells["B5"].Value = 600; myWorkSheet.Cells["B6"].Value = 500; myWorkSheet.Cells["B7"].Value = 200;
ExcelRange values = myWorkSheet.Cells["B2,B4,B6"];
ExcelRange xvalues = myWorkSheet.Cells["A2,A4,A6"];
const string myTitle = "Chart 1";
ExcelChart chart1 = ExcelWorksheet.Drawings.AddChart(myTitle, eChartType.Pie);
chart1.Series.Add(values, xvalues);
pck.Save();
}
}
来源:https://stackoverflow.com/questions/29207020/epplus-chart-from-list-of-single-excel-cells-how