我如何使用EPPLus修改图表系列? [英] How do I modify a chart series using EPPLus?

查看:895
本文介绍了我如何使用EPPLus修改图表系列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想请教一下如何更新使用EPPLUS图书馆现有图表在Excel中的序列值。
我找不到还没有如何做到这一点。
我已经创建的图表成功,但仍然无法更新序列值。

I want to ask about how to update series value of existing chart in excel using EPPLUS library. I can't find yet how to do it. I have created chart successfully, but still can't to update the series value.

任何人都可以给我一些示例代码或引用链接?
谢谢

Anyone can provide me some sample code or reference link? Thank you

推荐答案

不好意思回答这么晚(翻过来到您的文章,同时寻找别的东西)。如果图中有一系列的被指向细胞在工作表中,为什么不能你刚才直接更新单元格的值?与此类似,首先创建饼图:

Sorry to answer so late (came accross your post while searching for something else). If the chart has a series which are pointed at cells in a worksheet, why cant you just update the cell values directly? Like this, first create the pie chart:

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();

}



然后更新单元格:

Then update the cells:

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();
}

如果您需要更改的在定义系列本身,然后按名称得到它:

If you need to change the definition of the series itself, then get it by name:

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();
}



希望有所帮助。

Hope that helps.

这篇关于我如何使用EPPLus修改图表系列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆