前端
<div class="panel-body">
<span class="glyphicon glyphicon-th-list" aria-hidden="true"></span>
批量上传
</div>
后端
def depart_multi(request):
'''导入Excel文件'''
from openpyxl import load_workbook
# 1.获取用户上传的文件对象
file_object = request.FILES.get('exc')
# 2.对象传递给openpyxl,由openpyxl读取文件的内容
wb = load_workbook(file_object)
sheet = wb.worksheets[0]
# 3.循环获取每一行数据
for row in sheet.iter_rows(min_row=2):
text = row[0].value
# 4.添加到数据库,先判断要添加的数据是否存在
exists = models.Department.objects.filter(title=text).exists()
if not exists:
models.Department.objects.create(title=text)
return redirect('/xxx/list/')
标签:文件,openpyxl,exists,text,Excel,导入,row
From: https://www.cnblogs.com/jyfyb/p/17834597.html