如何以excel格式导出python中的数据? [英] How to export data in python with excel format?

查看:647
本文介绍了如何以excel格式导出python中的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

def export_to_excel(request):

    lists = MyModel.objects.all()

    # your excel html format
    template_name = "sample_excel_format.html"

    response = render_to_response(template_name, {'lists': lists})

    # this is the output file
    filename = "model.csv"

    response['Content-Disposition'] = 'attachment; filename='+filename
    response['Content-Type'] = 'application/vnd.ms-excel; charset=utf-16'
    return response



urls.py



urls.py

from django.conf.urls.defaults import *

urlpatterns = patterns('app_name.views',
   url(r'^export/$', 'export_to_excel', name='export_to_excel'),  
)




  1. 最后,在您的页面中创建一个将导出的按钮或链接。



page.html



page.html

<a href="{% url app_name:export_to_excel %}">Export</a>

没有获取文件选项下载,没有发生任何错误,但我可以看到所有结果在日志中它的工作

Nothing getting file option for download and not giving any error but i can see all result in log its working fine.

推荐答案

除了其他答案中显示的选项,您还可以使用 XlsxWriter 来创建Excel文件。

In addition to the options shown in the other answers you can also use XlsxWriter to create Excel files.

请参阅此示例

这篇关于如何以excel格式导出python中的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆