【问题标题】:django excel xlwtdjango excel xlwt
【发布时间】:2010-10-27 08:48:24
【问题描述】:

在一个django网站上,我想根据数据库中的一些数据生成一个excel文件。

我正在考虑使用xlwt,但它只有一种将数据保存到文件的方法。如何才能将文件获取到 HttpResponse 对象?或者你知道更好的图书馆吗?

我也找到了这个snippet,但它不能满足我的需要。我想要的只是一种将流从 xlwt 对象获取到响应对象的方法(无需写入临时文件)

【问题讨论】:

    标签: python django excel xlwt


    【解决方案1】:

    ***更新:django-excel-templates 不再维护,改为尝试 Marmir http://brianray.github.com/mm/

    在我输入此内容时仍在开发中,但 http://code.google.com/p/django-excel-templates/ Django excel 模板项目旨在满足您的要求。

    具体看测试。这是一个简单的案例:

    #
    from django_excel_templates import *
    from django_excel_templates.color_converter import *
    from models import *
    from django.http import HttpResponse
    
    def xls_simple(request):
    
        ## Simple ##
        testobj = Book.objects.all()
    
        formatter = ExcelFormatter()
        simpleStyle = ExcelStyle(vert=2,wrap=1)
        formatter.addBodyStyle(simpleStyle)
        formatter.setWidth('name,category,publish_date,bought_on',3000)
        formatter.setWidth('price',600)
        formatter.setWidth('ebook',1200)
        formatter.setWidth('about',20000)
    
        simple_report = ExcelReport()
        simple_report.addSheet("TestSimple")
        filter = ExcelFilter(order='name,category,publish_date,about,bought_on,price,ebook')
        simple_report.addQuerySet(testobj,REPORT_HORZ,formatter, filter)
    
        response = HttpResponse(simple_report.writeReport(),mimetype='application/ms-excel')
        response['Content-Disposition'] = 'attachment; filename=simple_test.xls'
        return response
    

    【讨论】:

      【解决方案2】:
      【解决方案3】:

      整洁的包装!我不知道这个

      根据文档,save(filename_or_stream) 方法需要一个文件名来保存,或者一个类似文件的流来写入。

      而 Django 响应对象恰好是一个类似文件的流!所以就做xls.save(response)。使用 ReportLab 查看有关 generating PDFs 的 Django 文档以了解类似情况。

      编辑:(改编自 ShawnMilo 的评论):

      def xls_to_response(xls, fname):
          response = HttpResponse(mimetype="application/ms-excel")
          response['Content-Disposition'] = 'attachment; filename=%s' % fname
          xls.save(response)
          return response
      

      然后,从您的视图函数中,只需创建 xls 对象并以

      结束
      return xls_to_response(xls,'foo.xls')
      

      【讨论】:

      • 谢谢,我以为应该是这样的,只是不知道去哪里找。我试试看
      • 这很好用,但除非你弄清楚语法。诀窍是创建一个 HttpResponse 对象并将 xlwt 工作簿保存到其中。示例:xls_response = HttpResponse(mimetype="application/ms-excel") xls_response['Content-Disposition'] = 'attachment;文件名=foo.xls' xls.save(xls_response) return xls_response
      • 谢谢你,你真的解决了我的问题。非常感谢您的帮助!
      • 很好的解决方案。非常感谢您的分享!
      • 您的回答看起来非常棒!但我只是很困惑他的 xls 对象是什么?由于它有一个 save() 方法,我认为它是“xls = xlwt.Workbook()”,但这让我退回了 500,请帮忙? @哈维尔
      【解决方案4】:

      您可能想检查huDjango,它带有一个名为serializers.queryset_to_xls() 的函数,将查询集转换为可下载的Excel 工作表。

      【讨论】:

        【解决方案5】:

        您可以将 XLS 文件保存到类似于文件的 StringIO 对象。

        您可以在响应中返回 StringIO 对象的getvalue()。请务必添加标题以将其标记为可下载的电子表格。

        【讨论】:

          【解决方案6】:

          如果您的数据结果不需要公式或精确的呈现样式,您可以随时使用 CSV。任何电子表格程序都会直接读取它。我什至见过一些生成 CSV 但将其命名为 .XSL 的 Web 应用程序,以确保 Excel 可以打开它

          【讨论】:

          • 我也读过某处可以返回excel可以读取的html文件,但我想使用公式和样式,所以不好。
          猜你喜欢
          • 2021-11-14
          • 1970-01-01
          • 2018-10-30
          • 2014-03-05
          • 1970-01-01
          • 2013-06-08
          • 2013-02-23
          • 2016-10-21
          相关资源
          最近更新 更多