MySQL Forums
Forum List  »  Newbie

Re: Connecting to MySQL from Excel 2007
Posted by: Jay Alverson
Date: March 27, 2009 07:53AM

It's very easy if you use Excel Macros or VBA... if you don't then sorry...

This uses Excel's QueryTables feature.

Sub FileDSN()
    With ActiveSheet.QueryTables.Add(Connection:="ODBC;FileDSN=C:\Writing\Winbatch\MYSQL\VBS-MySQL.dsn;", Destination _
        :=Range("A1"))
		.CommandText = "SELECT * from orders"
        .Name = "MySQL Test 1"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .PreserveColumnInfo = True
        .SourceConnectionFile = "C:\Writing\Winbatch\MYSQL\VBS-MySQL.dsn"
        .Refresh BackgroundQuery:=False
    End With
End Sub

The FileDSN is just a text file you can create in Notepad or via a script and doesn't require any user interaction. It must have the .dsn extension

The contents looks like this...

[ODBC]
DRIVER=MySQL ODBC 5.1 Driver
database=gurps
uid=Winbatch
pwd=WBTPassword
server=localhost
port=3306

and that's it.

All you have to do is change the path/name of the .dsn to yours and change
all the database particulars in the .dsn file to fit your setup.

Took me about 1 minute.

You can use Excel to record a macro of adding a QueryTable via the DATA menu
then hitting IMPORT EXTERNAL DATA --> NEW DATABASE QUERY. The result is the
above macro.

>

Thanks, Jay

Options: ReplyQuote


Subject
Written By
Posted
Re: Connecting to MySQL from Excel 2007
March 27, 2009 07:53AM


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.