Reprint: Tingzuhuitou
#Coding:utf8ImportSYS#__desc__ = Exporting data from a database to an Excel data tableImportXLWTImportPymysqldefExport (host, user, password, dbname, Table_name,output_path): Conn= Pymysql.connect (host, user, password, dbname, charset='UTF8') Cursor=conn.cursor ()Print(cursor.rownumber) Count= Cursor.execute ('SELECT * from'+table_name)Print(Cursor.rownumber)Print(Cursor.lastrowid)Print(count)#Reset The position of the cursorCursor.scroll (0,mode='Absolute') Print(Cursor.rownumber)#Search All ResultsResults =Cursor.fetchall ()#Get the data field name inside MySQLFields =cursor.description Workbook=XLWT. Workbook () sheet= Workbook.add_sheet ('Table_'+table_name,cell_overwrite_ok=True)#Write the field information forFieldinchRange (0, Len (fields)): Sheet.write (0, field, fields[field][0])#Get and Write data segment informationrow = 1Col=0 forRowinchRange (1,len (results) +1): forColinchRange (0, Len (fields)): Sheet.write (Row, col, u'%s'% results[row-1][col]) workbook.save (Output_path)#Results Testif __name__=="__main__": Export ('localhost','Root','123456','Test','Test_input','E:/test_input.xls')
Python3 Implementing MySQL Export Excel