Results 1 to 9 of 9

Thread: Best way to export to Excel from vb.net

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    425

    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

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    425

    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:
    1. myConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\test\test.xls;Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"""
    2.         Try
    3.             Dim conn As New OleDbConnection
    4.             conn.ConnectionString = myConnectionString
    5.             conn.Open()
    6.             Dim cmd1 As New OleDbCommand
    7.             cmd1.Connection = conn
    8.             'cmd1.CommandText = "CREATE TABLE test (sKontraktnummer char(255), sFendertyp char(255), sFendernr char(255), nCykelnummer char(255), dtTime char(255), Id char(255))"
    9.             'cmd1.ExecuteNonQuery()
    10.             conn.Close()
    11.         Catch ex As OleDbException
    12.             MsgBox(ex.Message)
    13.         End Try

    Is there something that i am doing wrong?

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    425

    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:
    1. ' Open a connection to the sample Northwind database.
    2.         Dim conn As New System.Data.OleDb.OleDbConnection( _
    3.               "Provider=sqloledb;Data Source=(local);Initial Catalog=NorthWind;Integrated Security=SSPI;")
    4.  
    5.         conn.Open()
    6.         Dim cmd As New System.Data.OleDb.OleDbCommand()
    7.         cmd.Connection = conn
    8.  
    9.         cmd.CommandText = "SELECT * INTO [Excel 8.0;Database=C:\test.xls].[Products]" & _
    10.                         "FROM [Products]"
    11.         cmd.ExecuteNonQuery()
    12.         conn.Close()
    13.  
    14.     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.

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    425

    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:
    1. ' Open a connection to the sample Northwind database.
    2.         Dim conn As New System.Data.OleDb.OleDbConnection( _
    3.               "Provider=sqloledb;Data Source=(local);Initial Catalog=NorthWind;Integrated Security=SSPI;")
    4.  
    5.         conn.Open()
    6.         Dim cmd As New System.Data.OleDb.OleDbCommand()
    7.         cmd.Connection = conn
    8.  
    9.         cmd.CommandText = "SELECT * INTO [Excel 8.0;Database=C:\test.xls].[Products]" & _
    10.                         "FROM [Products]"
    11.         cmd.ExecuteNonQuery()
    12.         conn.Close()
    13.  
    14.     End Sub

  7. #7
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    425

    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 Attached Images  

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    425

    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
  •  



Click Here to Expand Forum to Full Width