在带有 ajax 代码的页面上,有一个我过滤的表单。
我正在使用 ajax 将过滤后的数据发送到 download_excel 方法,但下载不成功。
是什么原因?
View .py
def download_excel(request):
jsdata = request.GET.get('listed_data')
objdata = eval(jsdata)
#objdata = [{"id":"123123123","location":"test_location_1","device":"test_device_1","datetime":"12/12/2020","name":"asdas asdas","age":"21","gender":"male","temp":"37.6","mask":"0",risk":"1"},{"id":"123123123","location":"test_location_1","device":"test_device_1","datetime":"12/12/2020","name":"asdas asdas","age":"21","gender":"male","temp":"37.6","mask":"0",risk":"1"}...]
response = HttpResponse(content_type='application/ms-excel')
response['Content-Disposition'] = "attachment; filename='Report.xls'"
wb = xlwt.Workbook(encoding="utf-8")
ws = wb.add_sheet("Report")
row_num = 0
font_style = xlwt.XFStyle()
font_style.font.bold = True
columns = ["ID","Location","Device","Datetime","Full Name","Age Range","Gender","Body Temp","Mask","Risk"]
for col_num in range(len(columns)):
ws.write(row_num,col_num, columns[col_num], font_style)
font_style = xlwt.XFStyle()
for row in objdata:
row_num += 1
ws.write(row_num, 0, str(row["id"]), font_style)
ws.write(row_num, 1, row["location"], font_style)
ws.write(row_num, 2, row["device"], font_style)
ws.write(row_num, 3, row["datetime"], font_style)
ws.write(row_num, 4, row["name"], font_style)
ws.write(row_num, 5, row["age"], font_style)
ws.write(row_num, 6, row["gender"], font_style)
ws.write(row_num, 7, row["temp"], font_style)
ws.write(row_num, 8, row["mask"], font_style)
ws.write(row_num, 9, row["risk"], font_style)
wb.save(response)
return response
网址.py
path('ajax/download_excel/', views.download_excel, name="download_excel")
模板 html
function download_excel(){
$.ajax({
url: "/events/ajax/download_excel/",
data:{
'listed_data': "{{data|safe}}"
}
})
}
最佳答案
这对我有用,例如
path('x1/', returnexcel, name='mreturnexcel'),
import xlsxwriter
def returnexcel(request):
response = HttpResponse(content_type='application/vnd.ms-excel')
response['Content-Disposition'] = 'attachment; filename="mymodel.xlsx"'
workbook = xlsxwriter.Workbook(response, {'in_memory': True})
# add a worksheet
worksheet = workbook.add_worksheet()
#########sql
checkuserflagd2 = 0
pky_idd2 = request.session['pk_id']
print("download excel pky_id")
cursor3xd2 = connection.cursor()
sql3xd2 = "select id, userna, usercm, userpf, usermo, userd1 FROM myapp_document" # where id = " + str(pky_idd2)
cursor3xd2.execute(sql3xd2 )#, docfile , mailfield, usercm, usermo, userna , userpf, userpw FROM myapp_document")
transactions3xd2 = dictfetchall(cursor3xd2)
##############
row_num = 0
#font_style = xlwt.XFStyle()
#font_style.font.bold = True
columns = ["ID","userna","usercm","userpf","usermo","userd1"]
for col_num in range(len(columns)):
worksheet.write(row_num,col_num, columns[col_num])
for row in transactions3xd2:
row_num += 1
worksheet.write(row_num, 0, str(row["id"]))
worksheet.write(row_num, 1, str(row["userna"]))
worksheet.write(row_num, 2, str(row["usercm"]))
worksheet.write(row_num, 3, str(row["userpf"]))
worksheet.write(row_num, 4, str(row["usermo"]))
worksheet.write(row_num, 5, str(row["userd1"]))
#worksheet.write_...
worksheet.write('A1', 'Hello')
# use xlsxwriter routines to create the worksheet
# now write it out
workbook.close()
return response
////////////////
我希望能有所帮助
关于jquery - 使用ajax在Django中将数据下载为Excel文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62140162/