Laravel之安裝maatwebsite/excel

Laravel表格導(dǎo)入導(dǎo)出

在你項(xiàng)目根目錄找到composer.json里的require添加如下

"require": {
        "maatwebsite/excel": "^3.1"
    },

執(zhí)行更新

composer update

OR

 composer require maatwebsite/excel

在config/app.php添加

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

發(fā)布資源命令:

php artisan vendor:publish --provider="Maatwebsite\Excel\ExcelServiceProvider"

假設(shè)導(dǎo)入學(xué)生execl:
創(chuàng)建文件

<?php

namespace App\Imports;

use App\Http\Model\Student;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithHeadingRow;

class StudentImport implements ToModel
{
    /**
    * @param array $row
    *
    * @return \Illuminate\Database\Eloquent\Model|null
    */
    public function model(array $row)
    {
        return new Student([
            'grade'=>$row[5],
            'class'=>$row[6],
            'student_number'=>$row[7],
            'student_name'=>$row[9],
            'sex'=>$row[10],
            'card'=>$row[11],
    
        ]);
    }

Controller

 public function import(Request $request):array {
        $arr = \Maatwebsite\Excel\Facades\Excel::toArray(new StudentImport,$request->file('file'));
        return ['code'=>200]
}

導(dǎo)出:
創(chuàng)建導(dǎo)出文件:

<?php
/*
   *author:mj.shu
   *time 2017/3/15 3:37 PM
   *All rights reserved
*/


namespace App\Http\Controllers\V1\Admin\Execl;


use App\Http\Model\Bunk;
use App\Http\Model\Dorm;
use App\Http\Model\Student;
use App\Http\Model\Tower;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Maatwebsite\Excel\Concerns\FromArray;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithCustomValueBinder;
use Maatwebsite\Excel\Concerns\WithHeadings;
use PhpOffice\PhpSpreadsheet\Cell\Cell;
use PhpOffice\PhpSpreadsheet\Cell\DataType;
use PhpOffice\PhpSpreadsheet\Cell\DefaultValueBinder;

class Execl extends DefaultValueBinder implements FromArray,WithHeadings,ShouldAutoSize,WithCustomValueBinder
{

   protected $arr;

   public function __construct(array $arr)
   {
       $this->arr = $arr;
   }

   public function bindValue(Cell $cell, $value)
   {
       if (strlen($value) > 10) {
           $cell->setValueExplicit($value, DataType::TYPE_STRING);

           return true;
       }

       // else return default behavior
       return parent::bindValue($cell, $value);
   }

   public function headings(): array
   {
      $where = $this->arr;
      switch ($where['where']){
          case 0: //未入住學(xué)生
              $array =  ['id','姓名','性別','年紀(jì)','班級','學(xué)號','證件','培養(yǎng)類型','入學(xué)時(shí)間','手機(jī)號','郵箱'];
              break;
          case 1:
              $array = ['id','樓宇','層數(shù)','房間號','年紀(jì)','性別','朝向','類型','價(jià)格','總床位','空閑','床位編號','居住人'];
              break;
          case 2:
              $array = ['id','樓宇','層數(shù)','房間號','年紀(jì)','性別','朝向','類型','價(jià)格','床位編號','居住人','總床位','空閑'];
              break;
          case 3:
              $array = ['id','樓宇','層數(shù)','房間號','年紀(jì)','性別','朝向','類型','價(jià)格','總床位','空閑'];
              break;
      }
      return $array;

   }

headings()是自定義標(biāo)題,在導(dǎo)出長數(shù)字的時(shí)候比如身份證,會形成1.0E之類的科學(xué)計(jì)算,這個(gè)時(shí)候需要判斷類型,將其定義為String類型

public function bindValue(Cell $cell, $value)
    {
        if (strlen($value) > 10) {
            $cell->setValueExplicit($value, DataType::TYPE_STRING);

            return true;
        }

        // else return default behavior
        return parent::bindValue($cell, $value);
    }

繼續(xù)控制器調(diào)用:

 public function downXls(Request $request)
        {
            $data = $request->all();
            return \Maatwebsite\Excel\Facades\Excel::download(new Execl($where), 'data.xls');

        }

至此,Execl表格導(dǎo)入導(dǎo)出完成

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容