是否可以在epplus中创建动态图表?

问题描述 投票:0回答:1

我想知道是否可以通过以下方式配置epplus:打开excel文件时,可以单击表格数据,并根据所单击表格的行显示图形。 (我意识到这在excel中非常容易做到,我宁愿一切都交给某些人来解决)

目前,我只为每行提供一个数据表和一个图形,但是最好仅根据在excel中单击的行更改一个图形。我还尝试了数据透视表,但这对动态图表没有帮助。

asp.net-core epplus
1个回答
0
投票

对于任何试图弄清楚这一点的人。我最终使用了数据验证下拉列表,并创建了动态图表所基于的动态表行(在基表之外)。动态表行根据数据验证下拉列表的值而变化(您有点需要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());
    } 
}

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.