Mar 21st, 2006, 04:23 AM
#1
Thread Starter
Hyperactive Member
Best way to export to Excel from vb.net
Hi,
I have a program with a datagrid and a load to excel button.
I have written codes to loop through the datatable and insert the content to excel, but it took 30 secs just to load 700 rows x 5 columns of data.
Are there any faster way to do this? Besides exporting it straight from SQL server.
thanks
Mar 21st, 2006, 04:28 AM
#2
Re: Best way to export to Excel from vb.net
You could use a ADO.NET connection to a Excel Workbook. Then execute an INSERT INTO sql statment to insert all the data just like you do with a database.
VB/Office Guru™ (AKA: Gangsta Yoda ™ ® )
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it!
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6
Mar 21st, 2006, 06:31 AM
#3
Thread Starter
Hyperactive Member
Re: Best way to export to Excel from vb.net
Hi,
I tried creating an excel table with the following codes but i will get a "You can't modify database, as it is a read-only table"
VB Code:
myConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\test\test.xls;Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"""
Try
Dim conn As New OleDbConnection
conn.ConnectionString = myConnectionString
conn.Open()
Dim cmd1 As New OleDbCommand
cmd1.Connection = conn
'cmd1.CommandText = "CREATE TABLE test (sKontraktnummer char(255), sFendertyp char(255), sFendernr char(255), nCykelnummer char(255), dtTime char(255), Id char(255))"
'cmd1.ExecuteNonQuery()
conn.Close()
Catch ex As OleDbException
MsgBox(ex.Message)
End Try
Is there something that i am doing wrong?
Mar 21st, 2006, 10:44 AM
#4
Thread Starter
Hyperactive Member
Re: Best way to export to Excel from vb.net
ok, i have tried the SELECT INTO sql statement but i am still getting
Specified owner name 'Excel 8.0;Database=C:\test.xls' either does not exist or you do not have permission to use it.
here's the modified code.
VB Code:
' Open a connection to the sample Northwind database.
Dim conn As New System.Data.OleDb.OleDbConnection( _
"Provider=sqloledb;Data Source=(local);Initial Catalog=NorthWind;Integrated Security=SSPI;")
conn.Open()
Dim cmd As New System.Data.OleDb.OleDbCommand()
cmd.Connection = conn
cmd.CommandText = "SELECT * INTO [Excel 8.0;Database=C:\test.xls].[Products]" & _
"FROM [Products]"
cmd.ExecuteNonQuery()
conn.Close()
End Sub
The above should create a new worksheet in test.xls and it should contain all the data from the products table in northwind. By the way there is no access restrictions on the excel file.
Anyone? thanks
Last edited by hyper88; Mar 21st, 2006 at 10:48 AM .
Mar 21st, 2006, 02:00 PM
#5
Re: Best way to export to Excel from vb.net
Are you using your Excel code still to connect or automate it at all in your project? That would cause this error.
VB/Office Guru™ (AKA: Gangsta Yoda ™ ® )
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it!
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6
Mar 21st, 2006, 02:05 PM
#6
Thread Starter
Hyperactive Member
Re: Best way to export to Excel from vb.net
thanks for replying,
This are the only codes i am using in the project. I have commented rest of the codes which tries to do the same thing. Anyway the original code is from microsoft here but i also got the same error even following the sample there.
VB Code:
' Open a connection to the sample Northwind database.
Dim conn As New System.Data.OleDb.OleDbConnection( _
"Provider=sqloledb;Data Source=(local);Initial Catalog=NorthWind;Integrated Security=SSPI;")
conn.Open()
Dim cmd As New System.Data.OleDb.OleDbCommand()
cmd.Connection = conn
cmd.CommandText = "SELECT * INTO [Excel 8.0;Database=C:\test.xls].[Products]" & _
"FROM [Products]"
cmd.ExecuteNonQuery()
conn.Close()
End Sub
Mar 21st, 2006, 02:09 PM
#7
Re: Best way to export to Excel from vb.net
Try adding a dollar sign after the sheet name.
.[Products$]
VB/Office Guru™ (AKA: Gangsta Yoda ™ ® )
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it!
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6
Mar 21st, 2006, 02:14 PM
#8
Thread Starter
Hyperactive Member
Re: Best way to export to Excel from vb.net
Still got the same error after adding the $ sign behind the sheet name.
Attached is the error msg.
Attached Images
Mar 21st, 2006, 10:30 PM
#9
Thread Starter
Hyperactive Member
Re: Best way to export to Excel from vb.net
up..
Or anyone has any fast way to export a result query to Excel. I have tried looping through datatable but it is pretty slow 30secs for 1000 records and 20 secs after some optimisation. I have also tried copyfromRecordset method but i would prefer to do it in the .net way. Are there any built in functions that does this? thanks
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Click Here to Expand Forum to Full Width