🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
首先使用Composer安装扩展包: ~~~ composer require phpoffice/phpexcel ~~~ ### 导出excel用例一(简版): ~~~ $ToolExcel = new \app\common\tool\Excel(); $list = array( array('id'=>1,'name'=>'a'), array('id'=>2,'name'=>'b'), array('id'=>3,'name'=>'c'), array('id'=>4,'name'=>'c'), ); $key = array( 'id' => '编号', 'name' => '姓名', ); $ToolExcel->exportExcelSimp('简版测试',$key,$list); ~~~ ![](https://box.kancloud.cn/a53e8d95f6968ac4da6f1f5ecfcde521_525x213.png) ### 导出excel用例二(跨行跨列): ~~~ $ToolExcel = new \app\common\tool\Excel(); $data = array( 'file_name' => '测试-跨行跨列', 'sheetInfo' => array( 'sheetIndex' => 0, 'sheetTitle' => '订单汇总', ), 'startCell' => array( //开始写入位置 'row'=>1, 'col'=>1 ), 'cellData' => array( array( array( 'val' => 'A1', 'rowspan' => 2, //跨行数 'colspan' => 2, //跨列数 'remarks' => '备注1', ), array( 'val' => 'B1', // 'rowspan' => 2, //跨行数 'colspan' => 2, //跨行数 ), array( 'val' => 'C1', 'colspan' => 2, ), array( 'val' => 'D1', // 'colspan' => 2, ), array( 'val' => 'A1', 'rowspan' => 2, //跨行数 'colspan' => 2, //跨列数 'remarks' => '备注1', ), array( 'val' => 'C1', 'colspan' => 2, ), array( 'val' => 'D1', // 'colspan' => 2, ), ), array( array( 'val' => '1', ), array( 'val' => '2', ), array( 'val' => '3', ), array( 'val' => '4', ), array( 'val' => '5', ), array( 'val' => '6', ), ), array( array( 'val' => 'a', ), array( 'val' => 'b', // ), ), ), ); $ToolExcel->exportExcel($data); ~~~ ![](https://box.kancloud.cn/31ce5b99bd8f25ce45f108f2736480df_1003x154.png) ### 导出excel用例三(配合model使用): columns配置格式参考table构建器字段配置格式 ~~~ $map = ['id'=>['in',$id]]; model('TestingAnswer')->downloadExcel([ 'title' => '报告', 'data_map' => $map, 'columns' => [ ['ip', 'IP'], ['start_time', '开始时间'], ['get_score', '分数'], ['get_score_proportion', '正确率', ['func'=>'proportionNum'] ], ['end_time', '完成时间'], ['use_second', '用时',['func'=>'format_time']], ['browser_info', '浏览器信息'], ['os_info', '操作系统'], ['project_version', '项目版本号'], ['tags', '标签',], ['answer_status', '状态', ['func'=>'c_name','config_name'=>'answer_status'] ], ], ]); ~~~