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');