0
我希望能夠將某些Django模型轉換爲Excel文件。我的看法如下:使用Django轉換爲Excel
@login_required
def archive_to_excel(request):
calculations_list = Calculations.objects.filter(user=request.user) \
.values_list('make', 'model', 'first_registration', 'body', 'facelift',
'engine', 'transmission', 'purchase_price', 'mileage',
'customer__firstname', 'customer__lastname', 'customer__email') \
.order_by('-id')
columns = [_('Make'), _('Model'), _('First registration'), _('Body'), _('Facelift'), _('Engine'), _('Transmission'),
_('Price'), _('Mileage'), _('Customer first name'), _('Customer last name'), _('Customer email')]
return convert_to_excel("archive", columns, calculations_list)
而且convert_to_excel
功能如下:
def convert_to_excel(file_name, columns, values):
response = HttpResponse(content_type='application/ms-excel')
response['Content-Disposition'] = 'attachment; filename="{}.xls"'.format(file_name)
wb = xlwt.Workbook(encoding='utf-8')
ws = wb.add_sheet(file_name.capitalize())
# Sheet header, first row
row_num = 0
font_style = xlwt.XFStyle()
font_style.font.bold = True
for col_num in range(len(columns)):
ws.write(row_num, col_num, columns[col_num], font_style)
# Sheet body, remaining rows
font_style = xlwt.XFStyle()
for row in values:
row_num += 1
for col_num in range(len(row)):
ws.write(row_num, col_num, row[col_num], font_style)
wb.save(response)
return response
這工作正常,但我有問題是purchase_price
在Calculations
模型存儲不含稅我想在Excel文件INCL VAT中顯示。
如何將purchase_price
與1.21相乘,並將其顯示在excel文件中?
有什麼建議嗎?