{% if session['role'] == 2 %}
赞
踩
flask 从前端获取excel ,将数据存入数据库
前端页面代码展示
<form action="/toexcel" method="post" enctype="multipart/form-data">
{% if session['role'] == 2 %}
<li><input type="file" name="file" value="请上传Excel文件"/></li>
<li><input type="submit" value="导入" class="btn_search"></li>
{% endif %}
后台代码展示
@app.route('/toexcel',methods = ['GET','POST']) def toExcel(): if request.method == 'POST': file = request.files.get('file') f = file.read() clinic_file = xlrd.open_workbook(file_contents=f) # sheet1 table = clinic_file.sheet_by_index(0) nrows = table.nrows for i in range(1, nrows): row_date = table.row_values(i) print(row_date) new = News(row_date[1],row_date[2],row_date[3],row_date[4],row_date[5]) db.session.add(new) db.session.commit() db.session.close() return redirect('/all')
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。