我需要上传excel文件并从数组创建数据表。怎么做呢?我有问题,使postExcel返回。
我使用http://www.maatwebsite.nl/laravel-excel/docs和http://datatables.net/
下面是我的代码:public function postExcel(Request $request)
{
if($request->ajax()) {
if ($request->hasFile('file'))
{
$file = $request->file('file');
$filename = $file->getClientOriginalName();
//$file->getRealPath();
$path = $file->move(public_path(), $filename);
'Excel::load($path, function($reader){
$results = $reader->toArray();
});
print_r($results);die();
$json = array(
'status' => 'ok',
'array' => $results
);
}
return Response::json($json);
}
}
这是viewjs我需要生成表:
$(document).on("change", "#file-input", function() {
var formData = new FormData($('form.upload-excel')[0]);
$.ajax({
type: 'POST',
data: formData,
url: '/Excel/public/upload-excel',
cache: false,
contentType: false,
processData: false,
success: function(data){
if(data.status === 'ok') {
console.log(data.array);
$('#table').DataTable({
colReorder: true,
});
}
}
});
});
问题解决。这是:
$data = 'Excel::load($path)->toArray();
$json = array(
'status' => 'ok',
'data' => $data
);