xpbl4/yii2-import
最新稳定版本:v1.0.0
Composer 安装命令:
composer require xpbl4/yii2-import
包简介
Extension for the Yii2 framework for importing Excel data
README 文档
README
Extension for the Yii2 framework for importing data with PhpSpreadsheet.
Installation
The preferred way to install this extension is through composer.
Either run
php composer.phar require --prefer-dist xpbl4/yii2-import "*"
or add
"xpbl4/yii2-import": "*"
to the require section of your composer.json file.
Usage
Implement the ImportInterface:
class Contact extends \yii\db\ActiveRecord implements ImportInterface
{
...
public function import($reader, $row, $data)
{
if ($row == 0) {
if ($data != ['First Name', 'Last Name', 'Email']) {
$reader->addError($row, 'Invalid headers.');
return false;
}
// Skip header row
return true;
}
// Create contact from data
$contact = new Contact;
...
}
}
Now you can import data using ExcelReader and your class as the model:
public function actionImport()
{
$form = new ImportForm();
$form->file = UploadedFile::getInstanceByName('file');
if ($form->validate()) {
Contact::deleteAll();
$reader = new ExcelReader(['model' => Contact::className()]);
$reader->import($form->file->tempName);
if ($reader->getErrors()) {
// Handle errors
...
}
}
return $form;
}
统计信息
- 总下载量: 10
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2023-08-31