python3.X django服务导出execl文件,实现下载
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | # 导出Excel文件 def export_excel(request): city = request.POST.get( 'city' ) print (city) list_obj = place.objects. filter (city = city) # 设置HTTPResponse的类型 response = HttpResponse(content_type = 'application/vnd.ms-excel' ) response[ 'Content-Disposition' ] = 'attachment;filename=' + city + '.xls' """导出excel表""" if list_obj: # 创建工作簿 ws = xlwt.Workbook(encoding = 'utf-8' ) # 添加第一页数据表 w = ws.add_sheet( 'sheet1' ) # 新建sheet(sheet的名称为"sheet1") # 写入表头 w.write( 0 , 0 , u '地名' ) w.write( 0 , 1 , u '次数' ) w.write( 0 , 2 , u '经度' ) w.write( 0 , 3 , u '纬度' ) # 写入数据 excel_row = 1 for obj in list_obj: name = obj.place sum = obj. sum lng = obj.lng lat = obj.lat # 写入每一行对应的数据 w.write(excel_row, 0 , name) w.write(excel_row, 1 , sum ) w.write(excel_row, 2 , lng) w.write(excel_row, 3 , lat) excel_row + = 1 # 写出到IO output = BytesIO() ws.save(output) # 重新定位到开始 output.seek( 0 ) response.write(output.getvalue()) return response |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步