日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

python查询结果写入excel_python实现查询的数据写入到excel

發(fā)布時(shí)間:2025/3/20 52 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python查询结果写入excel_python实现查询的数据写入到excel 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

#coding=utf-8

import sys

import xlwt

import pymysql as MySQLdb #這里是python3 如果你是python2.x的話,import MySQLdb

import datetime

host = '192.168.10.109'

user = 'root'

pwd = ''

port = 3306

db = 'com66nao_mi'

sheet_name = 'report'

out_path = r'D:\SQL\aaa'+'.xls'

print(out_path)

sql = '''select * from mi_orgs;'''

def export():

conn = MySQLdb.connect(host,user,pwd,db,charset='utf8')

cursor = conn.cursor()

count = cursor.execute(sql)

print("查詢出" + str(count) + "條記錄")

#來重置游標(biāo)的位置

cursor.scroll(0,mode='absolute')

#搜取所有結(jié)果

results = cursor.fetchall()

# 獲取MYSQL里面的數(shù)據(jù)字段名稱

fields = cursor.description

workbook = xlwt.Workbook() # workbook是sheet賴以生存的載體。

sheet = workbook.add_sheet(sheet_name,cell_overwrite_ok=True)

# 寫上字段信息

for field in range(0,len(fields)):

sheet.write(0,field,fields[field][0])

# 獲取并寫入數(shù)據(jù)段信息

row = 1

col = 0

for row in range(1,len(results)+1):

for col in range(0,len(fields)):

sheet.write(row,col,u'%s'%results[row-1][col])

workbook.save(out_path)

#結(jié)果測試

if __name__=="__main__":

export()

總結(jié)

以上是生活随笔為你收集整理的python查询结果写入excel_python实现查询的数据写入到excel的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。