Можете ли вы помочь мне импортировать Excel в мою программу?Я использую maatwebsite/excel
для загрузки файла Excel.
Итак, у меня есть
BarangController.php
public function import(Request $request)
{
// $path = $request->file('excel')->getRealPath();
$data = Excel::load($request->file('excel'))->get();
if($data->count()){
foreach ($data as $key => $value) {
$arr[] = ['nama_barang' => $value->nama_barang];
}
if(!empty($arr)){
return $arr;
}
}
}
Index.vue(модально)
<!-- Modal body -->
<div class="modal-body">
<div class="input-group">
<input @change="uploadExcel" id="excel" type="file" class="custom-file-input">
<label class="custom-file-label" for="excel">{{ fileName }}</label>
</div>
</div>
<!-- Modal footer -->
<div class="modal-footer">
<button @click="importExcel" type="button" class="btn btn-primary float-left">Import</button>
<button type="button" class="btn btn-danger float-right" data-dismiss="modal">Batal</button>
</div>
Index.vue (методы js)
importExcel() {
this.$Progress.start();
this.import.post('/api/barang/import').then((data) => {
console.log(data.data);
this.$Progress.finish();
}).catch(() => {
this.$Progress.fail();
})
},
uploadExcel(e) {
let file = e.target.files[0];
let reader = new FileReader();
// console.log(file);
if(file['type'] == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") {
this.fileName = file['name'];
//this.import.excel = file;
reader.onloadend = (file) => {
this.import.excel = reader.result;
}
reader.readAsDataURL(file);
//this.import.excel = file;
console.log(this.import.excel);
} else {
Swal.fire({
type: 'warning',
title: 'Format file salah'
})
}
},
И когда я проверяю консольный журнал, он не получает результат массивадля Excel, я надеюсь, вы можете помочь мне, спасибо