XlsxWriter对象作为http响应,在Django中创建下载。

时间:2021-07-21 23:00:01

XlsxWriter object save as http response to create download in Django?

XlsxWriter对象保存为http响应以在Django中创建下载?

3 个解决方案

#1


44  

I think you're asking about how to create an excel file in memory using xlsxwriter and return it via HttpResponse. Here's an example:

我认为您正在询问如何使用xlsxwriter在内存中创建一个excel文件并通过HttpResponse返回它。这里有一个例子:

try:
    import cStringIO as StringIO
except ImportError:
    import StringIO

from django.http import HttpResponse

from xlsxwriter.workbook import Workbook


def your_view(request):
    # your view logic here

    # create a workbook in memory
    output = StringIO.StringIO()

    book = Workbook(output)
    sheet = book.add_worksheet('test')       
    sheet.write(0, 0, 'Hello, world!')
    book.close()

    # construct response
    output.seek(0)
    response = HttpResponse(output.read(), mimetype="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
    response['Content-Disposition'] = "attachment; filename=test.xlsx"

    return response

Hope that helps.

希望有帮助。

#2


43  

A little update on @alecxe response for Python 3 (io.BytesIO instead of StringIO.StringIO) and Django >= 1.5 (content_type instead of mimetype), with the fully in-memory file assembly that has since been implemented by @jmcnamara ({'in_memory': True}) !
Here is the full example :

关于Python 3的@alecxe响应的一个小更新(io)。BytesIO而不是string . stringio)和Django >= 1.5 (content_type而不是mimetype),具有完整的内存文件程序集,该程序集已经被@jmcnamara实现({'in_memory': True}) !这里有一个完整的例子:

import io

from django.http.response import HttpResponse

from xlsxwriter.workbook import Workbook


def your_view(request):

    output = io.BytesIO()

    workbook = Workbook(output, {'in_memory': True})
    worksheet = workbook.add_worksheet()
    worksheet.write(0, 0, 'Hello, world!')
    workbook.close()

    output.seek(0)

    response = HttpResponse(output.read(), content_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
    response['Content-Disposition'] = "attachment; filename=test.xlsx"

    output.close()

    return response

#3


13  

When it comes to Django, you can even do without the whole StringIO shenanigans. HttpResponse behaves just like a StringIO in that respect:

说到《被解救的姜戈》,你甚至可以不使用所有的StringIO诡计。HttpResponse在这方面就像一个StringIO:

from django.http import HttpResponse
from xlsxwriter.workbook import Workbook

def your_view(request):
    # your view logic here

    # create the HttpResponse object ...
    response = HttpResponse(content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
    response['Content-Disposition'] = "attachment; filename=test.xlsx"

    # .. and pass it into the XLSXWriter
    book = Workbook(response, {'in_memory': True})
    sheet = book.add_worksheet('test')       
    sheet.write(0, 0, 'Hello, world!')
    book.close()

    return response

Addendum: You need to specify {'in_memory': True} or you might get HttpResponse has no attribute seek(). Thanks @Jeb

附录:您需要指定{'in_memory': True},否则您可能会得到HttpResponse没有属性seek()。由于@Jeb

#1


44  

I think you're asking about how to create an excel file in memory using xlsxwriter and return it via HttpResponse. Here's an example:

我认为您正在询问如何使用xlsxwriter在内存中创建一个excel文件并通过HttpResponse返回它。这里有一个例子:

try:
    import cStringIO as StringIO
except ImportError:
    import StringIO

from django.http import HttpResponse

from xlsxwriter.workbook import Workbook


def your_view(request):
    # your view logic here

    # create a workbook in memory
    output = StringIO.StringIO()

    book = Workbook(output)
    sheet = book.add_worksheet('test')       
    sheet.write(0, 0, 'Hello, world!')
    book.close()

    # construct response
    output.seek(0)
    response = HttpResponse(output.read(), mimetype="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
    response['Content-Disposition'] = "attachment; filename=test.xlsx"

    return response

Hope that helps.

希望有帮助。

#2


43  

A little update on @alecxe response for Python 3 (io.BytesIO instead of StringIO.StringIO) and Django >= 1.5 (content_type instead of mimetype), with the fully in-memory file assembly that has since been implemented by @jmcnamara ({'in_memory': True}) !
Here is the full example :

关于Python 3的@alecxe响应的一个小更新(io)。BytesIO而不是string . stringio)和Django >= 1.5 (content_type而不是mimetype),具有完整的内存文件程序集,该程序集已经被@jmcnamara实现({'in_memory': True}) !这里有一个完整的例子:

import io

from django.http.response import HttpResponse

from xlsxwriter.workbook import Workbook


def your_view(request):

    output = io.BytesIO()

    workbook = Workbook(output, {'in_memory': True})
    worksheet = workbook.add_worksheet()
    worksheet.write(0, 0, 'Hello, world!')
    workbook.close()

    output.seek(0)

    response = HttpResponse(output.read(), content_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
    response['Content-Disposition'] = "attachment; filename=test.xlsx"

    output.close()

    return response

#3


13  

When it comes to Django, you can even do without the whole StringIO shenanigans. HttpResponse behaves just like a StringIO in that respect:

说到《被解救的姜戈》,你甚至可以不使用所有的StringIO诡计。HttpResponse在这方面就像一个StringIO:

from django.http import HttpResponse
from xlsxwriter.workbook import Workbook

def your_view(request):
    # your view logic here

    # create the HttpResponse object ...
    response = HttpResponse(content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
    response['Content-Disposition'] = "attachment; filename=test.xlsx"

    # .. and pass it into the XLSXWriter
    book = Workbook(response, {'in_memory': True})
    sheet = book.add_worksheet('test')       
    sheet.write(0, 0, 'Hello, world!')
    book.close()

    return response

Addendum: You need to specify {'in_memory': True} or you might get HttpResponse has no attribute seek(). Thanks @Jeb

附录:您需要指定{'in_memory': True},否则您可能会得到HttpResponse没有属性seek()。由于@Jeb