首页 > 解决方案 > 使用 django 和纯 javascript 提供现有的 excel 文件

问题描述

我正在尝试使用 django 提供现有的 excel 文件,使用 javascript 下载它。

我在 django 视图中尝试了以下代码:

Generate_Report is the io.Bytes() read from xlsxwriter.
response=HttpResponse(Generate_Report.output,content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
                        response['Content-Disposition']='attachment;filename=Report.xlsx'
                        return response

在html中我写了这个

xhttp.onreadystatechange = function() 
        {
            if (this.readyState == 4 && this.status == 200) {
                var downloadLink = window.document.createElement('a');
                var contentTypeHeader = xhttp.getResponseHeader("Content-Type");
                downloadLink.href = window.URL.createObjectURL(new Blob([xhttp.response], {type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'}));

                downloadLink.download = "Report.xlsx";
                document.body.appendChild(downloadLink);
                downloadLink.click();
                document.body.removeChild(downloadLink);
            }

        };

我想以下载格式提供现有的 excel 文件。

标签: javascriptdjangoexcelxlsxwriter

解决方案


推荐阅读