前端
后端
import xlrd
import pymysql
# 建立数据库连接
def sql_func(sql_command):
conn = pymysql.connect(
host = "39.107.24.78",
port = 3306,
user = "root",
password = "123456",
charset = "utf8",
database = "flask_test"
)
cursor = conn.cursor(pymysql.cursors.DictCursor) # pymysql.cursors.DictCursor 以列表套字典形式返回,默认以元组套元组返回
sql = sql_command
cursor.execute(sql)
conn.commit()
conn.close()
def execl_input(info):
# 找到文件
# xls = xlrd.open_workbook("/Users/liuguixiang/Documents/excel_test.xlsx") # 指定文件路径
xls = xlrd.open_workbook(file_contents=info) # 指定文件内容
# 确定工作表
sheet = xls.sheet_by_name("sheet1")
# 遍历文件
for i in range(2,sheet.nrows): # 注意数据是从第几行开始的
title = sheet.cell(i,0).value # 取第i行,第0列,以此类推
price = sheet.cell(i,1).value
auther = sheet.cell(i,2).value
sql_command = "insert into book (title,price,auther) values ('%s',%s,'%s')" %(title, price, auther)
sql_func(sql_command)