导入CSV文件扩展名为yii2-csv-importer

时间:2017-12-30 20:59:49

标签: yii2 import-csv

我不确定如何使用扩展程序“yii2-csv-importer”。 我需要导入位于 app / web / uploads / mod-key.csv 目录中的CSV文件

我的表/模型(BASE)和csv文件具有以下结构:

'id'
'date'
'Pan
'key'
'goal'
'accomplished'
'Weight'
'points'
'update'

我的行动怎么样?

更新

use ruskid\csvimporter\CSVImporter;
use ruskid\csvimporter\CSVReader;
use ruskid\csvimporter\ImportInterface;
use ruskid\csvimporter\MultipleImportStrategy;
use ruskid\csvimporter\BaseImportStrategy;


$importer = new CSVImporter();

$importer->setData(new CSVReader([
'filename' => Yii::$app->request->baseUrl."/uploads/mod-key.csv",
'tableName' => Base::tableName(),
'fgetcsvOptions' => [
'delimiter' => ';'
]
]));

$numberRowsAffected = $importer->import(new MultipleImportStrategy([
'tableName' => ModelName::tableName(), // change your model names accordingly
'configs' => [
[
    'attribute' => 'id',
    'value' => function($line) {
        return $line[0];
    }
],
[
    'attribute' => 'date',
    'value' => function($line) {
        return $line[1];
    }
]
// put your remaining columns here
],
]));

1 个答案:

答案 0 :(得分:0)

use ruskid\csvimporter\CSVImporter;
use ruskid\csvimporter\CSVReader;
use ruskid\csvimporter\ImportInterface;
use ruskid\csvimporter\MultipleImportStrategy;
use ruskid\csvimporter\BaseImportStrategy;


$importer = new CSVImporter();

$importer->setData(new CSVReader([
'filename' => Yii::$app->request->baseUrl."/uploads/mod-key.csv",
'tableName' => Base::tableName(),
'fgetcsvOptions' => [
'delimiter' => ';'
]
]));

$numberRowsAffected = $importer->import(new MultipleImportStrategy([
'tableName' => ModelName::tableName(), // change your model names accordingly
'configs' => [
[
    'attribute' => 'id',
    'value' => function($line) {
        return $line[0];
    }
],
[
    'attribute' => 'date',
    'value' => function($line) {
        return $line[1];
    }
]
// put your remaining columns here
],
]));