title | description | ms.date | ms.topic | dev_langs | helpviewer_keywords | author | ms.author | manager | ms.subservice | ||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Add Chart controls to worksheets |
Learn how you can add Chart controls to a Microsoft Office Excel worksheet at design time and at run time in document-level customizations. |
02/02/2017 |
how-to |
|
|
John-Hart |
johnhart |
mijacobs |
office-development |
You can add xref:Microsoft.Office.Tools.Excel.Chart controls to a Microsoft Office Excel worksheet at design time and at run time in document-level customizations. You can also add xref:Microsoft.Office.Tools.Excel.Chart controls at run time in VSTO Add-ins.
[!INCLUDEappliesto_xlalldocapp]
This topic describes the following tasks:
-
Add Chart controls at run time in a VSTO Add-in project
For more information about xref:Microsoft.Office.Tools.Excel.Chart controls, see Chart control.
You can add the xref:Microsoft.Office.Tools.Excel.Chart control to your worksheet in the same manner you would add a chart from within the application.
Note
The xref:Microsoft.Office.Tools.Excel.Chart control is not available from the Toolbox or the Data Sources window.
-
On the Insert tab, in the Charts group, click Column, click a category of charts, and then click the type of chart you want.
-
In the Insert Chart dialog box, click OK.
-
On the Design tab, in the Data group, click Select Data.
-
In the Select Data Source dialog box, click in the Chart data range box and clear any default selection.
-
In the Data for Chart sheet, select the range of cells that contains the data for the chart (cells A5 through D8).
-
In the Select Data Source dialog box, click OK.
You can add the xref:Microsoft.Office.Tools.Excel.Chart control dynamically at run time. Dynamically created charts are not persisted in the document as host controls when the document is closed. For more information, see Add controls to Office documents at run time.
-
In the xref:Microsoft.Office.Tools.Excel.Worksheet.Startup event handler of
Sheet1
, insert the following code to add the xref:Microsoft.Office.Tools.Excel.Chart control.:::code language="csharp" source="../vsto/codesnippet/CSharp/Trin_VstcoreHostControlsExcelCS/Sheet1.cs" id="Snippet1":::
You can add a xref:Microsoft.Office.Tools.Excel.Chart control programmatically to any open worksheet in a VSTO Add-in project. For more information, see Extend Word documents and Excel workbooks in VSTO Add-ins at run time.
Dynamically created chart controls are not persisted in the worksheet as host controls when the worksheet is closed. For more information, see Add Controls to Office documents at run time.
-
The following code generates a worksheet host item that is based on the open worksheet, and then adds a xref:Microsoft.Office.Tools.Excel.Chart control.
:::code language="csharp" source="../vsto/codesnippet/CSharp/Trin_Excel_Dynamic_Controls/ThisAddIn.cs" id="Snippet9":::
This example has the following requirements:
- Data to be charted, stored in the range from A5 to D8 in the worksheet.