日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

mysql写到excel_使用Python从 MySQL写数据到Excel

發布時間:2023/12/19 数据库 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mysql写到excel_使用Python从 MySQL写数据到Excel 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

直接上代碼:#!/usr/bin/env?python

#coding:utf-8

import?xlwt

import?MySQLdb

import?datetime

database?=?MySQLdb.connect(host=‘192.168.1.30‘,user=‘root‘,passwd=‘123456‘,db=‘crm‘)

#設置字符集

database.set_character_set(‘utf8‘)

cursor?=?database.cursor()

cursor.execute(‘SET?NAMES?utf8;‘)

cursor.execute(‘SET?CHARACTER?SET?utf8;‘)

cursor.execute(‘SET?character_set_connection=utf8;‘)

starttime?=?datetime.datetime.now()

print?‘開始時間:%s‘?%?(starttime)

#通過SQL得到該表有多少行,如果想取出指定的數據,只需要在后面加where條件即可。

sql2?=?‘select?count(*)?from?bill_test;‘;

cursor.execute(sql2)

count_rows=cursor.fetchone()[0]

wbk?=?xlwt.Workbook(encoding=‘utf-8‘,style_compression=0)

sheet?=?wbk.add_sheet(‘sheet?1‘,?cell_overwrite_ok=True)

#設置寫excel的樣式

style?=?xlwt.XFStyle()

font?=?xlwt.Font()

font.name?=?‘Times?New?Roman‘

#0x0190設置字體為20,默認為0x00C8?字體為10?,0x00C8為十六進制的數字

font.height?=?0x0190

font.bold?=?True

style.font?=?font

#查詢得到該表有多少列

query_colums="select?count(*)?from?information_schema.COLUMNS?where?TABLE_SCHEMA=‘crm‘?and?table_name=‘bill_test‘;"

cursor.execute(query_colums)

count_cols?=?cursor.fetchone()[0]

sql?=?‘select?member_id,?name,?tel,?phone,?dq_datetime,?address,?parking?from?bill_test;‘

cursor.execute(sql)

#定義所有的列名,共7列

columnName?=?[‘賬號‘,‘名稱‘,‘電話‘,‘手機‘,‘到期日期‘,‘地址‘,‘園區名稱‘]

#將列名插入表格,共7列

for?i?in?range(len(columnName)):

sheet.write(0,i,columnName[i],style)

#通過循環取出每一行數據,寫入excel

for?i?in?range(1,count_rows-1):

data?=?cursor.fetchone()

for?j?in?range(0,count_cols-1):

sheet.write(i,j,data[j],style)

cursor.close()

database.close()

wbk.save(‘C:\Users\XUWU\Desktop\data01.xls‘)

endtime=datetime.datetime.now()

print?‘結束時間:%s‘?%?(endtime)

print?‘用時:%s?秒‘?%?(endtime-starttime)

執行情況:

原文:http://xumingjiang.blog.51cto.com/703960/1884261

總結

以上是生活随笔為你收集整理的mysql写到excel_使用Python从 MySQL写数据到Excel的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。