主要引用的包有:
MySQLdb
xlrd
代码为:
# coding=utf-8 import MySQLdb import xlrd book = xlrd.open_workbook("f:\\test\\excel.xlsx") sheet = book.sheet_by_name("Sheet1") conn= MySQLdb.connect( host='localhost', port = 3306, user='root', passwd='123456', db ='test', ) cursor = conn.cursor() sql = "insert into test1(name,age) values(%s,%s)" # 循环获取数据并保存数据 for r in range(1, sheet.nrows): name = sheet.cell(r,0).value age = sheet.cell(r,1).value cursor.execute(sql,(name,age)) cursor.close() conn.commit() conn.close()