没有合适的资源?快使用搜索试试~ 我知道了~
首页在django项目中导出数据到excel文件并实现下载的功能
在django项目中导出数据到excel文件并实现下载的功能
11 下载量 161 浏览量
更新于2023-03-16
评论
收藏 77KB PDF 举报
依赖模块 xlwt下载:pip install xlwt 后台模块 view.py # 导出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'] = 'a
资源详情
资源评论
资源推荐
在在django项目中导出数据到项目中导出数据到excel文件并实现下载的功能文件并实现下载的功能
依赖模块依赖模块
xlwt下载:pip install xlwt
后台模块后台模块
view.py
# 导出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
前端模块前端模块
<button id=”export_excel” type=”button” class=”btn btn-primary col-sm-5″ style=”margin-left: 10px” >导出
excel</button>
$("#export_excel").click(function () {
var csrf=$('input[name="csrfmiddlewaretoken"]').val();
const req = new XMLHttpRequest();
req.open('POST', '/export_excel/', true);
req.responseType = 'blob';
req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); //设置请求头
req.send('city='+$('#city').val()+"&&csrfmiddlewaretoken="+csrf); //输入参数
req.onload = function() {
const data = req.response;
const a = document.createElement('a');
const blob = new Blob([data]);
const blobUrl = window.URL.createObjectURL(blob);
download(blobUrl) ;
};
});
function download(blobUrl) {
var city = $("input[name='city']").val();
const a = document.createElement('a');
weixin_38639471
- 粉丝: 8
- 资源: 932
上传资源 快速赚钱
- 我的内容管理 收起
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
会员权益专享
最新资源
- zigbee-cluster-library-specification
- JSBSim Reference Manual
- c++校园超市商品信息管理系统课程设计说明书(含源代码) (2).pdf
- 建筑供配电系统相关课件.pptx
- 企业管理规章制度及管理模式.doc
- vb打开摄像头.doc
- 云计算-可信计算中认证协议改进方案.pdf
- [详细完整版]单片机编程4.ppt
- c语言常用算法.pdf
- c++经典程序代码大全.pdf
- 单片机数字时钟资料.doc
- 11项目管理前沿1.0.pptx
- 基于ssm的“魅力”繁峙宣传网站的设计与实现论文.doc
- 智慧交通综合解决方案.pptx
- 建筑防潮设计-PowerPointPresentati.pptx
- SPC统计过程控制程序.pptx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0