MySQL Forums
Forum List  »  Newbie

Re: Export mySQL to Excel
Posted by: Jay Alverson
Date: October 31, 2008 08:35AM

This works on my pc. Obviously you'll have to change the following in the connection string...

SERVER= {wherever your MySQL server is located}
DATABASE= {whatever db you use}
UID=
PWD=
sqlstr = "SELECT * FROM testtable;" {change the table name}

' connect to the test db, with UID/PWD and select all records from testtable and place the contents of the recordset into Excel...

dim MySQLConn, sqlstr, MySQLRS
Set MySQLConn = CreateObject("ADODB.Connection")
MySQLConn.open = "DRIVER={MySQL ODBC 5.1 Driver};SERVER=localhost;DATABASE=test;UID=Winbatch;PWD=WBTPassword;OPTION=35;"
sqlstr = "SELECT * FROM testtable;"
Set MySQLRS = CreateObject("ADODB.Recordset")
MySQLRS.open sqlstr, MySQLConn, 1,4,1

dim Excel
Set Excel = CreateObject("Excel.Application")
Excel.visible = True
Excel.Workbooks.Add
Excel.Workbooks(1).Worksheets(1).Range("A2").CopyFromRecordset MySQLRS


MySQLConn.close() ' close the connection
Set MySQLConn = Nothing

See the VBA example in the previous post to insert the Field Names as a header row.

You can also get ADO to create a .DSN file using an extra keyword in the connection string. This way you don't have to supply a connection string in subsequent connections, all the info can be in the DSN. Use the MS-SQL ref below and scroll down to "SaveFile" and the following code. You'd have to go in by hand and add the PWD=WBTPassword {change to yours} pair into the .DSN file.

' now open a connection using a dsn file without that big, long string...
MySQLConn.open = "FileDSN=J:\Temporary\WBT-MySQL.dsn"

some of the MS-SQL Server pairs work for MySQL...

http://msdn.microsoft.com/en-us/library/ms130822.aspx
and
http://dev.mysql.com/doc/refman/5.0/en/connector-net-examples-mysqlconnection.html



>

Thanks, Jay

Options: ReplyQuote


Subject
Written By
Posted
October 31, 2008 03:48AM
October 31, 2008 08:13AM
November 19, 2008 10:58AM
November 19, 2008 12:16PM
November 19, 2008 12:40PM
November 19, 2008 03:38PM
November 20, 2008 04:07PM
November 20, 2008 04:22PM
December 02, 2008 04:47PM
December 02, 2008 09:19PM
December 03, 2008 10:00AM
December 03, 2008 11:13AM
December 14, 2008 10:19PM
Re: Export mySQL to Excel
October 31, 2008 08:35AM
November 01, 2008 05:01AM
November 01, 2008 07:39AM
November 01, 2008 02:18PM
November 02, 2008 12:09AM
November 02, 2008 07:00AM
November 02, 2008 08:59AM
November 02, 2008 10:57AM
November 02, 2008 12:44PM
November 03, 2008 09:42PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.