Python将读取的SQL查询记录写入xls文件中

    import pymssql
    from xlwt import Workbook
    conn = pymssql.connect(charset='utf8',host='127.0.0.1:13000',user='username',password='password',database='databasename')
    cur = conn.cursor()
    sqlstr ='select A ,B from tablename'
    cur.execute(sqlstr)
    rows=cur.fetchall()

    w=Workbook(encoding='utf-8')
    ws=w.add_sheet('Sheet1')
    for i,row in enumerate(rows):
        for j,cell in enumerate(row):
            ws.write(i,j,cell)

    w.save('xlsname.xls')  

你可能感兴趣的:(Python)