Laravel Excel导入.csv行结尾无法识别

时间:2016-08-31 07:02:41

标签: php excel laravel csv laravel-excel

我正在尝试使用Laravel Excel存储库在Laravel中使用excel文件(.csv)的数据更新sql数据库表。

我的控制器功能返回excel文件的内容,但只是一个2的数组(它应该是604)。

因此,我认为我必须在我的功能中添加'line ending \ t'。
但我不知道如何。

<小时/>
这是我目前所知的:

控制器

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');
}


根据{{​​3}},我必须将其添加到我的项目中以识别正确的行结尾:

class UserListImport extends \Maatwebsite\Excel\Files\ExcelFile {
    protected $lineEnding = '\t';
}



如果我的预感是正确的,文档中的这个片段将解决我的问题:

我在哪里创建此文件,其中包含文档中的代码?

我是否必须更改其他任何内容才能使该文件生效?

<小时/> 我是Laravel的新手,我会非常感谢任何帮助!!


更新

错误消息

  

Class App \ UserListImport包含1个抽象方法,因此必须   被宣布为抽象或实施其余方法   (Maatwebsite \ EXCEL \文件\ 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();
}

2 个答案:

答案 0 :(得分:1)

文档假设您创建了一个新的自定义类Excelfile

class UserListImport extends \Maatwebsite\Excel\Files\ExcelFile

意味着这个。

我通常更喜欢为应用程序需要的任何内容添加自己的应用程序命名空间文件夹。

在这种情况下,我会从控制器中提取任何内容并调用我自己的类来处理Input::file('import_file')。该类是放置该属性以覆盖库与文件交互方式的地方。

答案 1 :(得分:0)

session_start(); if(!isset($_SESSION['SESS_MEMBER_ID']) || (trim($_SESSION['SESS_MEMBER_ID']) == '')) { header("location: index.php"); //Write action to txt log $log = "User: ".$_SERVER['REMOTE_ADDR'].' - '.date("F j, Y, g:i a").PHP_EOL. "Attempt: ".($result[0]['success']=='1'?'Success':'Failed').PHP_EOL. "User: ".($_SESSION['SESS_USER_ID']).PHP_EOL. "User Type: ".($_SESSION['SESS_USER_TYPE']).PHP_EOL. "-------------------------".PHP_EOL; //- file_put_contents('./log_'.date("j.n.Y").'.txt', $log, FILE_APPEND); //exit(); } $expiry = 60;//session expiry required after 30 mins if (isset($_SESSION['LAST']) && (time() - $_SESSION['LAST'] > $expiry)) { session_unset(); session_destroy(); header("location: logout.php"); } $_SESSION['LAST'] = time(); 表示制表(单词之间的差距)。对于行结尾,使用以下字符组合\t。这取决于操作系统。 '\n', '\r\n'适用于Unix / Linux / Mac OS,'\n'适用于Windows系列。