python导出数据到excel
1,SMTP发送带excel附件的邮件:
def sendMail(filename, addressee):
"""
:param content: 发送内容
:param sendStr: 收件人
:return: sendmail
"""
msg = MIMEMultipart('related')
msg["Subject"] = "表格导出"
# msg["From"] = "***@qq.com"
msg["To"] = addressee
thebody = MIMEText(u'正文', 'plain', 'utf-8')
msg.attach(thebody)
att = MIMEText(open(u'%s.xls' % filename, 'rb').read(), 'base64', 'GB2312')#附件
att['Content-Type'] = 'application/vnd.ms-excel'
att['Content-Disposition'] = 'attachment; filename ="%s.xls"' % filename #xlsx格式
# att = MIMEText(open(u'%s.xlsx' % filename, 'rb').read(), 'base64', 'utf-8')
# att['Content-Type'] = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
# att['Content-Disposition'] = 'attachment; filename ="%s.xlsx"' % filename
msg.attach(att) server = smtplib.SMTP_SSL("smtp.qq.com", 465)
server.login("***@qq.com", "***")#第二个参数为邮件配置密钥
server.sendmail("***@qq.com", addressee, msg.as_string())
server.quit()
2,生成excel发送邮件并下载到本地客户端:
#views.py
def export_to_excel(request):
data = [{'a': '','b':''},{'a':'','b':''}]
filename = 'export'
addressee = '***@***.com'
workbook = xlwt.Workbook()
sheet = workbook.add_sheet('sheet1', cell_overwrite_ok=True)
for i in range(0, len(data[0])):
sheet.write(0, i, data[0].keys()[i])
for row in range(1, len(data) + 1):
for col in range(0, len(data[0].items())):
sheet.write(row, col, u'%s' % data[row - 1].values()[col])
print data[0].keys()
workbook.save(r"%s.xls" % filename)
sendMail(filename, addressee)
sio = StringIO.StringIO() # StringIO用来作为字符串的缓存,有些接口和文件操作一致,代码可以同时当成文件操作或者StringIO操作。
workbook.save(sio)
sio.seek(0)
response = HttpResponse(sio.getvalue(),content_type='application/vnd.ms-excel')
response['Content-Disposition'] = 'attachment; filename=%s.xls' % filename #xlsx文件,出现拓展名的地方都要改为xlsx
#response = HttpResponse(sio.getvalue(), content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
#response['Content-Disposition'] = 'attachment; filename=%s.xlsx' % filename response.write(sio.getvalue())
exist_file = os.path.exists(r"%s.xls" % filename)
if exist_file: #生成的文件会在项目文件里存留,所以在下载或者发送之后要删除
os.remove(r"%s.xls" % filename)
return response
python导出数据到excel的更多相关文章
- python 导出数据到excel 中,一个好用的导出数据到excel模块,XlsxWriter
最近公司有项目需要导出数据到excel,首先想到了,tablib,xlwt,xlrd,xlwings,win32com[还可以操作word],openpyxl,等模块但是 实际操作中tablib 写入 ...
- Python导出数据到Excel表格-NotImplementedError: formatting_info=True not yet implemented
在使用Python写入数据到Excel表格中时出现报错信息记录:“NotImplementedError: formatting_info=True not yet implemented” 报错分析 ...
- Python导出数据生成excel报表
#_*_coding:utf-8_*_ import MySQLdb import xlwt from datetime import datetime def get_data(sql): # 创建 ...
- 使用python脚本从数据库导出数据到excel
python从数据库导出数据到excel 最近需要从数据库里导出一些数据到excel,刚开始我是使用下面的命令 select * from xxx where xxx into outfile 'xx ...
- 1.ASP.NET MVC使用EPPlus,导出数据到Excel中
好久没写博客了,今天特地来更新一下,今天我们要学习的是如何导出数据到Excel文件中,这里我使用的是免费开源的Epplus组件. 源代码下载:https://github.com/caofangshe ...
- 导出数据到Excel --使用ExcelReport有感
先看图,这是几个月前用NPOI写的导出数据到Excel,用了上百行代码,而且难控制,导出来也比较难看 excel打开的效果 下面是我用ExcelReport类库导出到Excel的操作 1.首先引用Ex ...
- 使用Open xml 操作Excel系列之二--从data table导出数据到Excel
由于Excel中提供了透视表PivotTable,许多项目都使用它来作为数据分析报表. 在有些情况下,我们需要在Excel中设计好模板,包括数据源表,透视表等, 当数据导入到数据源表时,自动更新透视表 ...
- Dynamics CRM导出数据到Excel
原创地址:http://www.cnblogs.com/jfzhu/p/4276212.html 转载请注明出处 Pivot Table是微软BI的一个重要工具,所以这里讲一下Dynamics CRM ...
- MVC导出数据到EXCEL新方法:将视图或分部视图转换为HTML后再直接返回FileResult
导出EXCEL方法总结 MVC导出数据到EXCEL的方法有很多种,常见的是: 1.采用EXCEL COM组件来动态生成XLS文件并保存到服务器上,然后转到该文件存放路径即可: 优点:可设置丰富的EXC ...
随机推荐
- BLE资料应用笔记 -- 持续更新(转载)
简而言之,蓝牙无处不在,易于使用,低耗能和低使用成本.’让我们’更深入地探索这些方面吧. 蓝牙无处不在—,您可以在几乎每一台电话.笔记本电脑 .台式电脑和平板电脑中找到蓝牙.因此,您可以便利地连接键盘 ...
- Linux内核分析作业 NO.2
操作系统是如何工作的 于佳心 原创作品转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 ...
- Linux内核分析——第六周学习笔记20135308
第六周 进程的描述和进程的创建 一.进程描述符task_struct数据结构 1.操作系统三大功能 进程管理 内存管理 文件系统 2.进程控制块PCB——task_struct 也叫进程描述符,为了管 ...
- 软工第三次作业 -- 结对之AutoCS1.0
031302331 031302223 一.将初始排课表导入系统数据库 法1:通过jxl解析excel,把数据插入数据库.较简单,预计用时60分钟 我们采取的是 法2(预计用时30分钟):我们使用的是 ...
- 微信小游戏 项目配置文件 project.config.json
一.项目配置文件project.config.json 小程序开发者工具在每个项目的根目录都会生成一个 project.config.json,在工具上做的任何配置都会写入到这个文件,当重新安装工具或 ...
- BeanFactory 简介以及它 和FactoryBean的区别(阿里面试)
BeanFacotry是spring中比较原始的Factory.如XMLBeanFactory就是一种典型的BeanFactory.原始的BeanFactory无法支持spring的许多插件,如AOP ...
- browser-sync & http server
browser-sync & http server browser-sync https://www.browsersync.io/ usage # step 1 $ npm install ...
- images & Skeleton
images & Skeleton image placeholder & loading ... skeleton.css https://github.com/dhg/Skelet ...
- 一本通1625【例 1】反素数 Antiprime
反素数 Antiprime 题目描述 原题来自:POI 2001 如果一个大于等于 1 的正整数 n,满足所有小于 n 且大于等于 1 的所有正整数的约数个数都小于 n 的约数个数,则 n 是一个反素 ...
- Nginx 配置遇到问题
1 connect() to 10.2.20.170:1009 failed (13: Permission denied) while connecting to upstream 现这个错误是因为 ...