Tornado导出Excel

import io
import asyncio
import xlsxwriter
import tornado.web


class MainHandler(tornado.web.RequestHandler):
    async def get(self):
        self.set_header('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
        self.set_header('content-Disposition', 'attachment; filename=Export.xlsx')
        output = io.BytesIO()
        workbook = xlsxwriter.Workbook(filename=output, options={'in_memory': True})
        worksheet = workbook.add_worksheet(name='test sheet')
        worksheet.write(0, 0, 'a')
        worksheet.write(0, 1, 'b')
        worksheet.write(0, 2, 'c')

        worksheet.write(1, 0, 1024)
        worksheet.write(1, 1, 'z')
        worksheet.write(1, 2, 23)
        workbook.close()
        self.write(output.getvalue())
        await self.flush()


def make_app():
    return tornado.web.Application([
        (r"/", MainHandler),
    ])


async def main():
    app = make_app()
    app.listen(8888)
    await asyncio.Event().wait()


if __name__ == "__main__":
    asyncio.run(main())