单个Excel单元格列表中的EPPlus图表。怎么样? [英] EPPlus chart from list of single excel cells. How?
本文介绍了单个Excel单元格列表中的EPPlus图表。怎么样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用EPPlus从单个单元格列表中制作一张excel图表。假设我想要一个三块的馅饼,它们从单元格C4,C6和C8中获取其值。怎么样?
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?
这两次尝试均无效:
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?
推荐答案
以逗号分隔的单元格名称行是您想要的格式。您是否同时指定了x和y轴范围?也许在实际添加系列的地方显示代码。
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.
您应该使用 ExcelRange
而不是Address。像这样:
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();
}
}
这篇关于单个Excel单元格列表中的EPPlus图表。怎么样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文