view
def depart_multi(request):
from openpyxl import load_workbook
# 1.获取文件对象
file_object = request.FILES.get("exc")
print(file_object)
print(type(file_object))
# 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
print(text)
# 4. 写入数据库 (更多的验证规则, 此处省略)
exists = models.Department.objects.filter(title=text).exists()
if not exists:
models.Department.objects.create(title=text)
return redirect("/depart/list")
template
<div class="panel-body">
<form method="post" enctype="multipart/form-data" action="/depart/multi/">
{% csrf_token %}
<div class="form-group">
<input type="file" name="exc">
</div>
<input type="submit" value="上传" class="btn btn-info btn-sm">
</form>
</div>
本站文章除单独注明外均为原创,本文链接https://bowmanjin.com/316,未经允许请勿转载。
请先
!