Adding New Worksheets to Workbook and Activating a Sheet
When working with a template file, sometimes, there’s a need to add extra worksheets into the workbook to collect data. The new cells will be filled with data at specified positions and locations in each worksheet.
Similarly, you may need a specific worksheet to be active and viewed first when the file is opened in Microsoft Excel. An “active sheet” is the sheet that you’re working on in a workbook. The name on the tab of the active sheet is bold by default.
Adding worksheets and setting which sheet is active are common and simple tasks that developers need to know how to perform. In this article, we carry out these tasks using VSTO and Aspose.Cells for .NET .

Adding Worksheets and Activating a Sheet
For the purposes of this migration tip:
- Add new worksheets to an existing Microsoft Excel file.
- Fill data into the cells of each new worksheet.
- Activate a sheet in the workbook.
- Save as Microsoft Excel file.
Below, are parallel code snippets for VSTO (C#, VB) and Aspose.Cells for .NET (C#, VB), that show how to achieve these tasks.
Aspose.Cells for .NET
The C# Excel Library
- Intuitive C# & VB.NET Excel Document API
- No need to install Microsoft Office or Excel Interop
- Read , edit & create Excel spreadsheet files
- Fully supports .NET 7,6,5, Core, Framework, and Azure
- Read Excel Files without Interop
- Excel Worksheets
- Create Excel Files
- Convert Spreadsheet File Types
- Excel to SQL via System.Data.DataSet
- Excel to SQL and DataGrid via DataTable
- Excel Conditional Formatting
- Protect Excel Files
- Excel Formulas in C#
- Edit Excel Metadata in C#
- Update Excel Database Records
- Style Excel Cell Borders & Fonts
- Sort Excel Ranges in C#
- Repeat Excel Rows & Columns
- Excel Print Setup
- Cell Data Display Format
- Load Excel From SQL Database
- Combine Excel Ranges
- Select Excel Range
- Convert Excel to HTML
- Aggregate Excel Functions
- Create Excel Line Chart
- Freeze Panes in Excel
- Copy Excel Worksheets
IronXL Blog
- Using IronXL
- C# Spreadsheet
Published November 15, 2023
How to Edit a Spreadsheet in C#
Creating, managing, and analyzing data is a fundamental aspect of modern business and data-driven decision-making. In this context, C# .NET core, a versatile and powerful programming language, offers an ideal platform for working with spreadsheets.
Spreadsheets are indispensable tools for organizing and manipulating data, and C# provides a robust ecosystem for interacting with spreadsheet files.
Whether you need to automate data entry, generate reports pivot tables and charts, perform complex calculations, or extract valuable insights from large datasets, C# empowers you to excel in the world of spreadsheet applications. C#
In this article, we will explore the art of spreadsheet manipulation in C#, with the help of most advanced C# data manipulating Library named as IronXL without interacting with the user interface.
How to Edit a Spreadsheet in C#
The ability to edit and manipulate the data of Excel files using C# can be extremely helpful to businesses and developers, the following steps are a brief overview of how this can be done with ease in five simple steps
- Install the C# library for editing Spreadsheets.
- Utilize the WorkBook.Load method to open an Excel file.
- Use the Worksheet.Value method to Set the Value of the sell.
- Apply range.Sum method to sum the values of the cells.
- Save the newly generated Spreadsheet as .xlsx format using the SaveAs method.
IronXL is a powerful tool that has become a game-changer in the realm of C# spreadsheet processing. As a comprehensive library designed specifically for C#
Whether you're working on a financial application, data-driven dashboard, or any project that involves handling tabular data, IronXL provides a robust and user-friendly solution that simplifies complex spreadsheet operations in C# development that easily be implemented in your web application or your .NET applications.
In this article, we will explore the key features and benefits of IronXL, shedding light on how it enhances the efficiency and functionality of C# applications that deal with Excel spreadsheet processing. Using IronXL you can manipulate Excel spreadsheets without the need of Microsoft Office applications, also you can create advanced Excel spreadsheets without Microsoft Excel and Excel interop.
Create a New Visual Studio Project
Before installing IronXL first we need to create a new Visual Studio C# project or load an existing one, below are the steps to create a new project in Visual Studio.
Open the Visual Studio and click on file menu, there a drop-down menu will appear, in the dropdown menu click on the new, another side menu will appear in this side menu click on project.

A new window will appear in this new window click on the search bar and write console application in the search bar and select the one with C# option, after that click on next button.

A new configuration window will open, in this new window write the project Name, set the project location and then click on the next button.

The last window will appear, in this window select the target framework and click on create.

Installing Spreadsheet Library IronXL
Once the Project is created now let's install the free IronXL C# library, below are the steps to install IronXL in you C#
In the Visual Studio go to Tools, a drop-down menu will appear in the dropdown menu click on the NuGet Package manager, a new side menu will appear in the side menu click on the NuGet Package Manager for Solutions.

A new window in appear in this new window go to the browser tab and in the search bar write IronXL, a list of IronXL Packages will appear, select the latest package and click on install.

Edit Excel Files Spreadsheets using IronXL
IronXL offers many spreadsheet manipulation features allow users to change the values of cells, change font size, background color of columns, cell range and many more.
In this section we will load data set a spreadsheet from Excel files and edit then format and manipulate them.
Changing Spreadsheet Entries Using C#
Below is the code example that changes Excel entries using C# programming language code with a few lines of code.
Input Excel File

The provided code utilizes the IronXL library in C# to open source manipulate Spreadsheet in Excel files. It begins by importing the IronXL namespace. Then, it loads an existing Excel file named "test.xlsx" into a WorkBook object.
The default worksheet of this workbook is accessed and assigned to a WorkSheet variable, denoted as ws . The code sets the value of cell A1 in the worksheet to "Hello World Created by IronXL". Finally, the modified Spreadsheet is saved as XLSX files, for example the one we created in this code sample is a new Excel file named "sample.xlsx", which we save using the SaveAs method.
Output File

Adding Spreadsheet Cell Values Using Formulas
IronXL offers a free version of this feature where you can use formulas on spreadsheets and write them to other cells, below is the example using C# IronXL Spreadsheet Library.
This code employs the IronXL library in C# to manipulate an Excel file named "sample.xlsx." It begins by loading the existing Excel file into a WorkBook object.
The default worksheet of this workbook is accessed and assigned to a WorkSheet variable, denoted as ws . Subsequently, a specific range of cells, from D2 to D5, is selected using the ws["D2:D5"] notation.
The sum of the values within this range is then calculated using the Sum() method and stored in a variable named sum .
The code updates cell D6 in the new worksheet with this calculated sum, and cell C6 is assigned the label "Total Price:". Finally, the modified workbook is saved back to the same file, "sample.xlsx," using the SaveAs method.

This article has explored the significance of C# in spreadsheet manipulation and introduced IronXL as a powerful library that enhances the capabilities of C#
We discussed the process of creating a new Visual Studio C# project and outlined the steps to install IronXL. The article also delved into practical examples of using IronXL to edit Excel files, demonstrating how to change cell values and apply formulas to spreadsheet data.
IronXL proves to be a valuable tool for developers working on projects involving data analysis, reporting, and the support for other spreadsheet-related functionalities, providing a seamless and efficient solution exporting data used for C# applications. Its ability to handle spreadsheet operations without the need for Microsoft Office applications or Excel Interop further highlights its versatility and utility in C#
The sample Code and Example of Using Formulas in Spreadsheets can be found here . For the complete tutorial on Editing Spreadsheet visit at the following link .
Opt in to IronXL's trial today to begin exploring all of its features, and see how IronXL can help benefit your projects, whether you're wanting to create a new Excel file, edit existing worksheets, or add new cell data to a spreadsheet or cell range.
If you find IronXL to be beneficial to your working environment and wish to continue using the features it has to offer for all your spreadsheet data manipulation needs and more, you can purchase a license once your trial ends.
- All Articles
- Compare to Other Components
- Excel Tools
Ready to get started? Version: 2023.11 just released

- In Solution Explorer, right-click References, Manage NuGet Packages
- Select Browse and search "IronXL"
- Select the package and install

- Download and unzip IronXL to a location such as ~/Libs within your Solution directory
- In Visual Studio Solution Explorer, right click References. Select Browse, "IronXL.dll"
Have a question? Get in touch with our development team.
Want to deploy IronXL to a live project for FREE?
The trial form was submitted successfully .
Your trial key should be in the email. If it is not, please contact [email protected]
Get started for FREE
No credit card required
Test in a live environment
Test in production without watermarks. Works wherever you need it to.

Fully-functional product
Get 30 days of fully functional product. Have it up and running in minutes.
24/5 technical support
Full access to our support engineering team during your product trial
Get your free 30-day Trial Key instantly.
Trusted by over 2 million engineers worldwide.
Licenses from $749 . Have a question? Get in touch .
Fully-functional product, get the key instantly
PM > Install-Package IronXL.Excel
Your license key has been delivered to the email provided. Contact us
24-Hour Upgrade Offer:
Save 50% on a Professional Upgrade
Go Professional to cover 10 developers and unlimited projects.
Upgrade to Professional

Professional
- 10 developers
- 10 locations
- 10 projects
5 .NET Products for the Price of 2
Total Suite Value:
Upgrade price
After 24 Hrs
- C# Overview
- C# Language Basics
- C# Statements
- C# Collections
- C# File Operations
- C# Crystal Reports
- C# Networking
- C# Threading
- C# Data Providers
- C# DataAdapater
- C# DataView
- C# Remoting
- C# DataGridView
- C# Advanced Concepts
- C# Advanced Programming
Add new worksheet in Excel file
It is possible to dynamically insert a worksheet into an existing Excel file and subsequently include it within the collection of worksheets. The program provided below exemplifies the process of adding a new worksheet to an already established Excel workbook.
Excel Library
To access the object model from Visual C# .NET, you have to add the Microsoft Excel 12.0 Object Library to you project. In the previous chapter you can see a step by step instruction on how to add Excel library to your project.
programmatically Add New Worksheets to Workbooks
In order to add new worksheet to the excel file, this program open an existing Excel file and add a new worksheet in the existing excel file.
Add Excel Worksheet without prompts
To disable the Excel overwrite prompt and suppress prompts and alert messages during the execution of a macro, you can incorporate the following code. By setting the DisplayAlerts property to False, you can prevent Excel from displaying prompts that require user response. In situations where user input is required, Microsoft Excel will proceed with the default response. Following the completion of the execution process, Microsoft Excel automatically resets the DisplayAlerts property to True, unless cross-process code is being executed.
Programmatically Select Worksheets
You can Programmatically select Worksheet and set focus on that worksheet when user open the Excel document.
Above method shows how to select a specified worksheet, in this way you can select any existing worksheet in an Excel document.
Finally, we have to properly clean up Excel interop objects or release Excel COM objects. Here using a function releaseObject() to clean up the Excel object properly.
The following source code shows how to insert new worksheet in an excel file
- How to create Excel file in C#
- How to open an Excel file in C#
- How to read an Excel file using C#
- How to delete worksheet from an excel file
- How to format an Excel file using C#
- How to insert a picture in excel from C# App
- How to insert a background picture in excel
- How to create Excel Chart from C#
- How to export excel chart from C#
- How to excel chart in C# picturebox
- C# data validation input box in excel file
- Read and Import Excel File into DataSet or DataTable
- How to insert data to Excel file using OLEDB
- How to update data in Excel file using OLEDB
- How to export databse to excel file
- How to export DataGridView to excel file
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
How to automate Microsoft Excel from Microsoft Visual C#.NET
- Applies to: Microsoft Excel
This article demonstrates how to create an Automation client for Microsoft Excel by using Microsoft Visual C# .NET.
More Information
Automation is a process that permits applications that are written in languages such as Visual C# .NET to programmatically control other applications. Automation to Excel permits you to perform actions such as creating a new workbook, adding data to the workbook, or creating charts. With Excel and other Microsoft Office applications, virtually all of the actions that you can perform manually through the user interface can also be performed programmatically by using Automation.
Excel exposes this programmatic functionality through an object model. The object model is a collection of classes and methods that serve as counterparts to the logical components of Excel. For example, there is an Application object, a Workbook object, and a Worksheet object, each of which contain the functionality of those pieces of Excel. To access the object model from Visual C# .NET, you can set a project reference to the type library.
This article demonstrates how to set the proper project reference to the Excel type library for Visual C# .NET and provides sample code to automate Excel.
Create an Automation Client for Microsoft Excel
Start Microsoft Visual Studio .NET.
On the File menu, click New, and then click Project. Select Windows Application from the Visual C# Project types. Form1 is created by default.
Add a reference to the Microsoft Excel Object Library. To do this, follow these steps:
- On the Project menu, click Add Reference.
- On the COM tab, locate Microsoft Excel Object Library, and click Select.
Note Microsoft Office 2003 includes Primary Interop Assemblies (PIAs). Microsoft Office XP does not include PIAs, but they can be downloaded.
- Click OK in the Add References dialog box to accept your selections. If you are prompted to generate wrappers for the libraries that you selected, click Yes.
On the View menu, select Toolbox to display the toolbox, and then add a button to Form1.
Double-click Button1. The code window for the form appears.
In the code window, replace the following code:
Scroll to the top of the code window. Add the following line to the end of the list of using directives:
Test the Automation Client
- Press F5 to build and to run the program.
- Click Button1 on the form. The program starts Excel and populates data on a new worksheet.
- When you are prompted to enter quarterly sales data, click Yes. A chart that is linked to quarterly data is added to the worksheet.
For more information, See Microsoft Developer Network (MSDN) Web site .
Additional resources
- Spire.Office for .NET
- Spire.OfficeViewer for .NET
- Spire.Doc for .NET
- Spire.DocViewer for .NET
- Spire.XLS for .NET
- Spire.Spreadsheet for .NET
- Spire.Presentation for .NET
- Spire.PDF for .NET
- Spire.PDFViewer for .NET
- Spire.PDFViewer for ASP.NET
- Spire.DataExport for .NET
- Spire.Barcode for .NET
- Spire.Email for .NET
- Spire.OCR for .NET
- Free Spire.Office for .NET
- Free Spire.Doc for .NET
- Free Spire.DocViewer for .NET
- Free Spire.XLS for .NET
- Free Spire.Presentation for .NET
- Free Spire.PDF for .NET
- Free Spire.PDFViewer for .NET
- Free Spire.PDFConverter for .NET
- Free Spire.DataExport for .NET
- Free Spire.Barcode for .NET
- Spire.Office for WPF
- Spire.Doc for WPF
- Spire.DocViewer for WPF
- Spire.XLS for WPF
- Spire.PDF for WPF
- Spire.PDFViewer for WPF
- Order Online
- Download Centre
- Temporary License
- Purchase Policies
- Renewal Policies
- Find A Reseller
- Purchase FAQS
- Support FAQs
- How to Apply License
- License Agreement
- Privacy Policy
- Customized Demo
- Code Samples
- Unsubscribe
- API Reference
- Spire.Doc for Java
- Spire.XLS for Java
- Spire.Presentation for Java
- Spire.PDF for Java
- Become Our Reseller
- Paid Support
- Our Customers
- Login/Register

- Document Operation
- Page Background
- Image and Shape
- Header and Footer
- Program Guide for WPF
- Quick Guide
- Find and Replace
- Quick Start for .NET
- Quick Start for .NET Core
- Quick Start for .NET Standard
- Quick Start for WPF
- View Word Documents
- Data Import/Export
- Conditional Formatting
- Pivot Table
- Smart Marker
- Marker Designer
- EXCEL Pagesetup in C#, VB.NET
- Add Column in Excel Workbook in C#, VB.NET
- Add Row in Excel Workbook in C#, VB.NET
Add Worksheet in Excel Workbook in C#, VB.NET
- Hide Worksheet Column in Excel in C#, VB.NET
- Hide Worksheet Row in Excel in C#, VB.NET
- Set Row Height in Excel Workbook in C#, VB.NET
- Delete Column in Excel in C#, VB.NET
- Delete Row in Excel in C#, VB.NET
- Set Background Color in Excel in C#, VB.NET
- Design Borders in Excel in C#, VB.NET
- Set Cell Fill in Excel in C#, VB.NET
- AutoFit Column in Exce in C#, VB.NET
- Copy Cell Data in Excel in C#, VB.NET
- Replace Data in Excel in C#, VB.NET
- Copy Excel Worksheet in Silverlight
- Silverlight
- Getting Started
- Program Guide
- Paragraph and Text
- Image and Shapes
- Audio and Video
- Comment and Note
- Getting started
- Page Setting
- Extract/Read
- Attachments
- Interaction
- Document Settings
- View PDF Documents
- PDF Document Viewer
- Datatable Export
- ListView Export
- Create and Scan Barcode
- Send, Receive, Extract Emails
- Manipulate Folders
- Recognize Text
- Form Fields
- VB.Net source
The sample demonstrates how to add worksheet in Excel workbook via Spire.XLS.

- Spire.Presentation
- Spire.Office for Java
- Spire.Barcode for Java
- Purchase FAQs
- License Upgrade
- Believe The Users
- Our Service

Excel Worksheet C# Tutorials
Excel C# Spreadsheet Tutorials with Aspose.Cells for .NET allow you to master the powerful features of the Aspose.Cells library to manipulate and manage Excel files in your .NET applications. These tutorials provide step-by-step instructions to help you understand how to create, edit, and format Excel spreadsheets, as well as how to access cell data and apply formulas. With the code examples provided, you will be able to take full advantage of the flexibility and ease of use of Aspose.Cells to meet your specific Excel data processing needs.
Whether you are a beginner or an experienced C# developer, these tutorials will walk you through the process of manipulating Excel spreadsheets using Aspose.Cells. You will learn how to create a new spreadsheet, define cell contents, format data, add charts and much more. The tutorials also cover advanced topics such as managing styles, adding comments, protecting spreadsheets, and generating reports. You will find clear explanations, detailed code examples and practical advice to help you master the use of Aspose.Cells in your projects.

IMAGES
VIDEO
COMMENTS
Ask Question Asked 11 years, 2 months ago Modified 3 years, 3 months ago Viewed 92k times 41 I am trying to add a new worksheet to an Excel workbook and make this the last worksheet in the book in C# Excel Interop. It seems really simple, and I thought the below code would do it:
Use the Add method of the Sheets collection. C# VB C# Copy Excel.Worksheet newWorksheet; newWorksheet = (Excel.Worksheet)Globals.ThisWorkbook.Worksheets.Add (); The new worksheet is a native Worksheet object and not a host item. If you want to add a Worksheet host item, you should add the worksheet at design time.
private void AddWorksheetsAndCopyData () { xlApp = new Excel.Application (); xlApp.Visible = true; Excel.Worksheet addedSheet; string filePath = @"C:\sample.xlsx"; xlWorkBook = xlApp.Workbooks.Open (filePath); //checking if cell value is null - if it is not adding worksheet to copy data to xlWorkBook = xlApp.Workbooks.Add (misValue); ...
public static void AddNewSheet () { Excel.Application xlApp; xlApp = new Excel.Application (); Excel.Worksheet Jobs, Estimates; string filePath = Environment.GetFolderPath (Environment.SpecialFolder.Desktop) + @"\Test.xlsx"; var xlWorkBook = xlApp.Workbooks.Open (filePath); Excel.Worksheet newSheet; //Add a new worksheet to the workbook...
Here is a simple C# class that programatically creates an Excel WorkBook and adds two sheets to it, and then populates both sheets. Finally, it saves the WorkBook to a file in the application root directory so that you can inspect the results...
1 Answer. The shs.Add (shs [3], Type.missing, Type.missing, Type.missing); does not look a valid call as you said you have two sheets in your workbook. You are attempting to add new sheet before third one which does not exist. Refer the documentation. Before - Optional - Variant - An object that specifies the sheet before which the new sheet is ...
Worksheets (1) is the first (leftmost) worksheet in the workbook, and Worksheets (Worksheets.Count) is the last one. All worksheets are included in the index count, even if they're hidden. The worksheet name is shown on the tab for the worksheet. Use the Name property to set or return the worksheet name.
Object. Optional Object. Determines how the new workbook is created. If this argument is a string specifying the name of an existing Microsoft Excel file, the new workbook is created with the specified file as a template. If this argument is a constant, the new workbook contains a single sheet of the specified type.
Add Excel Worksheet To Existing Workbook C# Tutorial Contents [ Hide ] Step 1: Define the Document Directory Step 2: Create a File Stream and Open the Excel File Step 3: Instantiate a Workbook Object Step 4: Add a New Sheet to the Workbook Step 5: Set New Sheet Name Step 6: Save the Excel File Step 7: Close File Stream and Release Resources
Add new worksheets to an existing Microsoft Excel file. Fill data into the cells of each new worksheet. Activate a sheet in the workbook. Save as Microsoft Excel file. Below, are parallel code snippets for VSTO (C#, VB) and Aspose.Cells for .NET (C#, VB), that show how to achieve these tasks. VSTO C# Copy
Step 1: Document Directory Setup Step 2: Instantiating a Workbook Object Step 3: Adding a new worksheet Step 4: Setting the name of the new worksheet Step 5: Saving the Excel file Conclusion In this tutorial, we will explain step by step C# source code to add a new sheet in Excel using Aspose.Cells for .NET.
Excel.Application xlApp; Excel.Workbook xlWorkBook; Excel.Worksheet xlWorkSheet; object misValue = System.Reflection.Missing.Value; xlApp = new Excel.Application (); //xlApp = new Excel._Application.ApplicationClass (); xlWorkBook = xlApp.Workbooks.Add (misValue); xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item (1); xlWork...
Add a Worksheet to an Existing Excel file in C# and VB.NET The following are the steps to add a worksheet to an existing Excel file: Create an instance of Workbook class. Load an Excel workbook using Workbook.LoadFromFile () method. Add a worksheet to the workbook using Workbook.Worksheets.Add (sheetName) method.
Add a Worksheet to a New Excel File in C# and VB.NET. The following steps show how to create a new Excel file and add a worksheet to it: Create an instance of Workbook class. Clear the default worksheets using Workbook.Worksheets.Clear () method. Add a worksheet to the workbook using Workbook.Worksheets.Add (sheetName) method.
Install the C# library for editing Spreadsheets. Utilize the WorkBook.Load method to open an Excel file. Use the Worksheet.Value method to Set the Value of the sell. Apply range.Sum method to sum the values of the cells. Save the newly generated Spreadsheet as .xlsx format using the SaveAs method.
You can programmatically insert a worksheet and then add that worksheet to the collection of worksheets in the existing workbook. The following program shows...
Insert table in new Excel worksheet .net using C#. The below written code working fine for insert a table and its value in Already existing excel file (D:\MySamplefile.xls). private void WriteToExcel () { string connectionString = "Provider=Microsoft.Jet.OleDb.4.0; Data Source=D:\\MySamplefile.xls; Extended Properties=Excel 8.0;"; using ...
On the File menu, point to New, and then click Project. In the templates pane, expand Visual C# or Visual Basic, and then expand Office/SharePoint. Under the expanded Office/SharePoint node, select the Office Add-ins node. In the list of project templates, select Excel 2010 Add-in or Excel 2013 Add-in. In the Name box, type FirstExcelAddIn.
To access the object model from Visual C# .NET, you have to add the Microsoft Excel 12.0 Object Library to you project. In the previous chapter you can see a step by step instruction on how to add Excel library to your project. How to add Excel Library programmatically Add New Worksheets to Workbooks
To do this, follow these steps: On the Project menu, click Add Reference. On the COM tab, locate Microsoft Excel Object Library, and click Select. Note Microsoft Office 2003 includes Primary Interop Assemblies (PIAs). Microsoft Office XP does not include PIAs, but they can be downloaded.
Demo. C# source. VB.Net source. The sample demonstrates how to add worksheet in Excel workbook via Spire.XLS.
Excel Worksheet C# Tutorials. Excel C# Spreadsheet Tutorials with Aspose.Cells for .NET allow you to master the powerful features of the Aspose.Cells library to manipulate and manage Excel files in your .NET applications. ... Add Excel Worksheet To Existing Workbook C# Tutorial: Easily add a new sheet to an existing Excel workbook using Aspose ...