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

How to add a barchart to a OpenXML created document

我的 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

感谢您的帮助!

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

一种更快的解决方法是使用 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 并添加条形图(插入->图表->条形图)。
  5. 使用颜色、格式等设置条形图的样式以满足您的最终解决方案并保存并关闭。
  6. 运行 打开 XML 生产力工具并单击顶部的 比较文件 按钮。
  7. 为源选择 NoBarChart.docx,为目标选择 WithBarChart.docx
  8. 单击文件中突出显示的不同部分,然后单击 查看部分差异 查看 XML 中的差异。然后单击查看包代码,该工具将生成代码以使源看起来像目标。

检查它生成的代码以获得有关如何添加条形图的想法。如果缺少某些代码,您可以使用该工具生成整个 Target 文件。

A link 到一个简单的 WPF 应用程序,该应用程序生成带有条形图的空白文档 is on GitHub。这是文档的图片:

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

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

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);
    }

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