本文介绍了选择的EPPlus图表(饼图,barchart)(B2,B36,B38)等Excel单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与下面的问题类似。





我将其作为问题发布在他们的Codeplex页面上,以查看是否他们可以在下一个版本中修复它。


I have similar to the link below problem.

EPPlus chart from list of single excel cells. How?

I tried the code but it shows it twice in the chart. For example:

This code show excel chart -> select data-> horizontal(category) axis labels tab you show 100,100,300,600 write. What is the reason for this? The chart is written twice the first data I did not find a solution to the problem.

解决方案

I think you just discovered a bug with EPPlus. Shame on me for not noticing that with that post you reference. It seems that when using the Excel union range selector (the cell names separated by commas) the iterator for the ExcelRange class returns a double reference to the first cell, in this case B2.

A work around would be to use the other overload for Series.Add which will take two string ranges. Here is a unit test that show the problem and the workaround:

[TestMethod]
public void Chart_From_Cell_Union_Selector_Bug_Test()
{
    var existingFile = new FileInfo(@"c:\temp\Chart_From_Cell_Union_Selector_Bug_Test.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;

        //Pie chart shows with EXTRA B2 entry due to problem with ExcelRange Enumerator
        ExcelRange values = myWorkSheet.Cells["B2,B4,B6"];  //when the iterator is evaluated it will return the first cell twice: "B2,B2,B4,B6"
        ExcelRange xvalues = myWorkSheet.Cells["A2,A4,A6"]; //when the iterator is evaluated it will return the first cell twice: "A2,A2,A4,A6"
        var chartBug = ExcelWorksheet.Drawings.AddChart("Chart BAD", eChartType.Pie);
        chartBug.Series.Add(values, xvalues);
        chartBug.Title.Text = "Using ExcelRange";

        //Pie chart shows correctly when using string addresses and avoiding ExcelRange
        var chartGood = ExcelWorksheet.Drawings.AddChart("Chart GOOD", eChartType.Pie);
        chartGood.SetPosition(10, 0, 0, 0);
        chartGood.Series.Add("Content!B2,Content!B4,Content!B6", "Content!A2,Content!A4,Content!A6");
        chartGood.Title.Text = "Using String References";

        pck.Save();
    }
}

Here is the output:

I will post it as an issue on their codeplex page to see if they can get it fixed for the next release.

这篇关于选择的EPPlus图表(饼图,barchart)(B2,B36,B38)等Excel单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 23:35