Laravel -- Excel 導入(import) (v2.1.0)


原博客地址 https://www.jianshu.com/p/7287ebdc77bb

Install (安裝)
//> composer.json 中 require中添加如下: "maatwebsite/excel": "~2.1.0" //> 然后使用如下composer命令安裝 composer update 
//> config/app.php 中注冊 服務提供類 providers數組中 Maatwebsite\Excel\ExcelServiceProvider::class, //> config/app.php 中注冊 門面類 aliases 數組中 'Excel' => Maatwebsite\Excel\Facades\Excel::class, 
//> 移動配置文件 excel.php 到 config目錄下面 命令: php artisan vendor:publish --provider="Maatwebsite\Excel\ExcelServiceProvider" 
//> 服務器容器中綁定 的是'excel' App::make('excel') 
Import(導入)
  • 我們來修改 config/excel.php 的導入配置參數
//> 修改一下 excel.php 配置文件 的 import 數組參數,否則導入時只解析ASCII(對中文不敏感) 'to_ascii' => false # 這個參數改為false(否則,只會得到excel的ascii編碼以內組成的字符串) 'heading' => 'slugged' # 表示得到關聯數組(鍵是Excel的A1行標題) # 'heading' => false # 表示得到索引數組(從0開始到...)(其他參數請自行嘗試) 'startRow' => 1 # 如果沒特殊需求,就使用默認值(表示開始取得位置 row :從第幾行還是取值) # 這里就采用默認值吧(輸入、輸出編碼) 'encoding' => [ 'input' => 'UTF-8', 'output' => 'UTF-8' ], # 根據需要適當配置該參數(作用:如果設置為true,會自動忽略為空的某一數值) # 比如 SPU標題下存在一行為空值,那么該行中就不會出現 apu => null 情況(會自動忽略) 'ignoreEmpty' => false, # 改值為 false (表示:當前我們默認只或的一個sheet文件);true:表示當前默認獲取多個sheet文件;(如果不是多文件導入,默認設置為false) 'force_sheets_collection' => false, //> 其他import參數,遇到再詳細討論 
//> 文件導入 Excel::load($fileName, function ($reader){ foreach ($reader->get() as $item){ dump($item); } }); # 參看一下上傳控制器 動作 public function excel(Request $request){ //> 判斷請求類型 if( $request->isMethod('post') && $_FILES['file'] ){ //> 獲取上傳文件路徑 $_FILES if( $_FILES['file']['error'] == 0 ){ //> 獲取上傳文件名稱(已便於后面判斷是否上傳需要后綴文件) $name = $_FILES['file']['name']; //> 獲取上傳文件后綴 如(xls exe xlsx 等) $ext = strtolower(trim(substr($name,(strpos($name,'.')+1)))); //> 判斷文件是否為指定的上傳文件后綴 if( ! in_array($ext,array('xls','xlsx')) ){ //> 返回上一次請求位置,並攜帶錯誤消息 return redirect()->back()->withErrors('請輸入xls或xlsx后綴文件')->withInput(); } //> 獲取文件上傳路徑 $fileName = $_FILES['file']['tmp_name']; //> excel文件導入 上傳文件 Excel::load($fileName, function ($reader){ //> 處理上傳文件數據 此時 處理多個上傳的 sheet 文件 foreach ($reader->get() as $item){ //> 處理相關上傳excel數據 dump($item); } }); } exit; } return redirect()->route('home.index'); } //> 現在我們討論一下get()和all()方法 load回調的$reader->get()或$reader->all()並沒有很大的區別(得到結果都是一樣的) # 獲取 sheet 標題 (改標題) $reader->getTitle() 
# 獲取指定 前 $limit 行 數據 # ->takeRows($limit) 獲取指定的前$limit條數據 Excel::load($fileName, function ($reader){ //> takeRows 限制獲取數據 foreach ($reader->takeRows(10)->get() as $item) { dump($item); } }); # ->limitRows($start=0,$end) 獲取指定長度數據 Excel::load($fileName, function ($reader){ //> limitRows(5,10):獲取第5條到第10條數據 foreach ($reader->limitRows(5,10)->get() as $item) { dump($item); } }); # ->skipRows($num):跳過指定行 Excel::load($fileName, function ($reader){ //> skipRows($num) :開始跳過多少行,在取數據(這里:從第5行開始,取10條數據) //> skipRows和takeRows一起使用才能達到上面描述的效果(和limitRows()好像就無效了) foreach ($reader->skipRows(5)->takeRows(10)->get() as $item) { dump($item); } }); 
//> 上面是獲取行;下面介紹獲取列 # ->takeColumns() 類比 takeRows()方法:只是takeColumns獲取的是列數據 # ->limitColumns() 類比 limitRows()方法:只是limitColumns()獲取的是列數據 # ->skipColumns() 類比 skipColumns()方法: 只是skipColumns()跳過的列 
//> toArray() 對象轉換為數組 Excel::load($fileName, function ($reader){ //> 這樣使用也是可以的 $reader->get()->toArray(); foreach ($reader->get() as $item) { //> toArray() 轉換為數組 dump($item->toArray()); } }); //> toObject() 轉換為對象 # toObject() 好像並沒有多大用處(一直提示錯誤) 
Laravel Excel提供了兩個打印方法(並沒有多個用處)
$reader->dump();
$reader->dd();
//> each() 遍歷方法(該方法類似 foreach) Excel::load($fileName, function ($reader){ $reader->each(function($sheet){ $sheet->each(function($row){ dump($row); }); }); }); 
  • 選擇頁和列
//> Excel門面提供一個 selectSheets(...$sheet) 方法:允許打開指定的sheet文件 # 當我們上傳多個sheet文件需要被處理時,加載指定的sheet文件就可以使用該方法 Excel::selectSheets('sheet1', 'sheet2')->load(); //> 當然我們也可以選擇一張sheet表加載 //> 通過索引加載sheet文件 Excel::selectSheetsByIndex(0, 1)->load(); //> 表示加載第一個第二個sheet文件 
//> 獲取指定sheet文件指定字段 $reader->select(array('firstname', 'lastname'))->get(); //> 獲取firstname和lastname字段 # Or $reader->get(array('firstname', 'lastname')); //> 直接給get()傳遞參數 
  • 日期處理
//> 日期形式默認會被解析成Carbon對象 如 2017-01-01 08:00 //> 我們來看一下日期配置參數 dates => [ //> 是否開啟日期解析:默認開啟(解析日期字符串) //> 如果設置為false:2017-01-01 08:00 可能會得到一個 1/1/17 08:00結果(由於美格式(月/日/年)可能會有些區別) 'enabled' => true, //> 日期格式:如果設置為false 將返回一個Carbon時間對象 # 'format' => 'U' 時:返回一個時間戳 如 2016/1/1 8:12:00 => 1451635920 'format' => false, //> 日期字段,那些字段是日期字段 如果不存在值:默認解析值為字符串 時間的形式的 數據 //> 如果寫上:解析指定的字段 值 為指定字符串的形式 'columns' => [] ], //> ---------------------------------------------------------- //> date 參數是處理整個導入文件時(如果我們想處理單獨某個字段數據時) # ->formatDates($boolean, $format) 開啟或關閉 日期 參數 $reader->formatDates(true); $reader->formatDates(false); $reader->formatDates(true, 'Y-m-d H:i'); //> 轉換為 Y-m-d H:i 時間形式 Excel::load($fileName, function ($reader){ $reader->formatDates(false)->each(function($sheet){ $sheet->each(function($row){ dump($row); }); }); }); $reader->formatDates(true, 'U'); 裝換為時間戳形式 //> format($dateFormat) 轉換為指定數據形式 Excel::load($fileName, function ($reader){ $reader->each(function($sheet){ $sheet->each(function($row){ dump($row->{"顏色分類"}); dump($row->{"顏色分類"}->format('U')); dump($row->{"顏色分類"}->format('Y-m-d')); }); exit; }); }); //> setDateFormat($format) 默認返回指定形式,不在返回Carbon對象 $reader->setDateFormat('Y-m-d'); //> 設置日期列 setDateColumns() $reader->setDateColumns(array( 'created_at', 'deleted_at' ))->get(); 
import配置文件 參數 :calculate 是否開啟計算 (經測試,沒什么用處)
// Enable calculation $reader->calculate(); // Disable calculation $reader->calculate(false); 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM