Saving openpyxl file via text and filestream

前端 未结 5 1126
清酒与你
清酒与你 2021-02-01 16:49

I\'m building OpenPyXL into an application that expects a string containing the content of the excel file, for it to write via file stream.

From my investigation into th

5条回答
  •  失恋的感觉
    2021-02-01 17:05

    from openpyxl import Workbook
    from io import BytesIO
    
    rows = [[1,2], [3,4]]
    
    book = Workbook()
    sheet = book.active
    
    for row in rows:
        sheet.append(row)
    
    io = BytesIO
    book.save(io)
    
    content = io.getValue()
    
    return Response(
        content,
        mimetype=magic.from_buffer(content, mime=True),
        headers={
        'Content-Disposition': 'attachment;filename=' + 'test.xlsx'}
    )
    

提交回复
热议问题