19 Star 94 Fork 36

简言/rageframe2

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
helper-excel.md 1.19 KB
一键复制 编辑 原始数据 按行查看 历史

ExcelHelper

目录

  • 前置说明
  • 导出
  • 导入

前置说明

引入

use common\helpers\ExcelHelper;

demo

// [名称, 字段名, 类型, 类型规则]
$header = [
    ['ID', 'id', 'text'],
    ['手机号码', 'mobile'], // 规则不填默认text
    ['openid', 'fans.openid', 'text'],
    ['昵称', 'fans.nickname', 'text'],
    ['关注/扫描', 'type', 'selectd', [1 => '关注', 2 => '扫描']],
    ['性别', 'sex', 'function', function($model){
        return $model['sex'] == 1 ? '男' : '女';
    }],
    ['创建时间', 'created_at', 'date', 'Y-m-d'],
];

$list = [
    [
        'id' => 1,
        'type' => 1,
        'fans' => [
            'openid' => '123',
            'nickname' => '昵称',
        ],
        'sex' => 1,
        'create_at' => time(),
    ]
];

导出

// 简单使用
return ExcelHelper::exportData($list, $header);

// 定制 默认导出xlsx 支持 : xlsx/xls/html/csv
return ExcelHelper::exportData($list, $header, '测试', 'xlsx');

导入

/**
 * 导入
 *
 * @param $filePath 文件路径
 * @param int $startRow 开始行数 默认 1
 * @return array|bool|mixed
 */
$data = ExcelHelper::import($filePath, $startRow);
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
PHP
1
https://gitee.com/jianyan94/rageframe2.git
git@gitee.com:jianyan94/rageframe2.git
jianyan94
rageframe2
rageframe2
master

搜索帮助

Cb406eda 1850385 E526c682 1850385