【发布时间】:2021-07-18 16:16:04
【问题描述】:
这里我试图将 python 列表导出到 excel 文件,但它不能正常工作。
列表将是动态的。
response = HttpResponse(content_type='application/ms-excel')
response['Content-Disposition'] = 'attachment; filename="excle_file.xls"'
wb = xlwt.Workbook(encoding='utf-8')
ws = wb.add_sheet('datas', cell_overwrite_ok=True)
row_num = 0
font_style = xlwt.XFStyle()
font_style.font.bold = True
columns = ['col1', 'col2', 'col3', 'col4', 'col5']
for col_num in range(len(columns)):
ws.write(row_num, col_num, columns[col_num], font_style)
font_style = xlwt.XFStyle()
rows = [20, Decimal('50000.00'), Decimal('10.00'), Decimal('40000.00'), Decimal('90000.00'), 21, Decimal('31000.00'), Decimal('2000.00'), Decimal('41000.00'), Decimal('74000.00')]
for i,e in enumerate(rows):
ws.write(i,1, e)
wb.save(response)
return response
我想要这样的回应。
col1 col2 col3 col4 col5
20 50000 10 40000 90000
21 31000 2000 41000 74000
当前响应
col1 col2 col3 col4 col5
20
50000
10
40000
90000
21
31000
....
我还想根据文本增加列的大小?怎么做?
【问题讨论】: