PHP--EXCEL操作(实用最简洁)
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2020/3/12 * Time: 17:13 */ function exportExcel($data, $fields = [], $titles = [], $formater = [], $table_name = '表格'){ $table = ''; if(!empty($data[0])) { if(empty($fields) || empty($titles)){ $fields = $titles = array_keys($data[0]); } $table .= "<table> <thead> <tr>"; foreach ($titles as $value) { $table .= "<th class='name'>$value</th>"; } $table .= "</tr> </thead> <tbody>"; foreach ($data as $v) { $table .= "<tr>"; foreach ($fields as $field) { if(!empty($formater[$field])){ //替换别名 $v_value = $formater[$field][$v[$field]]; }else{ $v_value = $v[$field]; } $table .= "<td class='name'>{$v_value}</td>"; } $table .= "</tr>"; } $table .= "</tbody> </table>"; } header("Pragma: public"); header("Expires: 0"); header("Cache-Control:must-revalidate, post-check=0, pre-check=0"); header("Content-Type:application/octet-stream"); header('Content-Disposition:attachment;filename="'.$table_name.'.xlsx"'); header("Content-Transfer-Encoding:binary"); echo $table; exit; } $arr = [ [ 'project_id'=>1, 'name'=>'测试吴俊峰', 'desc'=>'[{"module_title":"我问问方式","module_content":"<p style=\"color:#999\">沙发斯蒂芬斯蒂芬</p>"}]', 'publish_time'=>'2019-11-07 00:00:00', 'username'=>'吴俊峰', 'business_type'=>'1' ], [ 'project_id'=>2, 'name'=>'测试刘俊杰', 'desc'=>'[{"module_title":"我问问方式","module_content":"<p style=\"color:#999\">沙发斯蒂芬斯蒂芬</p>"}]', 'publish_time'=>'2019-11-07 00:00:00', 'username'=>'刘俊杰', 'business_type'=>'2' ], [ 'project_id'=>3, 'name'=>'测试陈议案', 'desc'=>'[{"module_title":"我问问方式","module_content":"<p style=\"color:#999\">沙发斯蒂芬斯蒂芬</p>"}]', 'publish_time'=>'2019-11-07 00:00:00', 'username'=>'陈议案', 'business_type'=>'1' ], [ 'project_id'=>4, 'name'=>'测试刘美丽', 'desc'=>'[{"module_title":"我问问方式","module_content":"<p style=\"color:#999\">沙发斯蒂芬斯蒂芬</p>"}]', 'publish_time'=>'2019-11-07 00:00:00', 'username'=>'刘美丽', 'business_type'=>'1' ], [ 'project_id'=>5, 'name'=>'测试张大牛', 'desc'=>'[{"module_title":"我问问方式","module_content":"<p style=\"color:#999\">沙发斯蒂芬斯蒂芬</p>"}]', 'publish_time'=>'2019-11-07 00:00:00', 'username'=>'张大牛', 'business_type'=>'2' ], ]; $fields = ['project_id','name','desc','publish_time','username','business_type']; $titles = ['项目ID','项目名称','详细描述','出版时间','用户名','业务类型']; $formater = [ 'business_type' =>[ 1=>'场景秀', 2=>'个性化主题', ] ]; exportExcel($arr, $fields, $titles, $formater, '项目管理列表');
导出结果
龙卷风之殇