boundstate/yii2-importexport 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

boundstate/yii2-importexport

最新稳定版本:v0.0.3

Composer 安装命令:

composer require boundstate/yii2-importexport

包简介

Extension for the Yii2 framework for importing and exporting data

README 文档

README

Extension for the Yii2 framework for importing and exporting data with PHPExcel.

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist boundstate/yii2-importexport "*"

or add

"boundstate/yii2-importexport": "*"

to the require section of your composer.json file.

Usage

Implement the ImportInterface and/or ExportInterface:

class Contact extends \yii\db\ActiveRecord implements ImportInterface, ExportInterface
{
    ...

    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;
	    ...
    }

    public function export() {
        return self::find()->asArray()->all();
    }
}

Now you can import data using ExcelReader and your class as the destination:

public function actionImport()
{
    $form = new ImportForm();
    $form->file = UploadedFile::getInstanceByName('file');

    if ($form->validate()) {
        Contact::deleteAll();
        $reader = new ExcelReader(['destination' => Contact::className()]);
        $reader->import($form->file->tempName);
        if ($reader->getErrors()) {
            // Handle errors
            ...
        }
    }
    return $form;
}

Or export data using ExcelWriter and your class as the source:

public function actionExport()
{
	$writer = new ExcelWriter(['source' => Contact::className()]);
    $filename = $writer->write('Excel2007');
    Yii::$app->response->sendFile($filename, 'contacts.xlsx')->send();
}

统计信息

  • 总下载量: 655
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-05-25