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

Wah*_*med 35 python django excel httpresponse xlsxwriter

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

Jeb*_*Jeb 64

关于Python 3的@alecxe响应(io.BytesIO而不是StringIO.StringIO)和Django> = 1.5(content_type而不是mimetype)的一点点更新,完全内存中的文件程序集已经由@jmcnamara实现({ 'in_memory':真的})!
这是完整的例子:

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
Run Code Online (Sandbox Code Playgroud)

  • 上帝保佑你这个答案 (7认同)
  • **抬头**:xlsxwriter使用临时文件系统文件构建文件,因此读者可能会发现使用`in_memory` [实际使用StringIO]会感到困惑(https://github.com/jmcnamara/XlsxWriter/blob/master/ xlsxwriter/packager.py#L159).但是,最终文件输出由`Workbook()`的第一个参数确定.如果它是一个字符串,则创建一个具有该名称的文件(如果使用`in_memory`则为_even).如果它是StringIO,则最终文件将存储在内存中.[本例中的文字帮助](http://xlsxwriter.readthedocs.io/example_http_server.html) (2认同)

ale*_*cxe 53

我想你正在询问如何在内存中创建一个excel文件xlsxwriter并通过它返回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
Run Code Online (Sandbox Code Playgroud)

希望有所帮助.

  • 真好.如果你得到一些时间,你可以把它充实成一个示例程序,我可以添加到XlsxWriter [例子](https://github.com/jmcnamara/XlsxWriter/tree/master/examples). (2认同)
  • 当然可以.顺便说一句,严格来说,xlsxwriter并没有真正将工作簿存储在内存中 - 它在内部使用`tempfile.tempdir` - 这会导致谷歌应用引擎上出现问题:请参阅http://stackoverflow.com/questions/17014055/using -xlsxwriter功能于谷歌应用内发动机换蟒?LQ = 1. (2认同)

nut*_*uts 16

说到Django,你甚至可以没有整个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
Run Code Online (Sandbox Code Playgroud)

附录:您需要指定{'in_memory': True}或者您可能获得HttpResponse has no attribute seek().谢谢@Jeb