/** * 导出excel * @param array $column * eg: $column = [ * 'username' => '姓名', * 'age' => '年龄', * 'sex' => '性别' * ] * @param array $data * eg: $data = [ * ['username' => 'yht', 'age'=>20, 'sex'=>'男'], * ['username' => 'jsh', 'age'=>18, 'sex'=>'女'], * ['username' => 'klerk', 'age'=>25, 'sex'=>'男'], * ] */ public static function exportExcel($column, $data) { $keys = array_keys($column); header("Content-type:application/vnd.ms-excel"); header("Content-Disposition:attachment;filename=export_data.xls"); $html = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40">'; $html .= ' <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'; $html .= ' <html> '; $html .= ' <head> '; $html .= ' <meta http-equiv="Content-type" content="text/html;charset=UTF-8" /> '; $html .= ' <style id="Classeur1_16681_Styles"></style> '; $html .= ' </head> '; $html .= ' <body border=1> '; $html .= ' <div id="Classeur1_16681" align=center x:publishsource="Excel"> '; $html .= ' <table x:str border=1 cellpadding=1 cellspacing=1 width=100% style="border-collapse: collapse">'; $html .= ' <tr>'; foreach($column as $key => $value){ $html .= ' <td>' . Html::encode($value) . '</td>'; } $html .= ' </tr>'; if($data) { foreach ($data as $key => $value) { $html .= ' <tr>'; foreach ($keys as $val){ $html .= ' <td>' . $value[$val] . '</td>'; } $html .= ' </tr>'; } } $html .= ' </table> '; $html .= ' </div> '; $html .= ' </body> '; $html .= ' </html> '; echo $html; }