import MySQLdb
import xlwt
def outMySQL(file_name):
wb =
xlwt.Workbook()
sh = wb.add_sheet(
'sheet 1',cell_overwrite_ok=
True)
db = MySQLdb.connect(
"localhost",
"root",
"123",
"STUDENTS",use_unicode=1,charset=
'utf8')
cursor =
db.cursor()
sql =
'''SELECT * FROM STUDENTS '''
cursor.execute(sql)
data =
cursor.fetchall()
value =
cursor.description
rows =
len(data)
cols = len(map(list,zip(*
data)))
for v
in range(0,len(value)):
sh.write(0,v,value[v][0])
for i
in range(1,rows+1
):
for j
in range(0,cols):
sh.write(i,j,data[i-1
][j])
wb.save(file_name)
cursor.close()
db.commit()
db.close()
if __name__==
"__main__":
outMySQL('/home/wangzhch/excel.xls');
转载于:https://www.cnblogs.com/iwangzhch/p/3988579.html
相关资源:python将excel数据导入数据库
转载请注明原文地址: https://win8.8miu.com/read-1482557.html