Add new worksheet in Excel file

Adding new worksheets and setting which sheet is active are common and simple tasks that programmers need to know how to perform. The following program shows how to add a new worksheet to an existing Excel file and active the selected worksheet using VB.Net Application.
Excel Library
To access the object model from VB.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 LibraryProgrammatically Add New Worksheets to Excel
In order to add new worksheet to the excel file, this VB.Net program open an existing Excel file and add a new worksheet in the existing workbook.
Disable Excel overwrite promt - VB.Net

DisplayAlerts set to False for suppress prompts and alert messages while a macro is running. When a message need a response from the end user, Excel chooses the default response. After you finish the running process, Excel sets this property to True, unless you are running cross-process code.
Programmatically Select Worksheets - VB.Net
You can Programmatically select Worksheet and set focus on that worksheet when user open the Excel file.
Above method shows how to select (focus) a specified worksheet, in this way you can select any existing worksheet from an Excel document.
releaseObject()
Finally, we have to properly clean up Excel interop objects or release Excel COM objects after your program over. Here using a function releaseObject() to clean up the Excel object properly.
The following VB.Net source code shows how to insert new worksheet in an excel file
Imports Excel = Microsoft.Office.Interop.Excel Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim xlApp As Excel.Application = New Microsoft.Office.Interop.Excel.Application() If xlApp Is Nothing Then MessageBox.Show("Excel is not properly installed!!") Return End If xlApp.DisplayAlerts = False Dim filePath As String = "d:\test.xlsx" Dim xlWorkBook As Excel.Workbook = xlApp.Workbooks.Open(filePath, 0, False, 5, "", "", _ False, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", True, False, 0, _ True, False, False) Dim worksheets As Excel.Sheets = xlWorkBook.Worksheets Dim xlNewSheet = DirectCast(worksheets.Add(worksheets(1), Type.Missing, Type.Missing, Type.Missing), Excel.Worksheet) xlNewSheet.Name = "newsheet" xlNewSheet.Cells(1, 1) = "New sheet content" xlNewSheet = xlWorkBook.Sheets("sheet1") xlNewSheet.Select() xlWorkBook.Save() xlWorkBook.Close() releaseObject(xlNewSheet) releaseObject(worksheets) releaseObject(xlWorkBook) releaseObject(xlApp) MessageBox.Show("New Worksheet Created!") End Sub Private Sub releaseObject(ByVal obj As Object) Try System.Runtime.InteropServices.Marshal.ReleaseComObject(obj) obj = Nothing Catch ex As Exception obj = Nothing Finally GC.Collect() End Try End Sub End Class
- How to create an Excel file in VB.NET
- How to open or read an existing Excel file in VB.NET
- How to read an Excel file using VB.Net
- Delete worksheet from an excel file - VB.Net
- How to Format Excel Page in VB.NET
- How to insert a Picture in Excel through programing on VB.NET
- How to insert a background Picture in Excel through VB.NET
- How to create a Chart in Excel in VB.NET
- How to export a Chart in Excel as Picture file from VB.NET
- How to Excel Chart in VB.NET Picture Box
- How to Excel DataBar in VB.NET
- How to Excel Data Validation Input Message
- Read and Import Excel File into DataSet or DataTable in vb.net
- How to insert cell data in an Excel file using OLEDB
- How to update cell data in an Excel file using OLEDB
- How to export from database to excel
- How to export from DataGridView to excel