title: Write data to xls and download it in python
body: <p>I have successfully created .csv file and now want to create .xls for the same data.
  After which I want to download it as well.
  So after creation of csv file from python I am sending response to the ajax function and downloading it from there using jquery.</p>
  
  <pre><code>def expense_export(request):
  print(request.POST)
  if request.is_ajax():
      ids = request.POST.getlist('ids[]')
      expenses = Expense.objects.filter(id__in=ids)
      data = []
      field = ['SLNO', 'Date of Recording', 'Category', 'Sub Category', 'Invoice Date', 'Invoice No',
               'GST No. Mentioned', 'Vendor Name', 'Details', 'Gross Value', 'SGST', 'CGST', 'IGST',
               'Total Invoice Value', 'TDS(if any)', 'Net Payble']
      field1 = ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']
  
      response = HttpResponse(content_type='text/csv')
      response['Content-Disposition'] = 'attachment; filename="Search Results.csv"'
      sno = 1
      max = 0
      for record in expenses:
          pay_data = []
          if record.record:
              curr = 0
              for pay_record in record.record:
                  pay_row = [pay_record['date'], pay_record['amount'], pay_record['mode'], pay_record['ref'],
                             pay_record['bank']]
                  pay_data = pay_data + pay_row
                  curr = curr + 1
                  if curr &gt; max:
                      max = curr
          gst_exist = 'No'
          if record.vendor:
              if record.vendor.gst_no:
                  gst_exist = 'Yes'
          igst = int(record.gst) / 100 * record.amount
          tds = int(record.tds) / 100 * record.amount
          net_amount = int(record.amount) + int(igst)
          row = [
              sno, record.timestamp.strftime('%d-%m-%Y'), record.expense_name, record.category,
              record.invoice_date, record.invoice_no, gst_exist, record.vendor_name,
              record.remark, record.amount, igst / 2, igst / 2, igst, net_amount, tds, net_amount - tds,
          ]
          row = row + pay_data
          data.append(row)
          sno = sno + 1
      print(max)
      for i in range(0, max):
          extra_field = ['Date of Payment', 'Amount Paid', 'Mode of Payment', 'Ref No.', 'Bank Account No.']
          extra_field1 = ['Payment Details ' + str(i + 1), '', '', '', '']
          field = field + extra_field
          field1 = field1 + extra_field1
  
      writer = csv.writer(response)  # Initialization
      writer.writerow(field1)  # Header 1
      writer.writerow(field)  # Header 2
      writer.writerows(data)  # Rows
  
      return response
      # return workbook
  </code></pre>
  
  <p>And the Jquery Function</p>
  
  <pre><code>    $(document).on('click', '#expense_export', function () {
      // $('#overlay').show();
      ids = [];
      $('#expense_table tbody tr').each(function () {
          ids.push($(this).find('td:first').text());
      });
      $.ajax({
          type: 'post',
          url: '/invoice/expense_export/',
          data: {
              'ids': ids
          },
          enctype: 'multipart/form-data',
          success: function (result) {
              console.log(result);
              // $('#overlay').hide();
              var blob = new Blob([result]);
              var link = document.createElement('a');
              link.href = window.URL.createObjectURL(blob);
              link.download = 'QuaExpenses.csv';
              link.click();
          }
      });
  });
  </code></pre>
  
  <p>Now I know how to create a .xls file in python</p>
  
  <pre><code>        writer = csv.writer(response)  # Initialization
      writer.writerow(field1)  # Header 1
      writer.writerow(field)  # Header 2
      writer.writerows(data)  # Rows
  
      workbook = xlwt.Workbook()
      sheet = workbook.add_sheet("Sheet Name")
      style = xlwt.easyxf('font: bold 1, color red;')
  
      for i in range(len(field1)):
          sheet.write(0, i, field1[i], style)
      for i in range(len(field)):
          sheet.write(1, i, field[i], style)
  
      workbook.save('example.xls')
  </code></pre>
  
  <p>But I am not able to understand how to send this workbook as a response like the way I did with .csv file.</p>
  
  <p>Can someone please help!</p>
  
  <p>Further can I add styling such as background color in cell in csv files?</p>
  
  <p>Thankyou</p>