Add excel worksheet

  • 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)
        {
            //Add a new worksheet
            Excel.Workbook newWorkbook = this.Application.Workbooks.Add();
            Excel.Worksheet sheet = newWorkbook.ActiveSheet;

            //Set the worksheet name and the text of the specified range
            sheet.Name = "AddedSheet";
            Excel.Range cell = sheet.Cells;
            cell.set_Item(1, 1, "Spire.XLS");

            //Save
            newWorkbook.Save();

        }

        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)
        {
            //Initialize a new instance of workbook
            Workbook workbook = new Workbook();

            //Add a new worksheet
            Worksheet newSheet=workbook.Worksheets.Add("AddedSheet");

            //Set text of specified range
            newSheet.Range[1, 1].Text = "Spire.XLS";

            //Save and Launch
            workbook.SaveToFile("Sample.xlsx", ExcelVersion.Version2013);
            System.Diagnostics.Process.Start("Sample.xlsx");
        }
    }
}