Add the chart control to the worksheet programmatically
-
1. process the startup event in sheet1Program, Insert the followingCodeTo add the chart control. Run the following command in the "sheetaskstartup" event in the "sheet1.cs" file:
Microsoft. Office. Tools. Excel. Chart employeedata;
Employeedata = This. Controls. addchart (25,110,200,150, "employees ");
Employeedata. charttype = excel. xlcharttype. xl3dpie;
// Gets the cells that define the data to be charted.
Excel. Range chartrange = This. get_range ("A5", "D8 ");
Employeedata. setsourcedata (chartrange, missing );
-
2. The following code generates a worksheet host item based on the opened worksheet, and then adds a chart control. Run the task in the thisworkbook_startup event of the thisworkbook. CS file.
-
Private void addchart (){
-
// Use the following line of code in projects that target the. NET Framework 4. worksheet = globals. Factory. getvstoobject (globals. thisaddin. application. activeworkbook. activesheet );
-
// In projects that target the. NET Framework 3.5, use the following line of code.
-
// Worksheet =
-
// (Excel. worksheet) globals. thisaddin. application. activeworkbook. activesheet). getvstoobject ();
-
-
Excel. Range cells = worksheet. Range ["A5", "D8"];
-
Chart chart = worksheet. Controls. addchart (cells, "employees ");
-
Chart. charttype = Microsoft. Office. InterOP. Excel. xlcharttype. xl3dpie;
-
Chart. setsourcedata (cells, missing );
-
}