如何向OpenXML创建的文档添加条形图

时间:2017-02-22 21:02:55

标签: openxml

我的C#应用​​程序使用OpenXML创建一个MSWord文档,其中已包含多个表格。最后一部分是添加条形图。我无法找到这个案例的好例子。

感谢您的帮助!

我正在从头开始创建文档。从:

开始

using(WordprocessingDocument myDoc = WordprocessingDocument.Create(documentStream,WordprocessingDocumentType.Document,autoSave:true))

然后我在C#代码中添加新的表和段落。一切正常,直到我到达条形图。我找到了一个将饼图插入word文档的示例项目,但我不了解图表类型之间的差异以进行转换。这是我发现的饼图示例项目:

https://code.msdn.microsoft.com/office/How-to-create-Chart-into-a7d424f6

感谢您的帮助!

2 个答案:

答案 0 :(得分:3)

要插入基本条形图,我不会查看饼图代码并尝试猜测如何将其转换为条形图。

更快的解决方案是使用Open XML Productivity Tool。它有一个功能,允许您打开2个文件,确定两个Open XML结构之间的差异,并将生成使第一个文件看起来像第二个文件所需的代码。

尝试这种方法:

  1. Install the Open XML Productivity Tool
  2. 保存您的Word文档(添加饼图片段之前的干净版本)并将其命名为NoBarChart.docx
  3. 复制NoBarChart.docx将其命名为WithBarChart.docx
  4. 使用Word打开WithBarChart.docx并添加条形图(Insert-> Chart-> Bar)。
  5. 使用颜色,格式等设计条形图样式,以满足您的终极解决方案并保存并关闭。
  6. 运行Open XML Productivity Tool并点击顶部的比较文件按钮。
  7. 选择NoBarChart.docx作为来源,WithBarChart.docx作为目标
  8. 单击文件中突出显示的不同部分,然后单击查看部件差异以查看XML中的差异。然后点击查看软件包代码,该工具将生成代码,使源看起来像目标。
  9. 检查它生成的代码,以获取有关如何添加条形图的想法。如果缺少某些代码,您可以使用该工具生成整个Target文件。

    指向简单WPF应用的链接,该应用生成带有条形图is on GitHub的空白文档。这是该文件的图片:

    enter image description here

    这种方法可用于生成大多数Word,Excel或PowerPoint文件及其提供的功能。

答案 1 :(得分:1)

我按照您遵循的教程创建了一个Piechart。然后,我设法进行了更改,以便将条形图插入到word文档中。 希望以下代码对您有所帮助。 我正在CreateBarChart函数中创建数据样本,以便在Barchart中创建列。

public static WordprocessingDocument CreateBarChart(WordprocessingDocument document)//List<ChartSubArea> chartList,
    {
        string title = "New Chart";

        Dictionary<string, int> data = new Dictionary<string, int>();
        data.Add("abc", 1);

        // Get MainDocumentPart of Document
        MainDocumentPart mainPart = document.AddMainDocumentPart();
        mainPart.Document = new Document(new Body());

        // Create ChartPart object in Word Document
        ChartPart chartPart = mainPart.AddNewPart<ChartPart>("rId110");

        // the root element of chartPart 
        dc.ChartSpace chartSpace = new dc.ChartSpace();
        chartSpace.Append(new dc.EditingLanguage() { Val = "en-us" });

        // Create Chart 
        dc.Chart chart = new dc.Chart();
        chart.Append(new dc.AutoTitleDeleted() { Val = true });

        // Define the 3D view
        dc.View3D view3D = new dc.View3D();
        view3D.Append(new dc.RotateX() { Val = 30 });
        view3D.Append(new dc.RotateY() { Val = 0 });

        // Intiliazes a new instance of the PlotArea class
        dc.PlotArea plotArea = new dc.PlotArea();
        BarChart barChart = plotArea.AppendChild<BarChart>(new BarChart(new BarDirection()
        { Val = new EnumValue<BarDirectionValues>(BarDirectionValues.Column) },
           new BarGrouping() { Val = new EnumValue<BarGroupingValues>(BarGroupingValues.Clustered) }));

        plotArea.Append(new dc.Layout());


        dc.ChartShapeProperties chartShapePros = new dc.ChartShapeProperties();

        uint i = 0;
        // Iterate through each key in the Dictionary collection and add the key to the chart Series
        // and add the corresponding value to the chart Values.
        foreach (string key in data.Keys)
        {
            BarChartSeries barChartSeries = barChart.AppendChild<BarChartSeries>(new BarChartSeries(new Index()
            {
                Val =
                new UInt32Value(i)
            },
                new Order() { Val = new UInt32Value(i) },
                new SeriesText(new NumericValue() { Text = key })));

            StringLiteral strLit = barChartSeries.AppendChild<CategoryAxisData>(new CategoryAxisData()).AppendChild<StringLiteral>(new StringLiteral());
            strLit.Append(new PointCount() { Val = new UInt32Value(1U) });
            strLit.AppendChild<StringPoint>(new StringPoint() { Index = new UInt32Value(0U) }).Append(new NumericValue(title));

            NumberLiteral numLit = barChartSeries.AppendChild<DocumentFormat.OpenXml.Drawing.Charts.Values>(
                new DocumentFormat.OpenXml.Drawing.Charts.Values()).AppendChild<NumberLiteral>(new NumberLiteral());
            numLit.Append(new FormatCode("General"));
            numLit.Append(new PointCount() { Val = new UInt32Value(1U) });
            numLit.AppendChild<NumericPoint>(new NumericPoint() { Index = new UInt32Value(0u) }).Append
            (new NumericValue(data[key].ToString()));

            i++;
        }

        barChart.Append(new AxisId() { Val = new UInt32Value(48650112u) });
        barChart.Append(new AxisId() { Val = new UInt32Value(48672768u) });

        // Add the Category Axis.
        CategoryAxis catAx = plotArea.AppendChild<CategoryAxis>(new CategoryAxis(new AxisId()
        { Val = new UInt32Value(48650112u) }, new Scaling(new Orientation()
        {
            Val = new EnumValue<DocumentFormat.
            OpenXml.Drawing.Charts.OrientationValues>(DocumentFormat.OpenXml.Drawing.Charts.OrientationValues.MinMax)
        }),
           new AxisPosition() { Val = new EnumValue<AxisPositionValues>(AxisPositionValues.Bottom) },
           new TickLabelPosition() { Val = new EnumValue<TickLabelPositionValues>(TickLabelPositionValues.NextTo) },
           new CrossingAxis() { Val = new UInt32Value(48672768U) },
           new Crosses() { Val = new EnumValue<CrossesValues>(CrossesValues.AutoZero) },
           new AutoLabeled() { Val = new BooleanValue(true) },
           new LabelAlignment() { Val = new EnumValue<LabelAlignmentValues>(LabelAlignmentValues.Center) },
           new LabelOffset() { Val = new UInt16Value((ushort)100) }));

        // Add the Value Axis.
        ValueAxis valAx = plotArea.AppendChild<ValueAxis>(new ValueAxis(new AxisId() { Val = new UInt32Value(48672768u) },
        new Scaling(new Orientation()
        {
            Val = new EnumValue<DocumentFormat.OpenXml.Drawing.Charts.OrientationValues>(
            DocumentFormat.OpenXml.Drawing.Charts.OrientationValues.MinMax)
        }),
        new AxisPosition() { Val = new EnumValue<AxisPositionValues>(AxisPositionValues.Left) },
        new MajorGridlines(),
        new DocumentFormat.OpenXml.Drawing.Charts.NumberingFormat()
        {
            FormatCode = new StringValue("General"),
            SourceLinked = new BooleanValue(true)
        }, new TickLabelPosition()
        {
            Val = new EnumValue<TickLabelPositionValues>
            (TickLabelPositionValues.NextTo)
        }, new CrossingAxis() { Val = new UInt32Value(48650112U) },
        new Crosses() { Val = new EnumValue<CrossesValues>(CrossesValues.AutoZero) },
        new CrossBetween() { Val = new EnumValue<CrossBetweenValues>(CrossBetweenValues.Between) }));

        // create child elements of the c:legend element
        dc.Legend legend = new dc.Legend();
        legend.Append(new dc.LegendPosition() { Val = LegendPositionValues.Right });
        dc.Overlay overlay = new dc.Overlay() { Val = false };
        legend.Append(overlay);

        dc.TextProperties textPros = new DocumentFormat.OpenXml.Drawing.Charts.TextProperties();
        textPros.Append(new d.BodyProperties());
        textPros.Append(new d.ListStyle());

        d.Paragraph paragraph = new d.Paragraph();
        d.ParagraphProperties paraPros = new d.ParagraphProperties();
        d.DefaultParagraphProperties defaultParaPros = new d.DefaultParagraphProperties();
        defaultParaPros.Append(new d.LatinFont() { Typeface = "Arial", PitchFamily = 34, CharacterSet = 0 });
        defaultParaPros.Append(new d.ComplexScriptFont() { Typeface = "Arial", PitchFamily = 34, CharacterSet = 0 });
        paraPros.Append(defaultParaPros);
        paragraph.Append(paraPros);
        paragraph.Append(new d.EndParagraphRunProperties() { Language = "en-Us" });

        textPros.Append(paragraph);
        legend.Append(textPros);

        // Append c:view3D, c:plotArea and c:legend elements to the end of c:chart element
        chart.Append(view3D);
        chart.Append(plotArea);
        chart.Append(legend);

        // Append the c:chart element to the end of c:chartSpace element
        chartSpace.Append(chart);

        // Create c:spPr Elements and fill the child elements of it
        chartShapePros = new dc.ChartShapeProperties();
        d.Outline outline = new d.Outline();
        outline.Append(new d.NoFill());
        chartShapePros.Append(outline);

        // Append c:spPr element to the end of c:chartSpace element
        chartSpace.Append(chartShapePros);

        chartPart.ChartSpace = chartSpace;

        // Generate content of the MainDocumentPart
        GeneratePartContent(mainPart);

        return document;

    }
public static void GeneratePartContent(MainDocumentPart mainPart)
    {
        w.Paragraph paragraph = new w.Paragraph() { RsidParagraphAddition = "00C75AEB", RsidRunAdditionDefault = "000F3EFF" };

        // Create a new run that has an inline drawing object
        w.Run run = new w.Run();
        w.Drawing drawing = new w.Drawing();

        dw.Inline inline = new dw.Inline();
        inline.Append(new dw.Extent() { Cx = 5274310L, Cy = 3076575L });
        dw.DocProperties docPros = new dw.DocProperties() { Id = (UInt32Value)1U, Name = "Chart 1" };
        inline.Append(docPros);

        d.Graphic g = new d.Graphic();
        d.GraphicData graphicData = new d.GraphicData() { Uri = "http://schemas.openxmlformats.org/drawingml/2006/chart" };
        dc.ChartReference chartReference = new ChartReference() { Id = "rId110" };
        graphicData.Append(chartReference);
        g.Append(graphicData);
        inline.Append(g);
        drawing.Append(inline);
        run.Append(drawing);
        paragraph.Append(run);

        mainPart.Document.Body.Append(paragraph);
    }

这是我看到的结果。希望这会有所帮助。 enter image description here