0

I try to put statistics in excel spreadsheets in dynamic way, so when

excel.js

$('.js-excel').on('click', function () {
    $.get(
            '/ajax/stat_excel/',
            {
                'excel': 'loan',
                'date_from': $('#date_from').val(),
                'date_to': $('#date_to').val()
            }
    )
})

then

view.py

output = StringIO.StringIO()
        workbook = xlsxwriter.Workbook(output)
        if request.GET.get('excel') == 'loan':
            workbook = loanChart.excel(workbook)
        if request.GET.get('excel') == 'debet':
            workbook = debetChart.excel(workbook)
        workbook.close()

        xlsx_data = output.getvalue()

        response = HttpResponse(xlsx_data, mimetype='application/vnd.ms-excel')
        response['Content-Type'] = 'application/vnd.ms-excel'
        response['Content-Disposition'] = 'attachment; filename=report.xlsx'

        return response

And I'm not sure what I'm doing wrong, because response be like

PK�������F��AS]$��w������xl/worksheets/sheet1.xml��[oɑ���W|W+#�>�dx(�{}�%j$�$ $������ʞ��8�]C�.��QU���������//�q�����ۛ:�����?|���77��y�n^<=�}�p�������������y����O���,���

  1. Excel file generates excellent. I can see it, if I don't use StringIO
  2. I'm not sure, what I need to use, Mimetype or Content-Type. Can't see any difference. Works exactly the same, no matter which type I write in response.

Where can be my problem?

Alwx
  • 187
  • 3
  • 11

1 Answers1

0

Found answer here https://stackoverflow.com/a/4518775/4498908.

I can't use ajax for file download. But I can:

function download(path,val) {
    window.location.href = path+"download.php?val="+val;
};
Community
  • 1
  • 1
Alwx
  • 187
  • 3
  • 11