当前位置: 首页 > 后端技术 > PHP

Laravel6配合Maatwebsite-Excel实现Excel导入

时间:2023-03-29 20:22:49 PHP

上一段,项目需要通过Excel导入用户。之前用过phpexcel,总觉得太繁琐了。在我的印象中,phpexcel已经很久没有更新了。看到项目中使用了Maatwebsite\Excel,试试看。安装composerrequiremaatwebsite/excelimportandgenerateimportclassphpartisanmake:importAdminsImport--model=Admin你会看到app下生成了Imports文件夹。完善业务逻辑$row[2],'password'=>bcrypt($row[3]),'api_token'=>str_random(60),]);}}importtaskinfo($this->description.'完成');}}其他逻辑当然业务可能不只是写数据,可能会有一些操作来设计具体的业务,所以可以这样操作createData($rows);0;foreach($rowsas$row){$row[0]=(int)$row[0];如果(空($row[0])){继续;}(newAdmin())->create(['username'=>$row[2],'name'=>$row[2],'password'=>bcrypt($row[3]),'api_token'=>str_random(60),]);//其他业务$success++;}返回$success.'-'.count($rows);}}执行php7.2artisanimportAdmin总的来说,使用起来简单明了。更多具体的导入实现可以在Maatwebsite\Excel\Excel中搜索查看,还有export,importinqueue等,支持的格式也多种多样,具体代码如下,功能还是很强大的,足以满足日常需要。writer=$作家;$this->reader=$reader;$this->文件系统=$文件系统;$this->queuedWriter=$queuedWriter;}/***{@inheritdoc}*/publicfunctiondownload($export,string$fileName,string$writerType=null,array$headers=[]){returnresponse()->download($this->export($export,$fileName,$writerType)->getLocalPath(),$fileName,$headers)->deleteFileAfterSend(true);}/***{@inheritdoc}*/publicfunctionstore($export,string$filePath,string$diskName=null,string$writerType=null,$diskOptions=[]){if($exportinstanceofShouldQueue){return$this->queue($export,$filePath,$diskName,$writerType,$diskOptions);}$temporaryFile=$this->export($export,$filePath,$writerType);$exported=$this->filesystem->disk($diskName,$diskOptions)->copy($temporaryFile,$filePath);$临时文件->删除();返回$导出;}/***{@inheritdoc}*/publicfunctionqueue($export,string$filePath,string$disk=null,string$writerType=null,$diskOptions=[]){$writerType=FileTypeDetector::detectStrict($文件路径,$writerType);返回$this->queuedWriter->store($export,$filePath,$disk,$writerType,$diskOptions);}/***{@inheritdoc}*/publicfunctionraw($export,string$writerType){$temporaryFile=$this->writer->export($export,$writerType);$contents=$temporaryFile->contents();$临时文件->删除();返回$内容;}/***{@inheritdoc}*/publicfunctionimport($import,$filePath,string$disk=null,string$readerType=null){$readerType=FileTypeDetector::detect($filePath,$readerType);$response=$this->reader->read($import,$filePath,$readerType,$disk);如果($responseinstanceofPendingDispatch){return$response;}返回$this;}/***{@inheritdoc}*/publicfunctiontoArray($import,$filePath,string$disk=null,string$readerType=null):array{$readerType=FileTypeDetector::detect($filePath,$readerType);返回$this->reader->toArray($import,$filePath,$readerType,$disk);}/***{@inheritdoc}*/publicfunctiontoCollection($import,$filePath,字符串$disk=null,字符串$readerType=null):Collection{$readerType=FileTypeDetector::detect($filePath,$readerType);返回$this->reader->toCollection($import,$filePath,$readerType,$disk);}/***{@inheritdoc}*/publicfunctionqueueImport(ShouldQueue$import,$filePath,string$disk=null,string$readerType=null){return$this->import($import,$filePath,$disk,$读者类型);}/***@paramobject$export*@paramstring|null$fileName*@paramstring$writerType**@throws\PhpOffice\PhpSpreadsheet\Exception*@returnTemporaryFile*/protectedfunctionexport($export,string$fileName,字符串$writerType=null):TemporaryFile{$writerType=FileTypeDetector::detectStrict($fileName,$writerType);返回$this->writer->export($export,$writerType);}}最后,感谢下面这篇站内文章让我快速上手Maatwebsite/Excel3.1教程(导入)最后附上LaravelExcel文档:LaravelExcel