epplus 可以创建动态图表吗?

Is it possible to create dynamic charts in epplus?

我想知道是否可以将 epplus 配置为在打开 excel 文件时可以点击 table 数据并根据单击了 table 行。 (我意识到这在 excel 中非常容易做到,我宁愿在它到达某些人之前把所有事情都处理好)

目前我只有一个数据 table 和每一行的图表,但如果只有一个图表根据在 excel 中单击的行而改变会更好。我也尝试了一个枢轴 table 但这对动态图表没有帮助。

对于任何试图解决这个问题的人。我最终使用了数据验证下拉列表并制作了动态图表所基于的动态 table 行(在基础 table 之外)。动态 table 行根据数据验证下拉列表的值进行更改(您需要对 excel 有所了解才能做到这一点,而我没有):

class Program
{
    static void Main(string[] args)
    {
        // Creating an instance 
        // of ExcelPackage 
        ExcelPackage excel = new ExcelPackage();

        // name of the sheet 
        var workSheet = excel.Workbook.Worksheets.Add("testSheet");
        //init table
        var randTable = new DataTable();
        randTable.TableName = "randTable";
        //init columns
        var countColumn = new DataColumn()
        {
            DataType = typeof(int),
            ColumnName = "Count",
            ReadOnly = true,
            Unique = true
        };
        var randomColumn0 = new DataColumn()
        {
            DataType = typeof(int),
            ColumnName = "Random-0",
            ReadOnly = true,
            Unique = false
        };
        var randomColumn1 = new DataColumn()
        {
            DataType = typeof(int),
            ColumnName = "Random-1",
            ReadOnly = true,
            Unique = false
        };
        //add columns to table
        randTable.Columns.AddRange(new DataColumn[] { countColumn, randomColumn0, randomColumn1 });


        //init data validation
        ExcelRange dropdownRange = workSheet.Cells[12, 1, 12, 3];
        var dropdownValidation = workSheet.DataValidations.AddListValidation(dropdownRange.Address);
        workSheet.Names.Add("count", dropdownRange);
        //style data validation
        dropdownRange.Merge = true;
        workSheet.Cells[dropdownRange.Address].Style.Fill.PatternType = ExcelFillStyle.Solid;
        workSheet.Cells[dropdownRange.Address].Style.Fill.BackgroundColor.SetColor(Color.Yellow);
        workSheet.Cells[dropdownRange.Address].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;

        var rand = new Random();
        for (var i = 0; i < 10; i++)
        {
            //add table first column values to validation list
            dropdownValidation.Formula.Values.Add(i.ToString());
            var row = randTable.NewRow();
            row[countColumn] = i;
            row[randomColumn0] = rand.Next(0, 100);
            row[randomColumn1] = rand.Next(0, 100);
            randTable.Rows.Add(row);
        }

        //make the tableIndexer cell. This cell will be used to get the 
        //table indices for the selected table row cells
        ExcelRange randTableIndexer = workSheet.Cells[12, 4, 12, 4];
        randTableIndexer.Formula = "MATCH(INDEX(count,1,1), randTable[Count], 0)";
        workSheet.Cells[randTableIndexer.Address].Style.Fill.PatternType = ExcelFillStyle.Solid;
        workSheet.Cells[randTableIndexer.Address].Style.Fill.BackgroundColor.SetColor(Color.LightGreen);
        workSheet.Cells[randTableIndexer.Address].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
        workSheet.Names.Add("tableIndex", randTableIndexer);

        //make the cells based off the table at row(randTableIndexer.value)
        ExcelRange graphCells = workSheet.Cells[13, 1, 13, 3];
        graphCells.CreateArrayFormula("INDEX(randTable[], tableIndex, 0)"); //need [] on table names in epplus formulas
        workSheet.Cells[graphCells.Address].Style.Fill.PatternType = ExcelFillStyle.Solid;
        workSheet.Cells[graphCells.Address].Style.Fill.BackgroundColor.SetColor(Color.LightBlue);
        workSheet.Cells[graphCells.Address].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
        graphCells.Calculate();

        //add table to workSheet
        workSheet.Cells[1, 1].LoadFromDataTable(randTable, true, OfficeOpenXml.Table.TableStyles.Medium15);
        workSheet.Cells.AutoFitColumns();


        //add dynamic chart
        var chart = workSheet.Drawings.AddChart("rands", eChartType.Pie) as ExcelPieChart;
        chart.Title.Text = "rands";
        chart.Series.Add(graphCells.Address, ExcelRange.GetAddress(1, 1, 3, 1));
        chart.Legend.Position = eLegendPosition.Bottom;
        chart.SetSize(500, 400);
        chart.SetPosition(60, 500);


        WriteToFile(excel);
    }

    public static void WriteToFile(ExcelPackage package)
    {
        // file name with .xlsx extension  
        string p_strPath = "C:\your\file\path";

        if (File.Exists(p_strPath))
            File.Delete(p_strPath);

        // Create excel file on physical disk  
        FileStream objFileStrm = File.Create(p_strPath);
        objFileStrm.Close();

        // Write content to excel file  
        File.WriteAllBytes(p_strPath, package.GetAsByteArray());
    } 
}