Create excel chart

  • VSTO
  • Spire.XLS
  • Download Sample Code

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
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Spire.Xls;

namespace Spire.XLS
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a new workbook
            Workbook workbook = new Workbook();

            //Initialize worksheet
            workbook.CreateEmptySheets(1);
            Worksheet sheet = workbook.Worksheets[0];           

            //Add data for pie chart
            sheet.Range["A1"].Value = "Quarter";
            sheet.Range["B1"].Value = "Sales";
            sheet.Range["A2"].Value = "One";
            sheet.Range["B2"].NumberValue = 4000;
            sheet.Range["A3"].Value = "Two";
            sheet.Range["B3"].NumberValue = 6000;
            sheet.Range["A4"].Value = "Three";
            sheet.Range["B4"].NumberValue = 10000;
            sheet.Range["A5"].Value = "Four";
            sheet.Range["B5"].NumberValue = 13000;

            //Create a pie chart
            Chart chart = sheet.Charts.Add(ExcelChartType.Pie3D);

            //Set region of chart data
            chart.DataRange = sheet.Range["B2:B5"];
            chart.SeriesDataFromRange = false;

            //Set position of chart
            chart.LeftColumn = 1;
            chart.TopRow = 6;
            chart.RightColumn = 9;
            chart.BottomRow = 25;

            //Chart title
            chart.ChartTitle = "Sales by quarter";

            //Initialize the chart series
            Spire.Xls.Charts.ChartSerie cs = chart.Series[0];

            //Chart Labels resource
            cs.CategoryLabels = sheet.Range["A2:A5"];

            //Chart value resource
            cs.Values = sheet.Range["B2:B5"];
                    
            //Save and Launch
            workbook.SaveToFile("3DPieChart.xlsx",ExcelVersion.Version2010);
            System.Diagnostics.Process.Start("3DPieChart.xlsx");
        }
    }
}