Add new worksheet in Excel file

How to create multiple worksheets in Excel file using VB.Net

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 Library

Programmatically 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.

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"

Disable Excel overwrite promt - VB.Net

xlApp.DisplayAlerts = False
Save Excel file without asking to overwrite it in 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.

xlNewSheet = xlWorkBook.Sheets("sheet1") xlNewSheet.Select()

Above method shows how to select (focus) a specified worksheet, in this way you can select any existing worksheet from an Excel document.

releaseObject() How to programmatically Add New Worksheets to Workbooks from VB.Net application

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