1

我从中进行了搜索,其中包含很多搜索元素,然后在搜索结果中我有一个打印按钮,再次需要提交带有许多其他数据的搜索表单才能首先找到记录,然后将其打印到 Excel。为此,我使用 Ajax 提交表单并使用 laravel excel 编写,但现在它不适xlsx用于xls. 当.xls文件被下载时它的内容是这样的��������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������。贝娄是我的代码:

<script>
function generateReport(printType,printRange) {
        $.ajaxSetup({
            header:$('meta[name="_token"]').attr('content')
        })

        var url = '/auditLog/search/printReport';
        var params = $('#auditSearchForm').serialize()+'&printType=' + printType + '&printRange=' + printRange;

        $.ajax({
            type: "POST",
            url: url,
            data: params,
            success: function (response, status, xhr) {
                // check for a filename
                var filename = "";
                var disposition = xhr.getResponseHeader('Content-Disposition');
                if (disposition && disposition.indexOf('attachment') !== -1) {
                    var filenameRegex = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/;
                    var matches = filenameRegex.exec(disposition);
                    if (matches != null && matches[1]) filename = matches[1].replace(/['"]/g, '');
                }

                var type = xhr.getResponseHeader('Content-Type');
                var blob = new Blob([response], { type: type });

                if (typeof window.navigator.msSaveBlob !== 'undefined') {
                    // IE workaround for "HTML7007: One or more blob URLs were revoked by closing the blob for which they were created. These URLs will no longer resolve as the data backing the URL has been freed."
                    window.navigator.msSaveBlob(blob, filename);
                } else {
                    var URL = window.URL || window.webkitURL;
                    var downloadUrl = URL.createObjectURL(blob);

                    if (filename) {
                        // use HTML5 a[download] attribute to specify filename
                        var a = document.createElement("a");
                        // safari doesn't support this yet
                        if (typeof a.download === 'undefined') {
                            window.location = downloadUrl;
                        } else {
                            a.href = downloadUrl;
                            a.download = filename;
                            document.body.appendChild(a);
                            a.click();
                        }
                    } else {
                        window.location = downloadUrl;
                    }

                    setTimeout(function () { URL.revokeObjectURL(downloadUrl); }, 100); // cleanup
                }

            }
        });
    }

</script>

还有我的控制器

public function generateReport()
{
    $result = $this->searchRequest->getDetails();
    //dd($result);
    //$user = Auth::user();
    Excel::create('foo', function($file) {
        $file->sheet('bar', function($sheet) {
            $sheet->setTitle('Hi');
        });
    })->download('xls');
}

任何帮助谢谢。

4

1 回答 1

2

这可能会帮助你

 $(function() {
        $('#your_div').on('click', '#print', function (e) {
            var reportRange = $('#report_range').val();
            downloadReport($(this).attr('printType'),reportRange);
            e.preventDefault();
        });
    });


    function downloadReport(printType,printRange){
        var http = new XMLHttpRequest();
        http.responseType = 'blob';

        var header;
        var blob;
        var url = '/auditLog/search/printReport';
        var params = $('#auditSearchForm').serialize()+'&printType=' + printType + '&printRange=' + printRange;
        http.open("POST", url, true);

        http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

        http.onreadystatechange = function() {//Call a function when the state changes.
            if(http.readyState == 4 && http.status == 200) {
                var filename = "";
                var disposition = http.getResponseHeader('Content-Disposition');
                if (disposition && disposition.indexOf('attachment') !== -1) {
                    var filenameRegex = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/;
                    var matches = filenameRegex.exec(disposition);
                    if (matches != null && matches[1]) filename = matches[1].replace(/['"]/g, '');
                }
                var type = http.getResponseHeader('Content-Type');
                blob = new Blob([http.response], { type: type ,endings:'native'});
                var URL = window.URL || window.webkitURL;
                var downloadUrl = URL.createObjectURL(blob);
                var a = document.createElement("a");
                a.href = downloadUrl;
                a.download = filename;
                document.body.appendChild(a);
                a.click();
            }
        }
        http.send(params);
    }
于 2016-02-16T04:42:35.940 回答