Skip to content

Commit 3b69597

Browse files
committed
clean up docs\spreadsheet\how-to-insert-a-chart-into-a-spreadsheet.md
1 parent b9b7789 commit 3b69597

File tree

3 files changed

+55
-102
lines changed

3 files changed

+55
-102
lines changed

docs/spreadsheet/how-to-insert-a-chart-into-a-spreadsheet.md

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ ms.suite: office
99
ms.author: o365devx
1010
author: o365devx
1111
ms.topic: conceptual
12-
ms.date: 12/12/2023
12+
ms.date: 01/14/2025
1313
ms.localizationpriority: high
1414
---
1515

@@ -22,7 +22,7 @@ This topic shows how to use the classes in the Open XML SDK for Office to insert
2222
In this how-to, you are going to deal with the row, cell, and cell value
2323
elements. Therefore it is useful to familiarize yourself with these
2424
elements. The following text from the [!include[ISO/IEC 29500 URL](../includes/iso-iec-29500-link.md)] specification
25-
introduces row (\<**row**\>) element.
25+
introduces row (`<row/>`) element.
2626

2727
> The row element expresses information about an entire row of a
2828
> worksheet, and contains all cell definitions for a particular row in
@@ -75,7 +75,7 @@ element.
7575
## Cell element
7676

7777
The following text from the [!include[ISO/IEC 29500 URL](../includes/iso-iec-29500-link.md)] specification
78-
introduces cell (\<**c**\>) element.
78+
introduces cell (`<c/>`) element.
7979

8080
> This collection represents a cell in the worksheet. Information about
8181
> the cell's location (reference), value, data type, formatting, and
@@ -118,7 +118,7 @@ element.
118118
## Cell value element
119119

120120
The following text from the [!include[ISO/IEC 29500 URL](../includes/iso-iec-29500-link.md)] specification
121-
introduces Cell Value (\<**c**\>) element.
121+
introduces Cell Value (`<c/>`) element.
122122

123123
> This element expresses the value contained in a cell. If the cell
124124
> contains a string, then this value is an index into the shared string
@@ -128,7 +128,7 @@ introduces Cell Value (\<**c**\>) element.
128128
> element.
129129
>
130130
> For applications not wanting to implement the shared string table, an
131-
> "inline string" may be expressed in an \<**is**\> element under \<**c**\> (instead of a \<**v**\> element under \<**c**\>), in the same way a string would be
131+
> "inline string" may be expressed in an `<is/>` element under `<c/>` (instead of a `<v/>` element under `<c/>`), in the same way a string would be
132132
> expressed in the shared string table.
133133
>
134134
> &copy; [!include[ISO/IEC 29500 version](../includes/iso-iec-29500-version.md)]
@@ -143,7 +143,7 @@ In the following example cell B4 contains the number 360.
143143

144144
## How the sample code works
145145

146-
After opening the spreadsheet file for read/write access, the code verifies if the specified worksheet exists. It then adds a new <xref:DocumentFormat.OpenXml.Packaging.DrawingsPart> object using the <xref:DocumentFormat.OpenXml.Packaging.OpenXmlPartContainer.AddNewPart*> method, appends it to the worksheet, and saves the worksheet part. The code then adds a new <xref:DocumentFormat.OpenXml.Packaging.ChartPart> object, appends a new <xref:DocumentFormat.OpenXml.Packaging.ChartPart.ChartSpace*> object to the **ChartPart** object, and then appends a new <xref:DocumentFormat.OpenXml.Drawing.Charts.ChartSpace.EditingLanguage*> object to the **ChartSpace*** object that specifies the language for the chart is English-US.
146+
After opening the spreadsheet file for read/write access, the code verifies if the specified worksheet exists. It then adds a new <xref:DocumentFormat.OpenXml.Packaging.DrawingsPart> object using the <xref:DocumentFormat.OpenXml.Packaging.OpenXmlPartContainer.AddNewPart*> method, appends it to the worksheet, and saves the worksheet part. The code then adds a new <xref:DocumentFormat.OpenXml.Packaging.ChartPart> object, appends a new <xref:DocumentFormat.OpenXml.Packaging.ChartPart.ChartSpace*> object to the `ChartPart` object, and then appends a new <xref:DocumentFormat.OpenXml.Drawing.Charts.ChartSpace.EditingLanguage*> object to the `ChartSpace` object that specifies the language for the chart is English-US.
147147

148148
### [C#](#tab/cs-1)
149149
[!code-csharp[](../../samples/spreadsheet/insert_a_chartto/cs/Program.cs#snippet1)]
@@ -153,11 +153,11 @@ After opening the spreadsheet file for read/write access, the code verifies if t
153153
***
154154

155155

156-
The code creates a new clustered column chart by creating a new <xref:DocumentFormat.OpenXml.Drawing.Charts.BarChart> object with
157-
<xref:DocumentFormat.OpenXml.Drawing.Charts.BarDirectionValues> object set to **Column** and <xref:DocumentFormat.OpenXml.Drawing.Charts.BarGroupingValues> object set to **Clustered**.
156+
The code creates a new clustered column chart by creating a new <xref:DocumentFormat.OpenXml.Drawing.Charts.BarChart> object with
157+
<xref:DocumentFormat.OpenXml.Drawing.Charts.BarDirectionValues> object set to `Column` and <xref:DocumentFormat.OpenXml.Drawing.Charts.BarGroupingValues> object set to `Clustered`.
158158

159-
The code then iterates through each key in the **Dictionary** class. For each key, it appends a
160-
<xref:DocumentFormat.OpenXml.Drawing.Charts.BarChartSeries> object to the **BarChart** object and sets the <xref:DocumentFormat.OpenXml.Drawing.Charts.SeriesText> object of the **BarChartSeries** object to equal the key. For each key, it appends a <xref:DocumentFormat.OpenXml.Drawing.Charts.NumberLiteral> object to the **Values** collection of the **BarChartSeries** object and sets the **NumberLiteral** object to equal the **Dictionary** class value corresponding to the key.
159+
The code then iterates through each key in the `Dictionary` class. For each key, it appends a
160+
<xref:DocumentFormat.OpenXml.Drawing.Charts.BarChartSeries> object to the `BarChart` object and sets the <xref:DocumentFormat.OpenXml.Drawing.Charts.SeriesText> object of the `BarChartSeries` object to equal the key. For each key, it appends a <xref:DocumentFormat.OpenXml.Drawing.Charts.NumberLiteral> object to the `Values` collection of the `BarChartSeries` object and sets the `NumberLiteral` object to equal the `Dictionary` class value corresponding to the key.
161161

162162
### [C#](#tab/cs-2)
163163
[!code-csharp[](../../samples/spreadsheet/insert_a_chartto/cs/Program.cs#snippet2)]
@@ -177,7 +177,7 @@ The code adds the <xref:DocumentFormat.OpenXml.Drawing.Charts.CategoryAxis> obje
177177
***
178178

179179

180-
The code positions the chart on the worksheet by creating a <xref:DocumentFormat.OpenXml.Packaging.DrawingsPart.WorksheetDrawing*> object and appending a **TwoCellAnchor** object. The **TwoCellAnchor** object specifies how to move or resize the chart if you move the rows and columns between the <xref:DocumentFormat.OpenXml.Drawing.Spreadsheet.FromMarker> and <xref:DocumentFormat.OpenXml.Drawing.Spreadsheet.ToMarker> anchors. The code then creates a <xref:DocumentFormat.OpenXml.Drawing.Spreadsheet.GraphicFrame> object to contain the chart and names the chart "Chart 1," and saves the worksheet drawing.
180+
The code positions the chart on the worksheet by creating a <xref:DocumentFormat.OpenXml.Packaging.DrawingsPart.WorksheetDrawing*> object and appending a `TwoCellAnchor` object. The `TwoCellAnchor` object specifies how to move or resize the chart if you move the rows and columns between the <xref:DocumentFormat.OpenXml.Drawing.Spreadsheet.FromMarker> and <xref:DocumentFormat.OpenXml.Drawing.Spreadsheet.ToMarker> anchors. The code then creates a <xref:DocumentFormat.OpenXml.Drawing.Spreadsheet.GraphicFrame> object to contain the chart and names the chart "Chart 1".
181181

182182
### [C#](#tab/cs-4)
183183
[!code-csharp[](../../samples/spreadsheet/insert_a_chartto/cs/Program.cs#snippet4)]
@@ -199,6 +199,7 @@ The following is the complete sample code in both C\# and Visual Basic.
199199

200200
### [Visual Basic](#tab/vb)
201201
[!code-vb[](../../samples/spreadsheet/insert_a_chartto/vb/Program.vb#snippet0)]
202+
***
202203

203204
## See also
204205

samples/spreadsheet/insert_a_chartto/cs/Program.cs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// <Snippet0>
21
using DocumentFormat.OpenXml;
32
using DocumentFormat.OpenXml.Drawing;
43
using DocumentFormat.OpenXml.Drawing.Charts;
@@ -8,9 +7,11 @@
87
using System.Collections.Generic;
98
using System.Linq;
109

10+
InsertChartInSpreadsheet(args[0], args[1], args[2], new Dictionary<string, int>() { { "First", 1 }, { "Second", 2 }, { "Third", 3 } });
1111

1212
// Given a document name, a worksheet name, a chart title, and a Dictionary collection of text keys
1313
// and corresponding integer data, creates a column chart with the text as the series and the integers as the values.
14+
// <Snippet0>
1415
static void InsertChartInSpreadsheet(string docName, string worksheetName, string title, Dictionary<string, int> data)
1516
{
1617
// Open the document for editing.
@@ -39,7 +40,6 @@ static void InsertChartInSpreadsheet(string docName, string worksheetName, strin
3940
DrawingsPart drawingsPart = worksheetPart.AddNewPart<DrawingsPart>();
4041
worksheetPart.Worksheet.Append(new DocumentFormat.OpenXml.Spreadsheet.Drawing()
4142
{ Id = worksheetPart.GetIdOfPart(drawingsPart) });
42-
worksheetPart.Worksheet.Save();
4343

4444
// Add a new chart and set the chart language to English-US.
4545
ChartPart chartPart = drawingsPart.AddNewPart<ChartPart>();
@@ -129,9 +129,6 @@ static void InsertChartInSpreadsheet(string docName, string worksheetName, strin
129129
new Layout()));
130130

131131
chart.Append(new PlotVisibleOnly() { Val = new BooleanValue(true) });
132-
133-
// Save the chart part.
134-
chartPart.ChartSpace.Save();
135132
// </Snippet3>
136133

137134
// <Snippet4>
@@ -162,16 +159,11 @@ static void InsertChartInSpreadsheet(string docName, string worksheetName, strin
162159
new Extents() { Cx = 0L, Cy = 0L }));
163160

164161
graphicFrame.Append(new Graphic(new GraphicData(new ChartReference() { Id = drawingsPart.GetIdOfPart(chartPart) })
165-
{ Uri = "https://schemas.openxmlformats.org/drawingml/2006/chart" }));
162+
{ Uri = "http://schemas.openxmlformats.org/drawingml/2006/chart" }));
166163

167164
twoCellAnchor.Append(new ClientData());
168-
169-
// Save the WorksheetDrawing object.
170-
drawingsPart.WorksheetDrawing.Save();
171165
// </Snippet4>
172166
}
173167

174168
}
175169
// </Snippet0>
176-
177-
InsertChartInSpreadsheet(args[0], args[1], args[2], new Dictionary<string, int>() { { "First", 1 }, { "Second", 2 }, { "Third", 3 } });

0 commit comments

Comments
 (0)