1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| now = datetime.now() time = datetime.strftime(now, '%Y%m%d%H%M%S') 把时间格式化 filename = time + '.xls' # 创建一个sheet对象 wb = xlwt.Workbook(encoding='utf-8') sheet = wb.add_sheet('order-sheet') # 写入文件标题 sheet.write(0, 0, '职工号') sheet.write(0, 1, '姓名') sheet.write(0, 2, '性别') sheet.write(0, 3, '身份证号') sheet.write(0, 4, '手机') sheet.write(0, 5, '办公室') sheet.write(0, 6, '邮箱') sheet.write(0, 7, 'QQ') data_row = 1 for teacher in Teacher.query.all(): sheet.write(data_row, 0, teacher.to_full_dict()['sno']) sheet.write(data_row, 1, teacher.to_full_dict()['name']) sheet.write(data_row, 2, teacher.to_full_dict()['sex']) sheet.write(data_row, 3, teacher.to_full_dict()['idcard_num']) sheet.write(data_row, 4, teacher.to_full_dict()['phone_num']) sheet.write(data_row, 5, teacher.to_full_dict()['office']) sheet.write(data_row, 6, teacher.to_full_dict()['email']) sheet.write(data_row, 7, teacher.to_full_dict()['qq']) data_row = data_row + 1 # 写出到IO output = BytesIO() wb.save(output) output.seek(0) response = make_response(output.getvalue()) response.headers['content_type='] = ' application/vnd.ms-excel' # 创建一个文件对象 response.headers['Content-Disposition'] = 'attachment;filename={}'.format(filename.encode().decode('latin-1')) return response
|