composer插件之----Box\Spout\导入

2019-10-12  本文已影响0人  安晓生

目前我在项目中用到的是3.1版本的导入。
老的版本2.7的,已经废弃了好多方法。
下面的代码是3.1的,,老的版本后面注释有接受。就是废弃了。改用前面的了。

use Box\Spout\Reader\Common\Creator\ReaderEntityFactory; // namespace is no longer "Box\Spout\Reader"
...
$reader = ReaderEntityFactory::createXLSXReader(); // replaces ReaderFactory::create(Type::XLSX)
$reader = ReaderEntityFactory::createCSVReader();  // replaces ReaderFactory::create(Type::CSV)
$reader = ReaderEntityFactory::createODSReader();  // replaces ReaderFactory::create(Type::ODS)

言归正传,直接上代码。
下面是官方文档的代码demo 没有给具体的怎么使用。
在我半天的研究中,搞定了。

foreach ($reader->getSheetIterator() as $sheet) {
    foreach ($sheet->getRowIterator() as $row) { // $row is a "Row" object, not an array
        $rowAsArray = $row->toArray();  // this is the 2.x equivalent
        // OR
        $cellsArray = $row->getCells(); // this can be used to get access to cells' details
        ... 
    }
}

自己搞定的代码

use Box\Spout\Reader\Common\Creator\ReaderEntityFactory;

public function handle()
    {
        $array = ['医疗险'=>'medical','意外险'=>'accident','重疾险'=>'severe','寿险'=>'life','年金险'=>'annuity'];
        $flag = ['投保'=>'0','退保'=>'1'];
        $fileName = UploadedFile::getInstance($this, 'file');//框架接受上传过来的文件路径
        $reader = ReaderEntityFactory::createXLSXReader();
        $reader->open($fileName->tempName);
        foreach ($reader->getSheetIterator() as $sheet) {
            $i = 0;
            foreach ($sheet->getRowIterator() as $row) { // $row is a "Row" object, not an array
                if ($i > 0) {
                    $data = $row->toArray();
                    $channel_id = Channel::find()->select(['id'])->where(['short_name'=>$data['12']])->asArray()->one();
                    $userId = User::userMobile($data['4']);
                    /* @var Insurance $model */
                    $model = Yii::createObject(Insurance::class);
                            $model->setAttributes([
                                'user_id'               => empty($userId) ? "0" : $userId,
                                'product_type'          => empty($data['0'])? "" : $array[$data['0']],
                                'product_name'          => empty($data['1']) ? "" : $data['1'],
                                'policy_no'             => empty($data['2'])? "" : $data['2'],
                                'insure_real_name'      => empty($data['3'])? "" : $data['3'],
                                'insure_mobile'         => empty($data['4']) ? "" : $data['4'],
                                'guard_real_name'       => empty($data['5']) ? "" : $data['5'],
                                'payment_amount'        => empty($data['6']) ? "" : $data['6'],
                                'safeguard_amount'      => empty($data['7']) ? "" : $data['7'],
                                'safeguard_start_time'  => empty($data['8']) ? "" : $data['8'],
                                'safeguard_end_time'    => empty($data['9']) ? "" : $data['9'],
                                'fees_year'             => empty($data['10'])? "" : $data['10'],
                                'flag'                  => empty($data['11'])? "" : $flag[$data['11']],
                                'product_channel_id'    => empty($channel_id['id']) ? "0" :$channel_id['id'],
                            ], false);
                            $model->save();
                }
                $i++;
            }
        }
        $reader->close();
        return true;
    }

上面代码是YII2 框架的代码。自己做的一个实验。大家能用到的,可以根据自己需求来。
Laravel的用法

$reader = ReaderEntityFactory::createXLSXReader(); // 指定文件类型
$reader->open($excel_path); // 导入文件(需从本地导入,例如 storage/app/public/ 文件夹下)

foreach ($reader->getSheetIterator() as $k => $sheet) { // 遍历sheet
    foreach ($sheet->getRowIterator() as $k => $row) { // 遍历行
        $rowArr = $row->toArray(); // 行数据数组
        // TODO...
    }
}

$reader->close(); // 释放内存

感觉没差别,如果对大家有用,欢迎评论,如果大家有好的写法,可以告诉我,我及时更新。

上一篇下一篇

猜你喜欢

热点阅读