1. 程式人生 > >Django下載Excel檔案

Django下載Excel檔案

下面提供一個在Django中下載Excel檔案的方法,適用於小型簡單檔案:

html

<button onclick="window.location.href='/down_file'">下載檔案</button>

urls.py

url(r'^down_file', views.down_file),

views.py

def down_file(request):
    with open('file.xlsx', 'rb') as model_excel:
        result = model_excel.read()
    response = HttpResponse(result)
    response['Content-Disposition'] = 'attachment; filename=test.xlsx'
    return response