illusion 2018-09-03 16:01:34 8759次浏览 6条回复 6 2 0

下载:composer require illusion/yii2-excel 1.2.0 (下面的代码示例,仅仅适用于1.2.0及1.2.0以下版本,若 使用最新版本,请参考https://packagist.org/packages/illusion/yii2-excel) 使用示例:

return [
    'components' => [
        'excel' => [
            'class' => 'illusion\excel\Excel',
        ],
    ],
];

SAMPLE CODE LINK https://github.com/lkq0929/illusion-excel/tree/master/example NOTICE 目前只支持csv、xls、xlsx三种电子表格的导出和导入 SAMPLE CODE

<?php
/**
 * Created by PhpStorm.
 *
 * Auth: lkqlink@163.com
 * Date: 2018/8/27
 * Time: 14:28
 */

namespace app\controllers;


use yii\rest\Controller;
use yii\web\UploadedFile;

class ExampleController extends Controller
{
    /**
     * 单电子表(sheet)、多电子表格导出
     * 导出文件可直接浏览器下载或保存至服务器
     */
    public function actionExport()
    {
        /*单电子表的电子表格--数据格式
        $cellData = [
            ['部门', '组别', '姓名', '性别'],
            ['一米技术部', 'oms', 'illusion', '男'],
            ['一米技术部', 'oms', 'alex', '男'],
            ['一米技术部', 'pms', 'aaron', '女']
        ];*/
        /*多电子表的电子表格--数据格式*/
        $cellData = [
            'one' => [
                ['部门', '组别', '姓名', '性别'],
                ['一米技术部', 'oms', 'illusion', '男'],
                ['一米技术部', 'oms', 'alex', '男'],
                ['一米技术部', 'pms', 'aaron', '女']
            ],
            'two' => [
                ['类别', '名称', '价格'],
                ['文学类', '读者', '¥5'],
                ['科技类', 'AI之人工智能', '¥100'],
                ['科技类', '物联网起源', '¥500']
            ],
        ];
        $spreadsheet    = \Yii::$app->excel->createSpreadSheet('xls'); // 'xls' 自定义电子表格后缀
        $spreadsheet->write($cellData);
        $spreadsheet->download('department'); //'department' 自定义电子表格名
    }
    
    /**
     * 读出电子表格的原生数据并根据自定义属性名装换成对应格式
     * 然后根据你喜欢的方式将数据插入数据表
     *
     * @return array
     */
    public function actionImport()
    {
        $transData  = [];
        $attributes = [
            'department' => ['department', 'group', 'name', 'sex'],  //'department' 工作表(sheet)名  键值:['department', 'group', 'name', 'sex']列对应的名称,名称顺序必须一致
            'book'       => ['category', 'book_name', 'price'],
        ];
        if (\Yii::$app->request->isPost) {
            $importFile = UploadedFile::getInstanceByName('file');
        }
        $fileName    = explode('.', $importFile->name);
        $spreadsheet = \Yii::$app->excel->createSpreadSheet($fileName[1]); // $fileName[1] 自定义电子表格后缀
        $rawDatas    = $spreadsheet->read($importFile->tempName); //$importFile->tempName 电子表(excel)路径
        foreach ($rawDatas as $sheetName => $rawData) {
            $transData[] = $spreadsheet->columnTo($rawData, $attributes[$sheetName]);  //$rawData 读出的单个工作表(sheet)的原生数据,$attributes[$sheetName] $rawData电子表格中列按照顺序对应的自定义列名
        }
        
        return $transData;
    }
    
    /**
     * 读出电子表格的原生数据
     * 根据喜欢的方式插入数据表
     *
     * @return mixed
     */
    public function actionImportRaw()
    {
        if (\Yii::$app->request->isPost) {
            $importFile = UploadedFile::getInstanceByName('file');
        }
        $fileName    = explode('.', $importFile->name);
        $spreadsheet = \Yii::$app->excel->createSpreadSheet($fileName[1]); //同上注释
        $rawDatas    = $spreadsheet->read($importFile->tempName);
        
        return $rawDatas;
    }
}

先来说说写这个组件的初衷,接触yii2不是很久,在项目中刚好有这样一个excel导入导出的需求,筛选了多个组件, 但不是停止维护就是弃用,好不容易找到了一个可用的官方组件 phpoffice/phpspreadsheet,但是有点使用复杂,中文文档很少,但是不可否认这个组件功能很全面,代码质量比之前的phpExcel高出不少,于是就产生了写illusion/yii2-excel 组件的想法,让导入和导出excel这件事情变得更新简单、易于上手,让大家的时间集中在创新和业务上面。最终就在读了 phpoffice/phpspreadsheet 的代码后,基于该组件写出现在的 illusion/yii2-excel 组件,希望大家能喜欢,同时,有什么使用不方便的地方或者代码方面的bug,欢迎大家提出建议或者贡献代码。

觉得很赞
  • 回复于 2020-09-21 15:01 举报

    大佬 下载下来的文件乱码是什么问题

  • 回复于 2019-10-15 16:41 举报

    很不错 简单 好用 但是合并单元格不支持 希望作者继续迭代

    1 条回复
    回复于 2019-10-29 20:00 回复

    好的,最近有点忙,有时间会迭代新版本的

  • 回复于 2019-09-06 14:49 举报
    [
        'one' => [
            ['部门', '组别', '姓名', '性别'],
            ['一米技术部', 'oms', 'illusion', '男'],
            ['一米技术部', 'oms', 'alex', '男'],
            ['一米技术部', 'pms', 'aaron', '女']
        ],
        'two' => [
            ['类别', '名称', '价格'],
            ['文学类', '读者', '¥5'],
            ['科技类', 'AI之人工智能', '¥100'],
            ['科技类', '物联网起源', '¥500']
        ],
    ];
    

    这个从数据库读出要怎样写?

  • 回复于 2019-05-14 11:43 举报

    哪个版本是支持php5.6的呢?

  • 回复于 2019-02-23 10:40 举报

    老哥,代码不错,感谢分享!不过放到linux系统上报错:Worksheet not found。

    看了代码,路径/vendor/illusion/yii2-excel/src/下的文件WorkSheet.php,Sheet首个字符是大写;
    Spreadsheet.php的第52行$this->worksheet = new Worksheet($this->_spreadsheet);Worksheet的sheet首个字符是小写。

    可修改WorkSheet.php 的文件名为 Worksheet.php;或者修改class名和调用的地方解决。

    2 条回复
    回复于 2019-02-28 17:08 回复

    你下载最新版本试试

    回复于 2019-09-21 16:36 回复

    1.2.5版本依然有这个问题

  • 回复于 2018-09-07 11:53 举报

    厉害了

    1 条回复
    回复于 2018-09-07 17:31 回复

    大家相互学习

    觉得很赞
您需要登录后才可以回复。登录 | 立即注册