P68.Excel数据上传到数据库
def depart_multiple(request):
# 1.获取用户上传的文件对象
file_object = request.FILES.get("user_excel")
# 2.对象传递给openpyxl,读取文件内容
from openpyxl import load_workbook
# wb = load_workbook("文件路径或文件对象")
wb = load_workbook(file_object)
sheet = wb.worksheets[0]
# 3.循环获取每一行数据,添加到数据库
for row in sheet.iter_rows(min_row=2):
text = row[0].value
print(text)
exists = app02_staff_management.models.Department.objects.filter(title=text).exists()
if not exists:
app02_staff_management.models.Department.objects.create(title=text)
return redirect('/depart_list/')
from django import forms
class