mac excel mysql数据库,在Mac Excel 2011中从Mysql DB获取数据的VBA代码

I was using ADODB code in Windows to fetch data from Mysql Database and it was working fine. However, I can not seem to get my Excel workbook (using ADODB) to work with Excel Mac 2011. After a lot of googling, I found an ODBC connector from Actual tech and I was able to fetch three rows using Microsoft Query. But I want this to be done using VBA code but have been unable to do so. Has anyone gotten this to work? If yes, can you please provide me with a sample code. Thanks in Advance !!

P.S: I know there is a similar existing question but the link provided in the answer is no longer working. hence, I have asked a new question

Also, If anyone needs the link to do it through Microsoft Query, here is the link:

http://www.agentjim.com/MVP/Excel/2011Relational7Queries.html

Here is the code I use in Windows:

Sub getMysqlDBdata()

Dim Cn As Object

Dim sqlQa as string

dim temparray1 as variant

Source = "MySQL"

mysql_driver = "MySQL ODBC 5.2 ANSI Driver"

sqlQa = "select * from test.TestTable;"

Set Cn = CreateObject("ADODB.Connection")

Set rs = CreateObject("ADODB.Recordset")

Cn.Open "Driver={" & "MySQL ODBC 5.2 ANSI Driver" & "};Server=" & "127.0.01" & ";Database= test;UID=" & "root" & ";PWD=" & "12345"

rs.Open sqlQa, Cn, adOpenStatic

temparray1 = rs.GetRows()

rs.Close

Set rs = Nothing

End Sub

解决方案

After a lot of googling I came across this sample code provided by Bryan Duchesne in MSDN:

Sub TestSqlConnection()

Dim sqlstring As String

Dim connstring As String

Dim sLogin As String

sLogon = "Uid=myUserID;Pwd=myPassowrkd;"

sqlstring = "select * from zitemloc"

connstring = "ODBC;DSN=myDSN;" & sLogon

ActiveSheet.Range("B1:t2000").Clear

Dim qt As QueryTable

For Each qt In ActiveSheet.QueryTables

qt.Delete

Next qt

With ActiveSheet.QueryTables.Add(Connection:=connstring, Destination:=Range("B1"), Sql:=sqlstring)

.BackgroundQuery = False

.Refresh

End With

Set qt = ActiveSheet.QueryTables(1)

Dim rowCount As Integer

rowCount = UBound(qt.ResultRange.Value)

Dim ix, iy As Integer

Dim data As Variant

Dim colCount As Integer

colCount = qt.ResultRange.Columns.Count

For ix = 1 To rowCount

If Not IsArray(data) Then

ReDim data(rowCount - 1, colCount - 1)

End If

For iy = 1 To qt.ResultRange.Columns.Count

data(ix - 1, iy - 1) = qt.ResultRange.Value2(ix, iy)

Next

Next

End Sub

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要从 MySQL 数据库获取数据并将其导出到 Excel,你可以使用 Flask、MySQL Connector 和 openpyxl 库。以下是一个简单的示例代码: ```python from flask import Flask, make_response import mysql.connector from openpyxl import Workbook app = Flask(__name__) @app.route('/export-excel') def export_excel(): # 连接到 MySQL 数据库 conn = mysql.connector.connect( host='localhost', user='root', password='password', database='database' ) # 从数据库获取数据 cursor = conn.cursor() cursor.execute('SELECT * FROM table') data = cursor.fetchall() # 创建一个 Workbook 对象 wb = Workbook() # 选择默认的工作表 ws = wb.active # 将数据添加到工作表 for row in data: ws.append(row) # 将 Workbook 对象保存到内存 output = BytesIO() wb.save(output) # 创建一个响应对象并将 Excel 文件作为附件添加到响应 response = make_response(output.getvalue()) response.headers['Content-Disposition'] = 'attachment; filename=example.xlsx' response.headers['Content-Type'] = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' # 关闭数据库连接并返回响应 cursor.close() conn.close() return response if __name__ == '__main__': app.run() ``` 在上面的代码,我们首先连接到 MySQL 数据库,并从表获取数据。然后,我们创建一个 Workbook 对象,并将数据添加到工作表。接下来,我们将 Workbook 对象保存到内存,并创建一个响应对象,将 Excel 文件作为附件添加到响应。最后,我们关闭数据库连接并返回响应。请根据需要更改代码数据库连接和查询语句。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值