none
Open XML Excel Charts RRS feed

  • Question

  • im refering this msdn link and when i called the FixChartData method ChartsheetParts gets null. So that it will give me an exception called "Sequence contains no elements" from

     

    ChartPart chartPart = workbookPart.ChartsheetParts.First().DrawingsPart.ChartParts.First();

    Plz can anybody give me a solution for this?

    Sunday, June 12, 2011 11:32 AM

Answers

  • Hi Hasi123,

    I think your Excel file is different with the sample in the article, you need to get the ChartPart correctly through judging whether DrawingsPart is null, hope the following code snippet can help you:

     public static void FixChartData(WorkbookPart workbookPart, int totalCount)
            {
                //Get the appropriate chart part from template file.
                var wsparts = workbookPart.WorksheetParts.ToArray();
     
                foreach (WorksheetPart wsp in wsparts)
                {
                    if (wsp.DrawingsPart != null)
                    {
                        ChartPart chartPart = wsp.DrawingsPart.ChartParts.First();
     
                        //Change the ranges to accomodate the newly inserted data.
                        foreach (DocumentFormat.OpenXml.Drawing.Charts.Formula formula in chartPart.ChartSpace.Descendants<DocumentFormat.OpenXml.Drawing.Charts.Formula>())
                        {
                            if (formula.Text.Contains("$2"))
                            {
                                string s = formula.Text.Split('$')[1];
                                formula.Text += ":$" + s + "$" + totalCount;
                            }
                        }
                        chartPart.ChartSpace.Save();
                    }
                }              
            }

    If you still have any problems, just feel free to follow up.

    Best Regards,

     


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Bruce Song Friday, June 24, 2011 1:12 PM
    Thursday, June 16, 2011 10:00 AM

All replies

  • im refering this msdn link and when i called the FixChartData method ChartsheetParts gets null. So that it will give me an exception called "Sequence contains no elements"

    ChartPart chartPart = workbookPart.ChartsheetParts.First().DrawingsPart.ChartParts.First();

    Plz can anybody help me on this???

    • Moved by Cookie Luo Tuesday, June 14, 2011 8:31 AM (From:Visual C# General)
    • Merged by Bruce Song Friday, June 17, 2011 10:31 AM duplicate
    Sunday, June 12, 2011 11:37 AM
  • Sunday, June 12, 2011 11:39 AM
  • Sunday, June 12, 2011 11:40 AM
  • Hi Hasi123,

    I think your Excel file is different with the sample in the article, you need to get the ChartPart correctly through judging whether DrawingsPart is null, hope the following code snippet can help you:

     public static void FixChartData(WorkbookPart workbookPart, int totalCount)
            {
                //Get the appropriate chart part from template file.
                var wsparts = workbookPart.WorksheetParts.ToArray();
     
                foreach (WorksheetPart wsp in wsparts)
                {
                    if (wsp.DrawingsPart != null)
                    {
                        ChartPart chartPart = wsp.DrawingsPart.ChartParts.First();
     
                        //Change the ranges to accomodate the newly inserted data.
                        foreach (DocumentFormat.OpenXml.Drawing.Charts.Formula formula in chartPart.ChartSpace.Descendants<DocumentFormat.OpenXml.Drawing.Charts.Formula>())
                        {
                            if (formula.Text.Contains("$2"))
                            {
                                string s = formula.Text.Split('$')[1];
                                formula.Text += ":$" + s + "$" + totalCount;
                            }
                        }
                        chartPart.ChartSpace.Save();
                    }
                }              
            }

    If you still have any problems, just feel free to follow up.

    Best Regards,

     


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Bruce Song Friday, June 24, 2011 1:12 PM
    Thursday, June 16, 2011 10:00 AM