Dajngo查询数据,查询出来之后生成Excel保存本地

class ExportExcel(APIView):
def post(self, request, *args, **kwargs):
export_time = request.data.get('startEndTime')
user_id = request.data.get('user_id')
if len(user_id) == 1 and 0 in user_id:
message_content = StatisticsMessageCount.objects.filter(
creat_time__range=(export_time[0] + " 00:00:00", export_time[1] + " 23:59:59"))
elif len(user_id) > 1 and 0 in user_id:
return JsonResponse({"code": 201, "message": "导出失败,请把全部选项去除!"})
else:
message_content = StatisticsMessageCount.objects.filter(user_id__in=user_id,
creat_time__range=(export_time[0] + " 00:00:00",
export_time[1] + " 23:59:59"))
wb = xlwt.Workbook(encoding='utf8')
sheet = wb.add_sheet('sheet', cell_overwrite_ok=True)
style_heading = xlwt.easyxf("""
font:
height 220;
align:
vert center,
horiz center;
pattern:
pattern solid,
fore-colour 0x16;
borders:
left thin,
right thin,
top thin,
bottom thin
""")
for i in range(0, len(message_content)):
sheet.col(i).width = 256 * 30
sheet.row(i).height = 20 * 80
sheet.write(0, 0, '序号', style_heading)
sheet.write(0, 1, '销售', style_heading)
sheet.write(0, 2, '地区', style_heading)
sheet.write(0, 3, 'Qustions', style_heading)
sheet.write(0, 4, 'Answer', style_heading)
sheet.write(0, 5, '解答人', style_heading)
data_row = 0
file_name = None
for i in message_content:
# 格式化datetime
data_row += 1
if len(user_id) > 1 or len(user_id) == 1 and 0 in user_id:
file_name = "KPI统计"
else:
file_name = i.answerer
sheet.write(data_row, 0, data_row)
sheet.write(data_row, 1, i.sales)
sheet.write(data_row, 2, i.area, )
sheet.write(data_row, 3, i.problem)
sheet.write(data_row, 4, i.answer)
sheet.write(data_row, 5, i.answerer, )
try:
import os
test_url = "http://127.0.0.1:8081"
test_path = str('/medias/weekly/{}.xlsx'.format(file_name))
ret = os.getcwd()
wb.save(os.getcwd() + pord_path)
return JsonResponse({"code": 200,
"fileName": "{}.xlsx".format(file_name),
"filePath": pord_url + pord_path })
except Exception as e:
print("异常: {}".format(e))
return JsonResponse({"code": 201, "message": "导出失败,请关闭当前本地电脑打开的相同Excel重新导出!"})

 

前端 react 

   exportExcel = () => {
const {startEndTime, selectedItems} = this.state
let currentUser = JSON.parse(localStorage.getItem('userInfo'));
const {dispatch} = this.props
if (startEndTime.length === 2) {
dispatch({
type: 'GetStaticsCount/exportExcelData',
payload: {
apiPath: '/wx/kpi_export/',
user_id: currentUser.weights > 0? selectedItems:[currentUser.id],
startEndTime
},
callback: response => { // 这块是关键, 根据后台api返回的文件路径,在本地可以正常a标签下载,在服务器上不可以,直接通过链接打开是文件流形式
axios.post(response.filePath, '', {
headers: {
'Content-Type': 'application/x-www-form-urlencoded', //请求的数据类型为form data格式
},
'responseType': 'blob' //设置响应的数据类型为一个包含二进制数据的 Blob 对象,必须设置!!!
}).then(function (response) {
console.log(`数据流: ${response.data}`)
const blob = new Blob([response.data]);
const fileName = 'KPI统计.xlsx';
const linkNode = document.createElement('a');
linkNode.download = fileName; //a标签的download属性规定下载文件的名称
linkNode.style.display = 'none';
linkNode.href = URL.createObjectURL(blob); //生成一个Blob URL
document.body.appendChild(linkNode);
linkNode.click(); //模拟在按钮上的一次鼠标单击
URL.revokeObjectURL(linkNode.href); // 释放URL 对象
document.body.removeChild(linkNode);
}).catch(function (error) {
console.log(error);
});
}
})
} else {
message.error("请选择导出时间")
}
}

 

django 导出excel react下载 --- 导出并下载的更多相关文章

  1. C# 使用Epplus导出Excel [2]:导出动态列数据

    C# 使用Epplus导出Excel [1]:导出固定列数据 C# 使用Epplus导出Excel [2]:导出动态列数据 C# 使用Epplus导出Excel [3]:合并列连续相同数据 C# 使用 ...

  2. C# 使用Epplus导出Excel [1]:导出固定列数据

    C# 使用Epplus导出Excel [1]:导出固定列数据 C# 使用Epplus导出Excel [2]:导出动态列数据 C# 使用Epplus导出Excel [3]:合并列连续相同数据 C# 使用 ...

  3. 使用NPOI将数据库里信息导出Excel表格并提示用户下载

    使用NPOI进行导出Excel表格大家基本都会,我在网上却很少找到导出Excel表格并提示下载的 简单的代码如下 //mvc项目可以传多个id以逗号相隔的字符串 public ActionResult ...

  4. Java POI导出Excel不弹框选择下载路径(下载文件不选择下载路径,默认) Chrome

    在Chrome浏览器中,Java导出Excel文件时,浏览器弹出提示框,需要选择下载路径 在Chrome中的高级设置中,把“下载前询问每个文件的保存位置”去掉就解决了 DEEPLOVE(LC)

  5. JAVA 导出 Excel, JS 导出 Excel

    本介绍两种Excle导出方法: JAVA 导出 Excle, JS 导出 Excle 1, js 根据 html 页面的 table > tr > td 标签导出 js代码: //导出 v ...

  6. C# NPOI导出Excel和EPPlus导出Excel比较

    系统中经常会使用导出Excel的功能. 之前使用的是NPOI,但是导出数据行数多就报内存溢出. 最近看到EPPlus可以用来导出Excel,就自己测了下两者导出上的差异. NPIO官网地址:http: ...

  7. Powerdesigner 导出Excel格式数据字典 导出Excel格式文件

    版权声明:本文为博主原创文章,转载请注明出处; 网上我也看到了很多的Powerdesigner 导出方法,因为Powerdesigner 提供了部分VBA功能,所以让我用代码导出Excel格式文件得以 ...

  8. POI实现导出Excel和模板导出Excel

    一.导出过程 1.用户请求导出 2.先访问数据库,查询需要导出的结果集 3.创建导出的Excel工作簿 4.遍历结果集,写入工作簿 5.将Excel已文件下载的形式回复给请求客户端 二.具体实现(截取 ...

  9. C# NPOI导出Excel和EPPlus导出Excel

    转自:http://www.cnblogs.com/tanpeng/p/6155749.html 系统中经常会使用导出Excel的功能.之前使用的是NPOI,但是导出数据行数多就报内存溢出. 最近看到 ...

  10. JXLS导出Excel(模板导出)

    1.导包 在pom.xml中加入依赖如下: <dependency> <groupId>org.jxls</groupId> <artifactId>j ...

随机推荐

  1. CSS元素的盒类型

    一.css简介 CSS是Cascading Style Sheet的缩写,中文称层叠样式表.HTML中的元素都有着自己的属性和默认样式,CSS控制HTML内标签显示不同布局样式.控制对应html标签颜 ...

  2. 【MySQL】MySQL-front等客户端连接MySQL_8.0等失败的解决办法

    ALTER USER 'root'@'localhost' IDENTIFIED BY '新的密码' PASSWORD EXPIRE NEVER; ALTER USER 'root'@'localho ...

  3. spec cpu2006 官网

    https://www.spec.org/cpu2006/Docs/install-guide-unix.html

  4. Linux 如何查看系统负载

    Linux 如何查看系统负载 310 博客 /  Linux/ 4个月前/  534 /  0   操作系统的负载状态,反映了应用程序的资源使用情况,从中能找出应用程序优化的瓶颈所在. 系统平均负载, ...

  5. Kubernetes 部署微服务电商平台(16)

    一.概念 微服务就是很小的服务,小到一个服务只对应一个单一的功能,只做一件事.这个服务可以单独部署运行,服务之间可以通过RPC来相互交互,每个微服务都是由独立的小团队开发,测试,部署,上线,负责它的整 ...

  6. Yarn 集群环境 HA 搭建

    环境准备 确保主机搭建 HDFS HA 运行环境 步骤一:修改 mapred-site.xml 配置文件 [root@node-01 ~]# cd /root/apps/hadoop-3.2.1/et ...

  7. 使用python实现钉钉告警通知功能

    前言:日常工作中告警通知是必不可少的,一般会使用邮件.钉钉.企业微信等,今天分享一下使用python实现钉钉告警 一. 钉钉机器人创建 登录钉钉客户端,创建一个群,把需要收到报警信息的人员都拉到这个群 ...

  8. 利用TortoiseGit向Github上传文件

    利用TortoiseGit向Github上传文件 第一步:建一个新文件夹,作为本地仓库 第二步:右键选择设置为版本库 若弹出,确认即可 重新打开改文件,会发现多了一个绿色的小勾 在文件夹中会自动生成一 ...

  9. [论文阅读笔记] Unsupervised Attributed Network Embedding via Cross Fusion

    [论文阅读笔记] Unsupervised Attributed Network Embedding via Cross Fusion 本文结构 解决问题 主要贡献 算法原理 实验结果 参考文献 (1 ...

  10. MLIR中间表示和编译器框架

    MLIR中间表示和编译器框架 TensorFlow生态系统包含许多在软件和硬件堆栈的多个级别上运行的编译器和优化器.作为TensorFlow的日常用户,使用不同类型的硬件(GPU,TPU,移动设备)时 ...