没有合适的资源?快使用搜索试试~ 我知道了~
在django项目中导出数据到excel文件并实现下载的功能
11 下载量 126 浏览量
2020-12-20
11:32:05
上传
评论
收藏 77KB PDF 举报
温馨提示
试读
3页
依赖模块 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币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 基于Matlab人脸肤色定理的教师人数统计+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于Matlab霍夫曼变换的表盘读数识别+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于Matlab火灾烟雾检测源码带GUI界面+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于Matlab的恶劣天气交通标志识别系统+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于MATLAB的霍夫曼变换的表盘示数识别+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于Matlab的车道线识别系统 +源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于MATLAB的教室人数统计系统带Gui界面+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于MATLAB的教室人数统计系统带Gui界面+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于MATLAB 的霍夫曼变换答题卡识别源码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于Matlab+bp神经网络的神经网络汉字识别系统+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功