在 OpenXML 条形图上添加自定义文本

Add Custom Text on OpenXML BarChart

我想像这样在条形图上添加自定义文本:

如何使用 OpenXML 的哪个功能执行此操作?

请帮忙

这可以通过在不属于图表的形状上添加文本来实现。它可以通过定位电子表格行来放置。以下是示例代码:

            string[] strGenerator = new string[] { "2213969", "2213963", "2213979", "2213969", "2213963", "2213979" };
            string[] strRowId = new string[] { "22", "20", "18","18","20","22" };
            string[] strColumnId = new string[] { "1", "1", "1", "10", "10", "10" };
            string[] strRowOffset = new string[] { "18000", "27000", "27000", "50000", "50000", "50000" }; //{ "80000", "186266", "95036", "81643", "27214", "136071" };
            string[] strColumnOffset = new string[] { "489858", "508606", "520699", "497453", "497453", "497453" }; //{ "489858", "508606", "520699", "197453", "182032", "163285" };

            for (i = 0; i < 6; i++)
            {
                Xdr.OneCellAnchor oneCellAnchor6 = new Xdr.OneCellAnchor();

                Xdr.FromMarker fromMarker8 = new Xdr.FromMarker();
                Xdr.ColumnId columnId10 = new Xdr.ColumnId();
                columnId10.Text = strColumnId[i]; // "1";
                Xdr.ColumnOffset columnOffset10 = new Xdr.ColumnOffset();
                columnOffset10.Text = strColumnOffset[i]; // "520699"
                Xdr.RowId rowId10 = new Xdr.RowId();
                rowId10.Text = strRowId[i]; //"16";
                Xdr.RowOffset rowOffset10 = new Xdr.RowOffset();
                rowOffset10.Text = strRowOffset[i];//"95036"; // Convert.ToString(95036*(i+1));// "95036";

                fromMarker8.Append(columnId10);
                fromMarker8.Append(columnOffset10);
                fromMarker8.Append(rowId10);
                fromMarker8.Append(rowOffset10);
                //Xdr.Extent extent6 = new Xdr.Extent() { Cx = 1782535L, Cy = 204108L };

                Xdr.Extent extent6 = new Xdr.Extent() { Cx = 1782535L, Cy = 204108L };

                Xdr.Shape shape6 = new Xdr.Shape() { Macro = "", TextLink = "" };

                Xdr.NonVisualShapeProperties nonVisualShapeProperties6 = new Xdr.NonVisualShapeProperties();

                Xdr.NonVisualDrawingProperties nonVisualDrawingProperties8 = new Xdr.NonVisualDrawingProperties() { Id = (UInt32Value)21U, Name = "TextBox 20" };

                A.NonVisualDrawingPropertiesExtensionList nonVisualDrawingPropertiesExtensionList8 = new A.NonVisualDrawingPropertiesExtensionList();

                A.NonVisualDrawingPropertiesExtension nonVisualDrawingPropertiesExtension8 = new A.NonVisualDrawingPropertiesExtension() { Uri = "{FF2B5EF4-FFF2-40B4-BE49-F238E27FC236}" };

                OpenXmlUnknownElement openXmlUnknownElement9 = OpenXmlUnknownElement.CreateOpenXmlUnknownElement("<a16:creationId xmlns:a16=\"http://schemas.microsoft.com/office/drawing/2014/main\" id=\"{6230E3EC-6B4D-47AE-A392-192F8AB4F7CE}\" />");

                nonVisualDrawingPropertiesExtension8.Append(openXmlUnknownElement9);

                nonVisualDrawingPropertiesExtensionList8.Append(nonVisualDrawingPropertiesExtension8);

                nonVisualDrawingProperties8.Append(nonVisualDrawingPropertiesExtensionList8);
                Xdr.NonVisualShapeDrawingProperties nonVisualShapeDrawingProperties6 = new Xdr.NonVisualShapeDrawingProperties() { TextBox = true };

                nonVisualShapeProperties6.Append(nonVisualDrawingProperties8);
                nonVisualShapeProperties6.Append(nonVisualShapeDrawingProperties6);

                Xdr.ShapeProperties shapeProperties6 = new Xdr.ShapeProperties();

                A.Transform2D transform2D6 = new A.Transform2D();
                A.Offset offset8 = new A.Offset() { X = 1130299L, Y = 3276903L };
                A.Extents extents8 = new A.Extents() { Cx = 1782535L, Cy = 204108L };

                //A.Offset offset8 = new A.Offset() { X = 0, Y = 0 };
                //A.Extents extents8 = new A.Extents() { Cx = 0, Cy = 0 };

                transform2D6.Append(offset8);
                transform2D6.Append(extents8);

                A.PresetGeometry presetGeometry6 = new A.PresetGeometry() { Preset = A.ShapeTypeValues.Rectangle };
                A.AdjustValueList adjustValueList6 = new A.AdjustValueList();

                presetGeometry6.Append(adjustValueList6);
                A.NoFill noFill6 = new A.NoFill();

                shapeProperties6.Append(transform2D6);
                shapeProperties6.Append(presetGeometry6);
                shapeProperties6.Append(noFill6);

                Xdr.ShapeStyle shapeStyle6 = new Xdr.ShapeStyle();

                A.LineReference lineReference6 = new A.LineReference() { Index = (UInt32Value)0U };
                A.RgbColorModelPercentage rgbColorModelPercentage16 = new A.RgbColorModelPercentage() { RedPortion = 0, GreenPortion = 0, BluePortion = 0 };

                lineReference6.Append(rgbColorModelPercentage16);

                A.FillReference fillReference6 = new A.FillReference() { Index = (UInt32Value)0U };
                A.RgbColorModelPercentage rgbColorModelPercentage17 = new A.RgbColorModelPercentage() { RedPortion = 0, GreenPortion = 0, BluePortion = 0 };

                fillReference6.Append(rgbColorModelPercentage17);

                A.EffectReference effectReference6 = new A.EffectReference() { Index = (UInt32Value)0U };
                A.RgbColorModelPercentage rgbColorModelPercentage18 = new A.RgbColorModelPercentage() { RedPortion = 0, GreenPortion = 0, BluePortion = 0 };

                effectReference6.Append(rgbColorModelPercentage18);

                A.FontReference fontReference6 = new A.FontReference() { Index = A.FontCollectionIndexValues.Minor };
                A.SchemeColor schemeColor29 = new A.SchemeColor() { Val = A.SchemeColorValues.Text1 };

                fontReference6.Append(schemeColor29);

                shapeStyle6.Append(lineReference6);
                shapeStyle6.Append(fillReference6);
                shapeStyle6.Append(effectReference6);
                shapeStyle6.Append(fontReference6);

                Xdr.TextBody textBody6 = new Xdr.TextBody();

                A.BodyProperties bodyProperties6;
                // if (i == 2)
                // {
                //     bodyProperties6 = new A.BodyProperties() { VerticalOverflow = A.TextVerticalOverflowValues.Clip, HorizontalOverflow = A.TextHorizontalOverflowValues.Clip, Wrap = A.TextWrappingValues.Square, RightToLeftColumns = false, Anchor = A.TextAnchoringTypeValues.Bottom };
                //  }
                // else
                //  {
                bodyProperties6 = new A.BodyProperties() { VerticalOverflow = A.TextVerticalOverflowValues.Clip, HorizontalOverflow = A.TextHorizontalOverflowValues.Clip, Wrap = A.TextWrappingValues.Square, RightToLeftColumns = false, Anchor = A.TextAnchoringTypeValues.Top };
                // }

                A.NoAutoFit noAutoFit6 = new A.NoAutoFit();

                bodyProperties6.Append(noAutoFit6);
                A.ListStyle listStyle6 = new A.ListStyle();

                A.Paragraph paragraph6 = new A.Paragraph();
                A.ParagraphProperties paragraphProperties3 = new A.ParagraphProperties() { Alignment = A.TextAlignmentTypeValues.Left };

                A.Run run9 = new A.Run();

                A.RunProperties runProperties9 = new A.RunProperties() { Language = "en-US", FontSize = 1100, Bold = false, Italic = false, Underline = A.TextUnderlineValues.None, Strike = A.TextStrikeValues.NoStrike };

                A.SolidFill solidFill15 = new A.SolidFill();
                A.SchemeColor schemeColor30 = new A.SchemeColor() { Val = A.SchemeColorValues.Background1 };

                solidFill15.Append(schemeColor30);
                A.EffectList effectList7 = new A.EffectList();
                A.LatinFont latinFont6 = new A.LatinFont() { Typeface = "+mn-lt" };
                A.EastAsianFont eastAsianFont6 = new A.EastAsianFont() { Typeface = "+mn-ea" };
                A.ComplexScriptFont complexScriptFont6 = new A.ComplexScriptFont() { Typeface = "+mn-cs" };

                runProperties9.Append(solidFill15);
                runProperties9.Append(effectList7);
                runProperties9.Append(latinFont6);
                runProperties9.Append(eastAsianFont6);
                runProperties9.Append(complexScriptFont6);
                A.Text text9 = new A.Text();
                text9.Text = strGenerator[i];

                run9.Append(runProperties9);
                run9.Append(text9);

                A.Run run10 = new A.Run();

                A.RunProperties runProperties10 = new A.RunProperties() { Language = "en-US", FontSize = 1200 };

                A.SolidFill solidFill16 = new A.SolidFill();
                A.SchemeColor schemeColor31 = new A.SchemeColor() { Val = A.SchemeColorValues.Background1 };

                solidFill16.Append(schemeColor31);

                runProperties10.Append(solidFill16);
                A.Text text10 = new A.Text();
                text10.Text = "";

                run10.Append(runProperties10);
                run10.Append(text10);

                paragraph6.Append(paragraphProperties3);
                paragraph6.Append(run9);
                paragraph6.Append(run10);

                textBody6.Append(bodyProperties6);
                textBody6.Append(listStyle6);
                textBody6.Append(paragraph6);

                shape6.Append(nonVisualShapeProperties6);
                shape6.Append(shapeProperties6);
                shape6.Append(shapeStyle6);
                shape6.Append(textBody6);
                Xdr.ClientData clientData8 = new Xdr.ClientData();

                oneCellAnchor6.Append(fromMarker8);
                oneCellAnchor6.Append(extent6);
                oneCellAnchor6.Append(shape6);
                oneCellAnchor6.Append(clientData8);
                drawingspart.WorksheetDrawing.Append(oneCellAnchor6);
            }