我想从单个单元格列表中使用EPPlus制作excel图表。假设我想要一个有三个部分的馅饼从C4,C6和C8单元格中获取它们的值。怎么样?
这两次尝试都是不起作用的:
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
那么,有可能吗?如果是这样,语法是什么?
答案 0 :(得分:2)
带逗号分隔的单元格名称的行是您想要的格式。您是否同时指定了x轴和y轴范围?也许在你实际添加系列的地方显示你的代码。
您应该使用ExcelRange
而不是地址。像这样:
[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();
}
}