How to create chart in excel c#


Could you call Chart.Calculate() before evaluating DataLabels.Text value if it makes any difference.

I used code

                          for (int i = 0; i < series.Points.Count; i++)
                                    string _str = series.Points[i].DataLabels.Text;

but _str is null


As percentage value (to be shown) on the chart is automatic so it won’t give you the data labels all the times. I think you may try the following workaround (you need to pick the source data in the cells and compare) to cope with your issue, see the following sample code. I have also attached the template file for your reference:
Sample code:

   Workbook workbook = new Workbook("e:\\test2\\Bk_datalabels1.xlsx");
    Worksheet worksheet = workbook.Worksheets[0];
    Chart chart = worksheet.Charts[0];


    Series series = chart.NSeries[0];
    string cellsArea = series.Values;

    Range range = worksheet.Cells.CreateRange(cellsArea);

     for (int i = range.FirstRow; i < range.RowCount + range.FirstRow; i++)
       for (int j = range.FirstColumn; j < range.ColumnCount + range.FirstColumn; j++)
        string _str = worksheet.Cells[i, j].StringValue;
        if (_str == "0") 
            series.Points[i].DataLabels.ShowPercentage = false;



Hope, this helps a bit. (10.7 KB)

Thank you, Amjad_Sahi. This works for me


Good to know that the suggested code works for your needs. Feel free to contact us any time if you need further help or have some other issue or queries, we will be happy to assist you soon.