2

Hello fellow developers,

Being a newbie with the OpenXML SDK I cannot figure out how to retrieve a graph part that I've put in a rich text control (with a specific tag name).

For the moment I retrieve the graph part by using the mainDocumentPart.ChartParts collection. But a ChartPart object does not seem to know where it's located in the document: chartPart.GetParentParts() only contains the mainDocumentPart.

I have multiple graphs in my document, so how can I distinguish them? I have put my graphs in rich text controls, so I thought I could access them like that, but I cannot figure out how to do this. Retrieving the rich text control works, but how to find the graph within it?

        foreach (SdtProperties sdtProp in mainDocumentPart.Document.Body.Descendants<SdtProperties>())
        {
            Tag tag = sdtProp.GetFirstChild<Tag>();

            if (tag != null && tag.Val != null)
            {
                if (tag.Val == "containerX")
                {
                    SdtProperties sdtPropTestResults = sdtProp;

                    // How to retrieve the graph part??
                    // sdtPropTestResults.Descendants<ChartPart> does not seem to work
                }
            }
        }

Thanks a lot for your help.

4

1 回答 1

5

自己找到了解决方案。我现在不使用父容器。相反,我给图表空间一个“Alt Title”。现在我的代码查找具有给定标题的 docProperties 的绘图。

这里是:

// Find our graphs by looping all drawings in the document and comparing their "alt title" property
foreach (Drawing drawing in mainDocumentPart.Document.Body.Descendants<Drawing>())
{
    DocProperties docProperties = drawing.Descendants<DocumentFormat.OpenXml.Drawing.Wordprocessing.DocProperties>().FirstOrDefault();

    if (docProperties != null && docProperties.Title != null)
    {
        if (docProperties.Title.Value == AltTitleChartBlack || docProperties.Title.Value == AltTitleChartRed)
        {
            LineChartData lineChartData = null;
            switch (docProperties.Title.Value)
            {
                case AltTitleChartBlack:
                    lineChartData = this.chartDataBlack;
                    break;
                case AltTitleChartRed:
                    lineChartData = this.chartDataRed;
                    break;
            }

            ChartReference chartRef = drawing.Descendants<ChartReference>().FirstOrDefault();
            if (chartRef != null && chartRef.Id != null)
            {
                ChartPart chartPart = (ChartPart)mainDocumentPart.GetPartById(chartRef.Id);
                if (chartPart != null)
                {
                    Chart chart = chartPart.ChartSpace.Elements<Chart>().FirstOrDefault();
                    if (chart != null)
                    {

                        LineChart lineChart = chart.Descendants<LineChart>().FirstOrDefault();

                        if (lineChart != null)
                        {
                            LineChartEx chartEx = new LineChartEx(chartPart, lineChartData);
                            chartEx.Refresh();
                            chartPart.ChartSpace.Save();
                        }
                    }
                }
            }
        }
    }
}
于 2013-06-04T15:59:38.753 回答