I am building a web app that allows users to download data as XLSX file. How to keep a spreadsheet in memory and send it as bytes.
I was trying to use openpyxl but I cannot cast bytes or str.
It's not that easy as CSV.
@get(
"/download",
response_headers={
"content-disposition": 'inline; filename="file.csv"',
},
media_type="text/csv",
)
async def download_csv() -> bytes:
data = [
["Name", "Age", "City"],
["Alice", 30, "New York"],
["Bob", 25, "London"],
["Charlie", 35, "Paris"]
]
output = io.StringIO()
writer = csv.writer(output)
writer.writerows(data)
output.seek(0)
return output