Maatwebsite / Laravel-Excel 3.1(只适用3.1以上)

14 篇文章 0 订阅

1.安装

composer require maatwebsite/excel

该Maatwebsite\Excel\ExcelServiceProvider是自动发现,并在默认情况下注册,但如果你想自己注册它:

添加 ServiceProvider config/app.php

'providers' => [
     /*
      * Package Service Providers...
      */
     Maatwebsite\Excel\ExcelServiceProvider::class,
]

添加 Facade in config/app.php

'aliases' => [
    ...
    'Excel' => Maatwebsite\Excel\Facades\Excel::class,
]

导出类代码(app\Tools\ExcelExports.php)

<?php
namespace App\Tools;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\WithHeadings;   //设置标题
use Maatwebsite\Excel\Concerns\WithEvents; //格式化单元格
use Maatwebsite\Excel\Events\AfterSheet; //格式化单元格

/**
 * Excel导出通用类
 */
class ExcelExports implements FromCollection, WithHeadings  ,WithEvents
{
    use Exportable;

    private $data;
    private $headings;

    //数据注入
    public function __construct($data, $headings)
    {
        $this->data = $data;
        $this->headings = $headings;
    }

    //实现FromCollection接口
    public function collection()
    {
        return collect($this->data);
    }

    //实现WithHeadings接口
    public function headings(): array
    {

        return $this->headings;
    }
	
	/**
	 * @return array
	 */
	public function registerEvents(): array
	{
		
		return [
			AfterSheet::class    => function(AfterSheet $event) {
				// ... 此处你可以任意格式化

				$event->sheet->getDelegate()->getColumnDimension('A')->setWidth(30); //设置宽度
			},
		];
	}
}

导出类代码(app\Tools\ExcelImport.php)

<?php

namespace App\Tools;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
/**
 * Excel导入通用类
 */
class ExcelImport implements ToCollection
{
    public $data;
    protected $delTitle;

    /**
     *
     * @param $title integer   //去掉几行标题  默认一行
     */
    public function __construct($delTitle = 1)
    {
        $this->delTitle = $delTitle;
    }

    /**
     * @param Collection $rows
     */
    public function collection(Collection $rows)
    {
		
		
        $this->delTitle($rows);
        //$rows 是数组格式
        $this->data = $rows;
		
		foreach ($rows as $row) 
        {
           //这就是你导入的内容
			var_dump($row);die;
        }
		
		
    }

    public function delTitle (&$rows) {
        $rows = $rows->slice($this->delTitle)->values();
    }
}

调用方法(php artisan make:controller ExcelController)

<?php

namespace App\Http\Controllers;
use App\Tools\ExcelExports;
use App\Tools\ExcelImport;
use Illuminate\Http\Request;
use Excel;
use DB;


class ExcelController extends Controller
{
    public function export(){
        $cellData = DB::table('user')->select('id','name')->get()->toArray();
        $header = [
            'id'  => '编号', 
			'name' => '名字', 
        ];
		return Excel::download(new ExcelExports($cellData,$header), 'users.xlsx');

    }
		//Excel文件导入功能
	public function import(){
		$filePath = iconv('UTF-8', 'GBK', '20201230090705').'.xls';
		// 'imports' 在\config\filesystems.php 添加以下代码
		// 'disks' => [
			 // ... 
			// 'imports' => [
				// 'driver' => 'local',
				// 'root' => public_path('uploads/behaviors/'), //导入文件存放的目录
				// 'url' => env('APP_URL').'/public',
				// 'visibility' => 'public',
			// ],

		// ],
		
		Excel::import(new ExcelImport, $filePath, 'imports');


	}

}




根据以下文章所写
使用方法:
http://www.edbiji.com/doccenter/showdoc/209/nav/3722.html

https://www.cnblogs.com/niuben/p/11458450.html

设置列宽等其他功能使用方法:
https://learnku.com/laravel/t/24161

https://blog.csdn.net/lihongxian/article/details/106521367?utm_medium=distribute.pc_relevant.none-task-blog-OPENSEARCH-3.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-OPENSEARCH-3.control

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值