How to Create Excel File(.xls) from VB.Net App

C

Cathy

Hi All,

Can anyone tell me how to create an Excel (.xls) file from a VB.Net app? I
have a SQL Server database that contains a table with the data I want to
export. How do I create the file? Any code samples?

Thanks for your help!
 
P

Paul Clement

¤ Hi All,
¤
¤ Can anyone tell me how to create an Excel (.xls) file from a VB.Net app? I
¤ have a SQL Server database that contains a table with the data I want to
¤ export. How do I create the file? Any code samples?

See if the following works for you:

ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=e:\My Documents\Orders.xls;Extended Properties=""Excel 8.0;HDR=NO"""

Dim ExcelConnection As New System.Data.OleDb.OleDbConnection(ConnectionString)
ExcelConnection.Open()

Dim ExcelCommand As System.Data.OleDb.OleDbCommand = ExcelConnection.CreateCommand()

ExcelCommand.CommandText = "SELECT * INTO [Orders] FROM [Orders] IN '' [ODBC;Driver={SQL
Server};Server=(local);Database=Northwind;Trusted_Connection=yes];"
ExcelCommand.CommandType = CommandType.Text
ExcelCommand.ExecuteNonQuery()

ExcelConnection.Close()


Paul ~~~ (e-mail address removed)
Microsoft MVP (Visual Basic)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top