我从包含很多搜索元素的搜索中进行搜索,然后在搜索结果中有一个打印按钮,该按钮再次需要提交包含许多其他数据的搜索表单以首先找到记录,然后将其打印为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');
}
对于任何帮助,谢谢。
最佳答案
这可能对您有帮助
$(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);
}