https://github.com/boundstate/yii2-importexport
https://github.com/boundstate/yii2-importexport
Last synced: about 1 year ago
JSON representation
- Host: GitHub
- URL: https://github.com/boundstate/yii2-importexport
- Owner: boundstate
- Created: 2015-05-25T16:37:40.000Z (about 11 years ago)
- Default Branch: master
- Last Pushed: 2016-08-12T19:35:20.000Z (almost 10 years ago)
- Last Synced: 2025-02-14T17:43:18.762Z (over 1 year ago)
- Language: PHP
- Size: 3.91 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# yii2-importexport
Extension for the Yii2 framework for importing and exporting data with [PHPExcel].
## Installation
The preferred way to install this extension is through [composer](http://getcomposer.org/download/).
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();
}
[PHPExcel]: https://github.com/PHPOffice/PHPExcel