μλ νμΈμ μ¬λ¬λΆ,
λλ μ°λ¦¬ μ€ μΌλΆκ° ajax μμ²μμ νμΌμ μ 곡νλ €κ³ νλ κ²μ 보μμ΅λλ€. μ½κ°μ μ°κ΅¬ νμ, λλ κ·Έκ²μ ν λͺ νν ν΄κ²°μ± μ μ°Ύμ§ λͺ»νμ΅λλ€. κ·Έ μ€ μΌλΆλ₯Ό μ‘°μ νμ¬ Ajax μμ²μμ csv λ° xls λ°μ΄ν°λ₯Ό μ±κ³΅μ μΌλ‘ λ΄λ³΄λμ΅λλ€. λ¬Έμ λ νμΌ νμμ΄ xlsμΈ κ²½μ° μΈμ½λ© λλ¬Έμ μ‘°μμ΄ λ€λ₯΄λ―λ‘ μ½κ°μ μ‘°μ μ΄ μλ€λ κ²μ λλ€.
λ°μ΄ν°λ Arrayλ‘ λ³νλ μΌλ°μ μΈ Eloquent 쿼리μμ κ°μ Έμ΅λλ€.
PHP
if(!empty(Input::get('exportType'))) { //i use a custom get parameter here
$dd = Excel::create('testFileName', function($excel) use ($data) {
$excel->sheet('testSheetName', function($sheet) use ($data) {
$sheet->fromArray($data->get()->toArray());
});
$excel->setTitle($filename);
$excel->setLastModifiedBy(Carbon::now()->toDayDateTimeString()); //updated has Carbon::now() only now throw exception on vendor/phpoffice/phpexcel/Classes/PHPExcel/Writer/Excel5.php l847 strlen()
});
//tweak for serving XLS file from ajax (or go trough download() Excel method for csv files)
if(Input::get('exportType') == 'xls') {
$dd = $dd->string();
$response = array(
'filename' => 'testFileName', //as we serve a custom response, HTTP header for filename is not setted by Excel. From the JS, you need to retrieve this value if type is XLS to set filename
'file' => "data:application/vnd.ms-excel;base64,".base64_encode($dd)
);
return response()->success($response); //do a json encode
} else {
//direct use of Excel download method for non-xls files - xls files need special JS treatment
$dd->download(Input::get('exportType')); //not XLS, so CSV (didnt tried xlsx, pdf is blank but not sure it's related to this)
}
die; //prevent malformed binary data stream, not sure if needed
}
JS
$.ajax({
cache: false,
url: url, //GET route
responseType: 'ArrayBuffer', //not sure if needed
data: exportParam, //exportType parameter here
success: function (data, textStatus, request) {
//you could need to decode json here, my app do it automaticly, use a try catch cause csv are not jsoned
//already json decoded? custom return from controller so format is xls
if(jQuery.isPlainObject(data)) {
data = data.data; //because my return data have a 'data' parameter with the content
}
//V1 - http://stackoverflow.com/questions/35378081/laravel-excel-using-with-ajax-is-not-working-properly
//+V3 - http://stackoverflow.com/questions/27701981/phpexcel-download-using-ajax-call
var filename = "";
var disposition = request.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, '');
}
if(!jQuery.isPlainObject(data)) { //is CSV - we use blob
var type = request.getResponseHeader('Content-Type');
var blob = new Blob([data], { type: type ,endings:'native'});
var URL = window.URL || window.webkitURL;
var downloadUrl = URL.createObjectURL(blob);
}
var a = document.createElement("a");
a.href = jQuery.isPlainObject(data) ? data.file : downloadUrl;
a.download = jQuery.isPlainObject(data) ? data.filename : filename;
document.body.appendChild(a);
a.click();
a.remove();
},
error: function (ajaxContext) {
toastr.error('Export error: '+ajaxContext.responseText);
}
});
μΆμ : μ΄κ²μ λ¬Έμ κ° μλλλ€
CSVμμλ μ μλνμ§λ§ xlsμμλ μλνμ§ μμ΅λλ€.
ajaxμμ xlsxλ₯Ό λ°νν΄μΌ νκΈ° λλ¬Έμ λ€μ μ½κ° μ‘°μ νκ³ λ€μκ³Ό κ°μ΄ λλ©λλ€.
PHP
$dataλ Arrayλ‘ λ³νλ Eloquent 쿼리μ
λλ€.
$myFile= Excel::create("filename", function($excel) use($data) {
$excel->setTitle('title');
$excel->sheet('sheet 1', function($sheet) use($data) {
$sheet->fromArray($data, null, 'A1', true, true);
});
});
$myFile = $myFile->string('xlsx'); //change xlsx for the format you want, default is xls
$response = array(
'name' => "filename", //no extention needed
'file' => "data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64,".base64_encode($myFile) //mime type of used format
);
return response()->json($response);
js
$.ajax({
cache: false,
url: url, //GET route
data: params, //your parameters data here
success: function (response, textStatus, request) {
var a = document.createElement("a");
a.href = response.file;
a.download = response.name;
document.body.appendChild(a);
a.click();
a.remove();
},
error: function (ajaxContext) {
toastr.error('Export error: '+ajaxContext.responseText);
}
});
κ°μ¬ ν΄μ!!
"ν΄λμ€ 'Excel'μ μ°Ύμ μ μμ" μ€λ₯κ° λ°μν©λλ€. λμμ£Όμκ² μ΅λκΉ?
@randomhoodie μ΄λ€ μμ€μμ κ·Έ μ루μ μ μ»μμ΅λκΉ?
@eldyvoon λ΄κ° λ§νλ―μ΄ μλ λ΅λ³μ "μμ "νκ³ νμνμ§ μμ κ²μ μ κ±°νκ³ μ»΄ν©νΈνκ² λ§λ€κ³ MIME μ νμ ms office xlsx νμ₯μ μν duckduckgo(κ²μ μμ§), λλ νμ νμ§ λͺ»νμ΅λλ€. λ΄κ° κ·Έκ²μ μλ ν λκΉμ§ μλνμ§λ§ κ²μνκΈ° μ μ μλνκ³ μλνκΈ° λλ¬Έμ λκ΅°κ°κ° μ μ©νλ€κ³ μκ°ν κ²½μ°λ₯Ό λλΉνμ¬ κ²μνμ΅λλ€.
λλ μλ° μ€ν¬λ¦½νΈ λ μμ½μ€κ° μ ν νμνμ§ μλ€λ κ²μ λ°κ²¬νμ΅λλ€. λ€μν csv/xls/xlsx νμΌμ λ€μ΄λ‘λν μ μλ λ§ν¬κ° μλ μΉ νμ΄μ§κ° μλλ° νμ΄μ§λ₯Ό μλ‘ κ³ μΉκ³ μΆμ§ μμ΅λλ€. λ΄κ° ν λͺ¨λ κ²μ λ€μμ λ°ννλ μμ μ λ§ν¬λ₯Ό μ°κ²°νλ κ²μ λλ€ ...
κ³΅κ° ν¨μ getSpreadsheet() {
$items = νλͺ©::all();
Excel::create('items', function($excel) use($items) {
$excel->sheet('λ΄λ³΄λ΄κΈ° νμΌ', function($sheet) use($items) {
$sheet->fromArray($items);
});
})->λ΄λ³΄λ΄κΈ°('xls');
}
λ©μ§λ€!!!!
@randomhoodie κ°μ¬ν©λλ€!
3.x ν¨ν€μ§μ κ²½μ° μ κ·Έλ μ΄λ κ°μ΄λμ λ°λΌ λ€μκ³Ό κ°μ΄ PHPλ₯Ό μ λ°μ΄νΈν©λλ€.
$myFile = Excel::raw(new YOUR_Export_Class, \Maatwebsite\Excel\Excel::XLSX);
$response = array(
'name' => "filename", //no extention needed
'file' => "data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64,".base64_encode($myFile) //mime type of used format
);
return response()->json($response);
κ°μ¬ν©λλ€ @kynetiv , μ λ λ²μ 3.xλ₯Ό μ¬μ©νμ§λ§ λ€μκ³Ό κ°μ νμ₯μλ₯Ό λ£μ΄μΌ νμ΅λλ€. filename.xlsx
2020λ μλ μ΄ λ¬Έμ κ° κ³μ λ°μνλ κ²½μ° Laravel Excelμ λ²μ 3.xκ° λ³κ²½λμμΌλ―λ‘ μ¬κΈ°μ ν΄κ²°μ± μ΄ μμ΅λλ€.
μμ:
$exports = new ReportsExporter($data, $columns);
Excel::store($exports , 'filename.xlsx', 'custom_disk_location');
λ€μκ³Ό κ°μ΄ κ΅¬μ± νμΌ μμ€ν μμ μ¬μ©μ μ μ λμ€ν¬ μμΉλ₯Ό μ μνμμμ€.
'custom_disk_location' => [
'driver' => 'local',
'root' => public_path('files'),
],
...
μ΄λ κ² νλ©΄ Excel νμΌμ΄ μ μ₯μ/μ±μ μ μ₯λμ§ μμ΅λλ€.
κ·Έλ¬λ μλ²μ 곡κ°/νμΌ κ²½λ‘μ μ μ₯ν©λλ€.
μλ° μ€ν¬λ¦½νΈλ‘ λμκ°μ λ€μκ³Ό κ°μ νμΌμ λ€μ΄λ‘λνμμμ€.
function download(filename, path) {
let element = document.createElement('a');
element.setAttribute('href', path);
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
νμΌ μ΄λ¦κ³Ό κ²½λ‘λ₯Ό μ λ¬νμ¬ λ€μ΄λ‘λ κΈ°λ₯μ νΈμΆν©λλ€.
λ€μ΄λ‘λ("νμΌλͺ
.xlsx", location.origin+"νμΌλͺ
/νμΌλͺ
.xlsx");
λ€μ΄λ‘λ ν μλ²λ‘ λμκ°μ λ€μκ³Ό κ°μ΄ μλ²μ μ μ₯λ κ²μ μ κ±°νλ κ²μ μμ§ λ§μμμ€.
unlink("νμΌ/νμΌλͺ
.xlsx");
μ΄κ²μ΄ ajax λλ javascriptλ₯Ό ν΅ν΄ laravel-excelμ λ€μ΄λ‘λνκΈ° μ΄λ €μ΄ μ¬λμκ² λμμ΄ λκΈ°λ₯Ό λ°λλλ€.
μ΄κ²μ μ¬μ©μ κ²½νμ μ¬μ©μνν μ μλ λ λ§μ μ μ°μ±μ μ 곡νκ³
λ€μ΄λ‘λ μνμ κ΄ν νΌλλ°±μ μ 곡νκ³ μνλ λ°©μμΌλ‘ νμΌ μ΄λ¦μ μ§μ ν©λλ€.
ajaxμμ xlsxλ₯Ό λ°νν΄μΌ νκΈ° λλ¬Έμ λ€μ μ½κ° μ‘°μ νκ³ λ€μκ³Ό κ°μ΄ λλ©λλ€.
PHP
$dataλ Arrayλ‘ λ³νλ Eloquent 쿼리μ λλ€.$myFile= Excel::create("filename", function($excel) use($data) { $excel->setTitle('title'); $excel->sheet('sheet 1', function($sheet) use($data) { $sheet->fromArray($data, null, 'A1', true, true); }); }); $myFile = $myFile->string('xlsx'); //change xlsx for the format you want, default is xls $response = array( 'name' => "filename", //no extention needed 'file' => "data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64,".base64_encode($myFile) //mime type of used format ); return response()->json($response);
js
$.ajax({ cache: false, url: url, //GET route data: params, //your parameters data here success: function (response, textStatus, request) { var a = document.createElement("a"); a.href = response.file; a.download = response.name; document.body.appendChild(a); a.click(); a.remove(); }, error: function (ajaxContext) { toastr.error('Export error: '+ajaxContext.responseText); } });
ajaxμμ xlsxλ₯Ό λ°νν΄μΌ νκΈ° λλ¬Έμ λ€μ μ½κ° μ‘°μ νκ³ λ€μκ³Ό κ°μ΄ λλ©λλ€.
PHP
$dataλ Arrayλ‘ λ³νλ Eloquent 쿼리μ λλ€.$myFile= Excel::create("filename", function($excel) use($data) { $excel->setTitle('title'); $excel->sheet('sheet 1', function($sheet) use($data) { $sheet->fromArray($data, null, 'A1', true, true); }); }); $myFile = $myFile->string('xlsx'); //change xlsx for the format you want, default is xls $response = array( 'name' => "filename", //no extention needed 'file' => "data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64,".base64_encode($myFile) //mime type of used format ); return response()->json($response);
js
$.ajax({ cache: false, url: url, //GET route data: params, //your parameters data here success: function (response, textStatus, request) { var a = document.createElement("a"); a.href = response.file; a.download = response.name; document.body.appendChild(a); a.click(); a.remove(); }, error: function (ajaxContext) { toastr.error('Export error: '+ajaxContext.responseText); } });
λ무 μ’μ
κ°μ₯ μ μ©ν λκΈ
ajaxμμ xlsxλ₯Ό λ°νν΄μΌ νκΈ° λλ¬Έμ λ€μ μ½κ° μ‘°μ νκ³ λ€μκ³Ό κ°μ΄ λλ©λλ€.
PHP
$dataλ Arrayλ‘ λ³νλ Eloquent 쿼리μ λλ€.
js