Create an Excel 2007 file in VB.NET 2005

By: Issac Emailed: 1642 times Printed: 2109 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

Excel file can be automated or worked upon by Auomation to Excel 2007,It can be done in two ways through Excel Object Model and using Microsoft Jet Engine to connect to Excel, through this we can achieve creating new wokbook add data to it or create charts in it, For creating an Excel 2007 worksheet in VB.NET 2005 , you have to add the Microsoft Excel 12.0 Object Library in you project.

From the following pictures to show how to add Excel reference library in your project.

1. Create a new project and add a button to the Form.

Select reference dialouge from Project menu 

excel_reference.jpg


Select Microsoft Excel 12.0 Object Library and click OK button 


excel_library.jpg
Now you can start coding to create a new 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
        Dim xlWorkBook As Excel.Workbook
        Dim xlWorkSheet As Excel.Worksheet
        Dim misValue As Object = System.Reflection.Missing.Value

        xlApp = New Excel.ApplicationClass
        xlWorkBook = xlApp.Workbooks.Add(misValue)
        xlWorkSheet = xlWorkBook.Sheets("sheet1")
        xlWorkSheet.Cells(1, 1) = "http://vb.net-informations.com"
        xlWorkSheet.SaveAs("C:\vbexcel.xlsx")

        xlWorkBook.Close()
        xlApp.Quit()

        releaseObject(xlApp)
        releaseObject(xlWorkBook)
        releaseObject(xlWorkSheet)

        MsgBox("Excel file created , you can find the file c:\")
    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

In the code, Imports Excel = Microsoft.Office.Interop.Excel - we assign the excel reference to a variable called Excel. 
When you execute this program, the file created in the c:\ of your system.


VB.net Home | All VB.net Tutorials | Latest VB.net Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(1)


1. View Comment

Nice post. Can you tell me how can i add shape in Excel 2007 through vb.net (Visual Studio 2010) in a merge cell. I merged 4 cell (Range B2:B3 - B2-C2 C2:C3) now i want to insert shape in the merge cell.

View Tutorial          By: Sonia at 2012-06-08 04:57:18

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Issac
Save/Write/Read image file from/to a database using Java program
How to modify the objects using java classes
Java program for Cloning
Java program for changeable wrapper class
A tutorial on Chat Server and Chat Client in VB.net
Chat Server in VB.net
Chat client in VB.net
How to open and read an XML file in VB.net
How to create an XML file in VB.net
XML and VB.net
How to create an XML file in VB.NET using Dataset
Multi Threaded Client Socket Programming in VB.net
Multi Threaded Server Socket Programming in VB.net
Multi threaded Socket Programming in VB.net
Client Socket Program sample in VB.net

More Tutorials in VB.net
Scope in VB.net
Unstructured Exception Handling in VB.net
Structured Exception Handling in VB.net
Creating Sub Procedures in VB.net
Creating Functions in VB.net
Passing a Variable Number of Arguments to Procedures in VB.net
Specifying Optional Arguments with default values in Procedures in VB.net
Preserving a Variable's Values between Procedure Calls in VB.net
Procedure Delegates in VB.net
Properties in VB.net
Understanding Scope in VB.net
Using Resume Next and Resume Line in VB.net
Using On Error GoTo 0 in VB.net
Getting an Exception's Number and Description in VB.net
Raising an Exception Intentionally in VB.net

More Latest News
Most Viewed Articles (in VB.net )
Arrays and Dynamic Arrays in VB.net
For Each…Next Loop in VB.net
File directory operationst in VB.net
Format Excel 2007 Page in VB.NET
File Extensions Used in VB .NET
Data types in VB.net
Converting between Data Types in VB.net
Operators in VB.net
Sub Procedures and Functions in VB.net
Queue data structure in VB.net
String endswith, concat in VB.net
Create an Excel 2007 file in VB.NET 2005
To read the entire worksheet in an Excel workbook through VB.net Code
Using Excel 2007 Chart VB.net Picture Box
Using Excel 2007 DataBar in VB.net
Most Emailed Articles (in VB.net)
Multi threaded Socket Programming in VB.net
How to create an XML file in VB.net
How To Connect To A MySql Database in VB.net
Vb.net binary reader and writer
How to Open and Edit Cells in an Excel 2007 file in VB.NET
To read the entire worksheet in an Excel workbook through VB.net Code
Format Excel 2007 Page in VB.NET
Using Excel 2007 Chart in VB.NET 2005
How to send email using VB.NET code
How to find IP Address of Host using .net code
Chat client in VB.net
A tutorial on Chat Server and Chat Client in VB.net
Creating List Views in Code using VB.net
Structured Exception Handling in VB.net
ArrayList data structure in VB.net