import io
import time
import xlwt
def set_worksheet():
workbook = xlwt.Workbook(encoding="utf-8")
worksheet = workbook.add_sheet("订单记录")
worksheet.write(0, 0, label="订单号")
worksheet.write(0, 1, label="商品类型")
worksheet.write(0, 2, label="金额")
worksheet.write(0, 3, label="创建时间")
worksheet.col(0).width = 5000
worksheet.col(1).width = 4000
worksheet.col(2).width = 5000
worksheet.col(3).width = 4000
return workbook, worksheet
class ExportOrderHandler(BaseHandler):
async def get(self):
workbook, worksheet = set_worksheet()
order_list = [] # 订单记录
key_map = {
0: "order_no",
1: "order_type",
2: "money",
3: "create_at",
}
for index, item in enumerate(order_list):
for col in range(len(key_map.keys())):
worksheet.write(index + 1, col, label=item.get(key_map.get(col)))
sio = io.BytesIO()
workbook.save(sio)
# 界面用 window.url() 直接打开该请求url,即会下载
self.set_header('Content-Type', 'application/x-xls')
self.set_header('Content-Disposition',
'attachment; filename={}.xls'.format(time.strftime('%Y%m%d%H%M%S')))
self.write(sio.getvalue())