python django excel xlwt

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/883313/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 21:03:18  来源:igfitidea点击:

django excel xlwt

pythondjangoexcelxlwt

提问by Adrian Mester

On a django site, I want to generate an excel file based on some data in the database.

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

I'm thinking of using xlwt, but it only has a method to save the data to a file. How can get the file to the HttpResponse object? Or maybe do you know a better library?

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

I've also found this snippetbut it doesn't do what I need. All I want is a way to get the stream from the xlwt object to the response object (without writing to a temporary file)

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

回答by Javier

neat package! i didn't know about this

整洁的包!我不知道这个

According to the doc, the save(filename_or_stream)method takes either a filename to save on, or a file-like stream to write on.

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

And a Django response object happens to be a file-like stream! so just do xls.save(response). Look the Django docs about generating PDFswith ReportLab to see a similar situation.

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

edit:(adapted from ShawnMilo's comment):

编辑:(改编自 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

then, from your view function, just create the xlsobject and finish with

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

return xls_to_response(xls,'foo.xls')

回答by brianray

***UPDATE: django-excel-templates no longer being maintained, instead try Marmir http://brianray.github.com/mm/

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

Still in development as I type this but http://code.google.com/p/django-excel-templates/Django excel templates project aims to do what your asking.

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

Specifically look at the tests. Here is a simple case:

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

#
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

回答by S.Lott

You can save your XLS file to a StringIOobject, which is file-like.

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

You can return the StringIO object's getvalue()in the response. Be sure to add headers to mark it as a downloadable spreadsheet.

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

回答by max

You might want to check huDjangowhich comes fith a function called serializers.queryset_to_xls()do convert a queryset into an downloadable Excel Sheet.

您可能想要检查huDjango,它带有一个名为serializers.queryset_to_xls()do convert a queryset into an downloadable Excel Sheet的函数。

回答by Javier

If your data result doesn't need formulas or exact presentation styles, you can always use CSV. any spreadsheet program would directly read it. I've even seen some webapps that generate CSV but name it as .XSL just to be sure that Excel opens it

如果您的数据结果不需要公式或确切的表示样式,您始终可以使用 CSV。任何电子表格程序都会直接读取它。我什至看到一些生成 CSV 的 web 应用程序,但将其命名为 .XSL 只是为了确保 Excel 打开它