1 ImportPYMYSQL,XLWT2 defExport_excel (table_name):3Host, user, passwd, db ='118.24.3.40','Jxz','123456','Jxz'4Coon = Pymysql.connect (user=user,host=host,port=3306,passwd=passwd,db=db,charset='UTF8' )5Cur =coon.cursor ()6sql ='select * from%s;'%table_name7Cur.execute (SQL)#Execute SQL8Fileds = [Filed[0] forFiledinchCur.description]#All Fields9All_data =Cur.fetchall ()TenBook =XLWT. Workbook () OneSheet = Book.add_sheet ('Sheet1') A #col = 0 - #For filed in Fileds: #写表头 - #sheet.write (0,col,filed) the #col+=1 - forCol,filedinchEnumerate (fileds):#writing a table header - sheet.write (0,col,filed) -row = 1#control number of rows + forDatainchAll_data:#Number of rows - forCol, filedinchEnumerate (data):#Number of columns + sheet.write (Row, col, filed) ARow+=1#each time you finish a line, the number of rows plus 1 atBook.save ('%s.xls'%table_name) -Export_excel ('app_student')
Python-excel Module