using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;
using Microsoft.Office.Tools.Excel;
namespace VSTO
{
public partial class ThisAddIn
{
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
//Instantiate the Application object.
Excel.Application ExcelApp = Application;
//Add a Workbook.
Excel.Workbook objBook = ExcelApp.Workbooks.Add();
//Get the active Worksheet
Microsoft.Office.Interop.Excel.Worksheet nativeWorksheet = Globals.ThisAddIn.Application.ActiveWorkbook.ActiveSheet;
Microsoft.Office.Tools.Excel.Worksheet sheet = Globals.Factory.GetVstoObject(nativeWorksheet);
//Add data for pie chart
sheet.Cells[1, 1] = "Quarter";
sheet.Cells[1, 2] = "Sales";
sheet.Cells[2, 1] = "One";
sheet.Cells[2, 2] = 4000;
sheet.Cells[3, 1] = "Two";
sheet.Cells[3, 2] = 6000;
sheet.Cells[4, 1] = "Three";
sheet.Cells[4, 2] = 10000;
sheet.Cells[5, 1] = "Four";
sheet.Cells[5, 2] = 13000;
//Chart reference
Microsoft.Office.Tools.Excel.Chart chart;
//Add a Pie Chart
chart = sheet.Controls.AddChart(0, 100, 400, 200, "Sales by quarter");
chart.ChartType = Microsoft.Office.Interop.Excel.XlChartType.xl3DPie;
//Set chart title
chart.HasTitle = true;
chart.ChartTitle.Text = "Sales by quarter";
//Set region of chart data
Microsoft.Office.Interop.Excel.Range chartRange = sheet.get_Range("A2", "B5");
chart.SetSourceData(chartRange);
//Get the active workbook
Microsoft.Office.Interop.Excel.Workbook workbook = sheet.Application.ActiveWorkbook;
//Save the file
workbook.SaveAs("3DPieChart.xlsx");
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
}
#region VSTO generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisAddIn_Startup);
this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
}
#endregion
}
}