官方文档地址 https://docs.laravel-excel.com/3.1/imports/batch-inserts.html
安装
- 安装Composer包
composer require maatwebsite/excel
- 在 config/app.php 中注册服务提供者到 providers 数组:
Maatwebsite\Excel\ExcelServiceProvider::class,
3.在 config/app.php 中注册到 aliases 数组:
‘Excel’ => Maatwebsite\Excel\Facades\Excel::class,
4.生成 Laravel Excel 的配置文件:(发布)
php artisan vendor:publish --provider=“Maatwebsite\Excel\ExcelServiceProvider”
至此,完成了composer包的安装步骤
导入使用说明
创建一个导入类
php artisan make:import 文件名称 --model=Model地址
或
php artisan make:import 文件名称
注:导入文件名命名为导入表模型名+Import,例如模型名WasteCode,导入类名WasteCodeImport
此操作会在app/下创建一个Import目录并创建一个文件
.
├── app
│ ├── Imports
│ │ ├── WasteCodeImport
│
└── composer.json
我这里需要导入的数据需要自己处理一下,所以使用的ToCollection的方法
app/Imports/WasteCodeImport代码如下:
<?php
namespace App\Imports;
use App\Models\admin\WasteCode;
use Maatwebsite\Excel\Concerns\ToCollection;
use Illuminate\Support\Collection;
class WasteCodeImport implements ToCollection
{
public function collection(Collection $rows)
{
foreach ($rows as $row)
{
}
WasteCode::insert($data);
}
public function createData($rows)
{
}
}
控制器调用
<?php
namespace App\Http\Controllers\WasteCode;
use Illuminate\Http\Request;
use App\Imports\WasteCodeImport;
use App\Http\Controllers\Controller;
use Maatwebsite\Excel\Facades\Excel;
class WasteCode extends Controller
{
public function importMember(Request $request)
{
$path = '文件路径';
Excel::import(new WasteCodeImport, $path);
}
}
导出使用说明
1.发布配置,请运行 vendor publish 命令:
php artisan vendor:publish
将会自动创建一个新配置文件config/excel.php
2、用法
先创建导出类,以导出用户为例
php artisan make:export Export
将会自动创建一个新文件app/Exports/Export.php
以下为导出类代码(Export.php)
<?php
namespace App\Exports;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Events\AfterSheet;
use PhpOffice\PhpSpreadsheet\Style\Border;
class Export implements FromCollection,WithHeadings, WithEvents
{
protected $data;
protected $headings;
protected $columnWidth = [];
protected $rowHeight = [];
protected $mergeCells = [];
protected $font = [];
protected $fontSize = [];
protected $bold = [];
protected $background = [];
protected $vertical = [];
protected $sheetName;
protected $borders = [];
public function __construct($data, $headings,$sheetName)
{
$this->data = $data;
$this->headings = $headings;
$this->sheetName = $sheetName;
$this->createData();
}
public function headings(): array
{
return $this->headings;
}
public function collection()
{
return new Collection($this->data);
}
public function createData()
{
$this->data = collect($this->data)->toArray();
}
public function registerEvents(): array
{
return [
AfterSheet::class => function(AfterSheet $event) {
$event->sheet->getDelegate()->getStyle('A1:Z1265')->getAlignment()->setVertical('center');
$event->sheet->getDelegate()->getStyle('A1:Z1265')->getAlignment()->setHorizontal('center');
foreach ($this->columnWidth as $column => $width) {
$event->sheet->getDelegate()
->getColumnDimension($column)
->setWidth($width);
}
foreach ($this->rowHeight as $row => $height) {
$event->sheet->getDelegate()
->getRowDimension($row)
->setRowHeight($height);
}
foreach ($this->vertical as $region => $position) {
$event->sheet->getDelegate()
->getStyle($region)
->getAlignment()
->setVertical($position);
}
foreach ($this->font as $region => $value) {
$event->sheet->getDelegate()
->getStyle($region)
->getFont()->setName($value);
}
foreach ($this->fontSize as $region => $value) {
$event->sheet->getDelegate()
->getStyle($region)
->getFont()
->setSize($value);
}
foreach ($this->bold as $region => $bool) {
$event->sheet->getDelegate()
->getStyle($region)
->getFont()
->setBold($bool);
}
foreach ($this->background as $region => $item) {
$event->sheet->getDelegate()->getStyle($region)->applyFromArray([
'fill' => [
'fillType' => 'linear',
'startColor' => [
'rgb' => $item
],
'endColor' => [
'argb' => $item
]
]
]);
}
foreach ($this->borders as $region => $item) {
$event->sheet->getDelegate()->getStyle($region)->applyFromArray([
'borders' => [
'allBorders' => [
'borderStyle' =>Border::BORDER_THIN,
'color' => ['argb' => $item],
],
],
]);
}
$event->sheet->getDelegate()->setMergeCells($this->mergeCells);
if(!empty($this->sheetName)){
$event->sheet->getDelegate()->setTitle($this->sheetName);
}
}
];
}
public function setColumnWidth (array $columnwidth)
{
$this->columnWidth = array_change_key_case($columnwidth, CASE_UPPER);
}
public function setRowHeight (array $rowHeight)
{
$this->rowHeight = $rowHeight;
}
public function setFont (array $font)
{
$this->font = array_change_key_case($font, CASE_UPPER);
}
public function setBold (array $bold)
{
$this->bold = array_change_key_case($bold, CASE_UPPER);
}
public function setBackground (array $background)
{
$this->background = array_change_key_case($background, CASE_UPPER);
}
public function setMergeCells (array $mergeCells)
{
$this->mergeCells = array_change_key_case($mergeCells, CASE_UPPER);
}
public function setFontSize (array $fontSize)
{
$this->fontSize = array_change_key_case($fontSize, CASE_UPPER);
}
public function setBorders (array $borders)
{
$this->borders = array_change_key_case($borders, CASE_UPPER);
}
}
3、调用方法 导出文件(UserController .php)
<?php
namespace App\Http\Controllers\Admin;
use Maatwebsite\Excel\Facades\Excel;
use App\Exports\UserExport;
use App\Models\User as userModel;
class UserController extends Controller
{
public function user_export(Request $request){
$row = [[
"id"=>'ID',
"nickname"=>'用户昵称',
"gender_text"=>'性别',
"mobile"=>'手机号',
"addtime"=>'创建时间 '
]];
0=>[
"id"=>'1',
"nickname"=>'张三',
"gender_text"=>'男',
"mobile"=>'18812345678',
"addtime"=>'2019-11-21 '
],
2=>[
"id"=>'2',
"nickname"=>'李四',
"gender_text"=>'女',
"mobile"=>'18812349999',
"addtime"=>'2019-11-21 '
]
];
?
$data = $list;
$header = $row;
$excel = new export($data, $header,'导出sheetName');
$excel->setColumnWidth(['B' => 40, 'C' => 40]);
$excel->setRowHeight([1 => 40, 2 => 50]);
$excel->setFont(['A1:Z1265' => '宋体']);
$excel->setFontSize(['A1:I1' => 14,'A2:Z1265' => 10]);
$excel->setBold(['A1:Z2' => true]);
$excel->setBackground(['A1:A1' => '808080','C1:C1' => '708080']);
$excel->setMergeCells(['A1:I1']);
$excel->setBorders(['A2:D5' => '#000000']);
return Excel::download($excel, '导出文件.xlsx');
?
参考了网上搜罗来的源码加上实际开发中用的sheetName设置、合并单元格、设置边框、设置字体等,网上留存,以备后用
|