c# - 如何使用 EPPLus 修改图表系列?

标签 c# excel charts epplus

我想问一下如何使用 EPPLUS 库更新 excel 中现有图表的系列值。 我还找不到怎么做。 我已成功创建图表,但仍然无法更新系列值。

谁能给我一些示例代码或引用链接?

最佳答案

很抱歉这么晚才回答(在搜索其他内容时看到您的帖子)。如果图表有一系列指向工作表中的单元格,为什么不能直接更新单元格值?像这样,首先创建饼图:

public void PieChartCreate()
{
    var file = new FileInfo(@"c:\temp\temp.xlsx");
    if (file.Exists)
        file.Delete();

    var pck = new ExcelPackage(file);
    var workbook = pck.Workbook;
    var worksheet = workbook.Worksheets.Add("newsheet");

    var data = new List<KeyValuePair<string, int>>
    {
        new KeyValuePair<string, int>("Group A", 44613),
        new KeyValuePair<string, int>("Group B", 36432),
        new KeyValuePair<string, int>("Group C", 6324),
        new KeyValuePair<string, int>("Group A", 6745),
        new KeyValuePair<string, int>("Group B", 23434),
        new KeyValuePair<string, int>("Group C", 5123),
        new KeyValuePair<string, int>("Group A", 34545),
        new KeyValuePair<string, int>("Group B", 5472),
        new KeyValuePair<string, int>("Group C", 45637),
        new KeyValuePair<string, int>("Group A", 37840),
        new KeyValuePair<string, int>("Group B", 20827),
        new KeyValuePair<string, int>("Group C", 4548),
    };

    //Fill the table
    var startCell = worksheet.Cells[1,1];
    startCell.Offset(0, 0).Value = "Group Name";
    startCell.Offset(0, 1).Value = "Value";

    for (var i = 0; i < data.Count(); i++)
    {
        startCell.Offset(i + 1, 0).Value = data[i].Key;
        startCell.Offset(i + 1, 1).Value = data[i].Value;
    }

    //Add the chart to the sheet
    var pieChart = worksheet.Drawings.AddChart("Chart1", eChartType.Pie);
    pieChart.SetPosition(data.Count + 1, 0, 0, 0);
    pieChart.Title.Text = "Test Chart";
    pieChart.Title.Font.Bold = true;
    pieChart.Title.Font.Size = 12;

    //Set the data range
    var series = pieChart.Series.Add(worksheet.Cells[2, 2, data.Count, 2], worksheet.Cells[2, 1, data.Count, 1]);
    var pieSeries = (ExcelPieChartSerie)series;
    pieSeries.Explosion = 5;

    //Format the labels
    pieSeries.DataLabel.Font.Bold = true;
    pieSeries.DataLabel.ShowValue = true;
    pieSeries.DataLabel.ShowPercent = true;
    pieSeries.DataLabel.ShowLeaderLines = true;
    pieSeries.DataLabel.Separator = ";";
    pieSeries.DataLabel.Position = eLabelPosition.BestFit;

    //Format the legend
    pieChart.Legend.Add();
    pieChart.Legend.Border.Width = 0;
    pieChart.Legend.Font.Size = 12;
    pieChart.Legend.Font.Bold = true;
    pieChart.Legend.Position = eLegendPosition.Right;

    pck.Save();

}

然后更新单元格:

public void PieChartUpdate()
{
    var file = new FileInfo(@"c:\temp\temp.xlsx");
    if (!file.Exists)
        return;

    var pck = new ExcelPackage(file);
    var workbook = pck.Workbook;
    var worksheet = workbook.Worksheets["newsheet"];

    var startCell = worksheet.Cells[2, 2];
    for (var i = 0; i < 12; i++)
    {
        startCell.Offset(i, 0).Value = ((double)startCell.Offset(i, 0).Value) * 100;
    }
    file.Delete();
    pck.Save();
}

如果你需要改变系列本身的定义,那么通过名称获取:

public void PieChartUpdateSeries()
{
    var file = new FileInfo(@"c:\temp\temp.xlsx");
    if (!file.Exists)
        return;

    var pck = new ExcelPackage(file);
    var workbook = pck.Workbook;
    var worksheet = workbook.Worksheets["newsheet"];

    var piechart = worksheet.Drawings["Chart1"] as ExcelPieChart;
    var series = piechart.Series[0];
    series.XSeries = worksheet.Cells[2, 1, 7, 1].FullAddress;
    series.Series = worksheet.Cells[2, 2, 7, 2].FullAddress;

    file.Delete();
    pck.Save();
}

希望对您有所帮助。

关于c# - 如何使用 EPPLus 修改图表系列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24637156/

相关文章:

c# - .Where(filter expression).FirstOrDefault() 和 .FirstOrDefault(filter expression) 的区别

c# - 从子域获取域

c# - 尝试在字符串中查找单个字符的更快方法是什么?

c# - 是否可以在未安装 excel 的计算机上将 excel 文件中的信息提取到 c# 中?

javascript - 滚动到该部分时如何使 Chart.js 动画?

javascript - HighCharts:在一个系列中绘制多个段?

c# - .Net Core 1.1 上的 OData v4 缺失/$元数据

excel - 不能从另一个单元格中减去一个单元格的文本

VBA - 如何将选定的范围传递给 TextBox 作为引用?

excel - VBA 图表问题