在 Laravel 5 中使用 Laravel Excel 实现 Excel/CSV 文件导入导出功能
發表於 : 2015-12-06 16:16:50
http://laravelacademy.org/post/2024.html
1.安裝
2.設定 config/app.php
3.產生設定檔 config/excel.php
4.產生 controller
app/Http/Controller/ExcelController.php
5.修改
app/Http/Controller/ExcelController.php
6.測試
http://homestead.app/excel/export
http://homestead.app/excel/import
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