1.安裝
代碼: 選擇全部
composer require maatwebsite/excel ~2.0.0
代碼: 選擇全部
//providers
Maatwebsite\Excel\ExcelServiceProvider::class,
//aliases
'Excel' => Maatwebsite\Excel\Facades\Excel::class,
代碼: 選擇全部
php artisan vendor:publish
app/Http/Controller/ExcelController.php
代碼: 選擇全部
php artisan make:controller ExcelController --plain
app/Http/Controller/ExcelController.php
代碼: 選擇全部
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Excel;
class ExcelController extends Controller
{
//Excel文件导出功能 By Laravel学院
public function export()
{
$cellData = [
['学号', '姓名', '成绩'],
['10001', 'AAAAA', '99'],
['10002', 'BBBBB', '92'],
['10003', 'CCCCC', '95'],
['10004', 'DDDDD', '89'],
['10005', 'EEEEE', '96'],
];
Excel::create('test', function ($excel) use ($cellData) {
$excel->sheet('score', function ($sheet) use ($cellData) {
$sheet->rows($cellData);
});
})->store('xls')->export('xls');
}
public function import()
{
$filePath = 'storage/exports/' . 'test.xls';
echo $filePath;
Excel::load($filePath, function ($reader) {
$data = $reader->all();
dd($data);
});
}
}
http://homestead.app/excel/export
http://homestead.app/excel/import