DolphinPHP实现导入导出excle表格功能

实现步骤如下: 
    1.下载excel插件: http://yxcs.dolphinphp.com/store.html
               2.将插件解压到plugins文件夹下
               3.实现导出功能:
               4.实现导入数据功能:
 
    实现导出功能:
    
    
    
        $data = Db::name('py')->select(); //查询数据
        $name='评语表'.time();
        // 设置表头信息(对应字段名,宽度,显示表头名称)
        $cellName = [
            ['id', 'auto', 'ID'],
            ['py', 'auto', '评语'],
            ['type', 'auto', '评语类别'],
            ['lv', 'auto', '评语等级'],
            ['addtime', 'auto', '添加时间'],
           
        ];
        // 调用插件(传入插件名,[导出文件名、表头信息、具体数据])
        plugin_action('Excel/Excel/export', [$name, $cellName, $data]);

    实现导入功能:

    

      // 提交数据
        if ($this->request->isPost()) {
            // 接收附件 ID
            $excel_file = $this->request->post('excel');
            // 获取附件 ID 完整路径
            $full_path = getcwd() . get_file_path($excel_file);
            // 只导入的字段列表
            $fields = [
                'id'=>'ID',
                'py' => '评语',
                'type' => '评语类别',
                'lv' => '评语等级',
                'addtime'=>'添加时间'
            ];
            // 调用插件('插件',[路径,导入表名,字段限制,类型,条件,重复数据检测字段])
            $import = plugin_action('Excel/Excel/import', [$full_path, 'py', $fields, $type = 0, $where = null, $main_field = 'id']);
            
            // 失败或无数据导入
            if ($import['error']){
                $this->error($import['message']);
            }
            // 导入成功
            $this->success($import['message']);
        }


        // 创建演示用表单
        return ZBuilder::make('form')
            ->setPageTitle('导入Excel')
            ->addFormItems([ // 添加上传 Excel
                ['file', 'excel', '上传文件'],
            ])
            ->fetch();

猜你喜欢

转载自www.cnblogs.com/cyk2/p/12460762.html