Laravel Excel导入.csv行尾无法识别
我正在尝试使用Laravel Excel存储库在Laravel中使用Excel文件(.csv)的数据更新sql数据库表 。
我的控制器函数给出的Excel文件的内容,但只是在一个数组2(应该是604)。
因此,我想我将不得不添加'行结束\ t'我的function。
但是我不知道怎么做。
这是我迄今所知道的:
控制器
public function uploadExcel() { Excel::load(Input::file('import_file'), function ($reader) { foreach ($reader->toArray() as $value) { $insert[] = [ 'member_title' => $value->member_title, 'member_first_name' => $value->member_first_name, 'member_name_affix' => $value->member_name_affix, 'member_last_name' => $value->member_last_name, 'member_private_address' => $value->member_private_address, 'member_private_zip_code' => $value->member_private_zip_code, 'member_private_location' => $value->member_private_location, 'member_private_phone' => $value->member_private_phone, 'member_private_mobile' => $value->member_private_mobile, 'member_private_fax' => $value->member_private_fax, 'member_private_mail' => $value->member_private_mail, 'member_business_position' => $value->member_business_position, 'member_business_name' => $value->member_business_name, 'member_business_address' => $value->member_business_address, 'member_business_zip_code' => $value->member_business_zip_code, 'member_business_location' => $value->member_business_location, 'member_business_area_code' => $value->member_business_area_code, 'member_business_phone' => $value->member_business_phone, 'member_business_fax' => $value->member_business_fax, 'member_business_mobile' => $value->member_business_mobile, 'member_business_mail' => $value->member_business_mail, 'member_join_date' => $value->member_join_date, 'extra' => $value->extra ]; } }); if(!empty($insert)) { die(var_dump($insert)); <-- puts out the array for testing DB::table('members')->insert($insert); } return redirect('index.index'); }
根据官方文件,我不得不把这个添加到我的项目中,以识别正确的行结尾:
class UserListImport extends \Maatwebsite\Excel\Files\ExcelFile { protected $lineEnding = '\t'; }
如果我的预感是正确的,从文档中的这个片段将解决我的问题:
我在哪里必须创build这个文件,其中包含文档中的代码?
还有,我必须改变任何东西,使该文件的影响?
我是Laravel的新手,我会非常感谢任何帮助!
UPDATE
错误信息
类App \ UserListImport包含1个抽象方法,因此必须声明为抽象或实现其余方法(Maatwebsite \ Excel \ Files \ ExcelFile :: getFile)
应用程序/ UserListImport.php
namespace App; class UserListImport extends \Maatwebsite\Excel\Files\ExcelFile { protected $lineEnding = '\t'; public function loadExcel() { Excel::load(Input::file('import_file'), function ($reader) { foreach ($reader->toArray() as $value) { $insert[] = [ 'member_title' => $value->member_title, 'member_first_name' => $value->member_first_name, 'member_name_affix' => $value->member_name_affix, 'member_last_name' => $value->member_last_name, 'member_private_address' => $value->member_private_address, 'member_private_zip_code' => $value->member_private_zip_code, 'member_private_location' => $value->member_private_location, 'member_private_phone' => $value->member_private_phone, 'member_private_mobile' => $value->member_private_mobile, 'member_private_fax' => $value->member_private_fax, 'member_private_mail' => $value->member_private_mail, 'member_business_position' => $value->member_business_position, 'member_business_name' => $value->member_business_name, 'member_business_address' => $value->member_business_address, 'member_business_zip_code' => $value->member_business_zip_code, 'member_business_location' => $value->member_business_location, 'member_business_area_code' => $value->member_business_area_code, 'member_business_phone' => $value->member_business_phone, 'member_business_fax' => $value->member_business_fax, 'member_business_mobile' => $value->member_business_mobile, 'member_business_mail' => $value->member_business_mail, 'member_join_date' => $value->member_join_date, 'extra' => $value->extra ]; } }); if(!empty($insert)) { die(var_dump($insert)); DB::table('members')->insert($insert); } return redirect('index.index'); } }
调节器
use Maatwebsite\Excel\Facades\Excel; use App\UserListImport; public function uploadExcel() { UserListImport::loadExcel(); }
文档假设您创build了一个新的自定义类,扩展Excelfile
。
class UserListImport extends \Maatwebsite\Excel\Files\ExcelFile
意味着这是无稽之谈。
我通常更喜欢为我的应用程序需要的任何东西添加我自己的应用程序命名空间文件夹。
这种情况下,我会从控制器中提取任何东西,并调用我自己的类来处理Input::file('import_file')
。 该类是放置该属性的地方,用于覆盖库与文件交互的方式。
这意味着制表(字之间的差距)。 对于行尾,使用下列字符组合: '\n', '\r\n'
。 这取决于操作系统。 '\n'
用于Unix / Linux / Mac OS, '\r\n'
用于Windows系列。