我正在使用连接到MS SQL服务器pyodbc.此外,我正在尝试使用写入Excel 2007/10 .xlsx文件openpyxl.
这是我的代码(Python 2.7):
import pyodbc
from openpyxl import Workbook
cnxn = pyodbc.connect(host = 'xxx',database='yyy',user='zzz',password='ppp')
cursor = cnxn.cursor()
sql = "SELECT TOP 10 [customer clientcode] AS Customer, \
[customer dchl] AS DChl, \
[customer name] AS Name, \
...
[name3] AS [name 3] \
FROM mydb \
WHERE [customer dchl] = '03' \
ORDER BY [customer id] ASC"
#load data
cursor.execute(sql)
#get colnames from openpyxl
columns = [column[0] for column in cursor.description]
#using …Run Code Online (Sandbox Code Playgroud)