来自单个 excel 单元格列表的 EPPlus 图表。如何?

EPPlus chart from list of single excel cells. How?

我想使用 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

那么,有可能吗?如果是这样,语法是什么?

以逗号分隔的单元格名称行是您想要的格式。您是否同时指定了 x 和 y 轴范围?也许在您实际添加系列的地方显示您的代码。

您应该使用 ExcelRange 而不是地址。像这样:

[TestMethod]
public void Chart_From_Cells_Test()
{
    //
    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();
    }
}