[jQuery] Excel download
Download Excel file using jquery ajax
https://stackoverflow.com/questions/39094681/download-excel-file-using-jquery-ajax
$.ajax({
type : 'GET',
url : 'downloadExcel',
beforeSend : function() {
startPreloader();
},
complete: function(){
stopPreloader();
},
success : function(response){
console.log(response);
var blob = new Blob([response], { type: 'application/vnd.ms-excel' });
var downloadUrl = URL.createObjectURL(blob);
var a = document.createElement("a");
a.href = downloadUrl;
a.download = "downloadFile.xlsx";
document.body.appendChild(a);
a.click();
}
});
Here is my server code
@RequestMapping(value = "/downloadExcel", method = RequestMethod.GET)
@ResponseBody
public List<LicenceType> downloadExcel() {
return licenceTypeService.findAllLicenceType();
}
=====
import excel
<script src="//cdn.rawgit.com/rainabba/jquery-table2excel/1.1.0/dist/jquery.table2excel.min.js"></script>
$("#Xcel").click(function () {
$("#Tab").table2excel({
exclude: '.exclude',
filename: 'MatchedSDNListNames.xls'
});
})
Can be a good choice if you are looking to import table as excel.
======
https://www.itread01.com/content/1548561620.html
https://www.aspsnippets.com/Articles/Download-Excel-File-XLS-and-XLSX-using-jQuery.aspx
=====
https://laracasts.com/discuss/channels/laravel/how-to-return-the-excel-download-to-ajax
$('#excel').on('click',function(){
$.ajax({
type : 'get',
url : '{{URL::to('downloadExcel_excess_milk')}}',
data:{
'search_time': $('#search_time').val(),
'fromdate': $('#fromdate').val(),
'todate': $('#todate').val()
},
success:function(data)
{
alert('success excel downloaded');
}
this is my controller
public function downloadExcel_excess_milk()
{
$ex = Excess_milk::get();
foreach($ex as $e)
{
$ex_array[]=array(
' Date'=>$e->date,
' Time'=>$e->time,
' No of litre'=>$e->no_of_litre,
' note'=>$e->note
);
}
Excel::create('salesdet', function($excel) use ($ex_array) {
$excel->sheet('mySheet', function($sheet) use ($ex_array)
{
return $sheet->fromArray($ex_array);
});
})->download('xlsx');
}
留言
張貼留言