def _excel_of_histories(self, histories, filename):
headers = ('??', '???', '????', '??', '????', '????', '????', '????', '????', '????', '????',)
columns = (
lambda x: x.timeslot and x.timeslot.order.order_id or (x.comment or '?????'),
'timeslot.order.grade',
'timeslot.order.subject',
'timeslot.order.level',
lambda x: x.timeslot and ('%s-%s' % (x.timeslot.start.strftime('%H:%M'), x.timeslot.end.strftime('%H:%M'),)) or '',
lambda x: x.timeslot and (x.timeslot.order.price / 100) or '',
lambda x: x.timeslot and x.timeslot.duration_hours() or '',
lambda x: x.timeslot and ('%s%%' % (x.timeslot.order.commission_percentage),) or '',
lambda x: x.amount and (x.amount / 100) or '',
)
workbook = xlwt.Workbook()
sheet_name = 'Export {0}'.format(datetime.date.today().strftime('%Y-%m-%d'))
sheet = workbook.add_sheet(sheet_name)
for y, th in enumerate(headers):
sheet.write(0, y, th, excel.HEADER_STYLE)
x = 1
for history in histories:
y = 0
day_val = history['day'].date()
sheet.write_merge(x, x + history['count'] - 1, y, y, day_val, excel.get_style_by_value(day_val))
x_sub = x
records = history['records']
for record in records:
y_sub = y+1
for column in columns:
value = callable(column) and column(record) or excel.get_column_cell(record, column)
sheet.write(x_sub, y_sub, value, excel.get_style_by_value(value))
y_sub += 1
x_sub += 1
income = history['income'] / 100
y += len(columns) + 1
sheet.write_merge(x, x + history['count'] - 1, y, y, income, excel.get_style_by_value(income))
x += len(records)
return excel.wb_excel_response(workbook, filename)
评论列表
文章目录