/** * 导出报表功能 * @param $dataArr 数据源 (二维结构); * @param $fieldArr 字段信息(一维数组,键值存放$dataArr中存在的key名,对应的value存放要导出表头的字段名称) ; * @param $tname excel表名(默认自动在最后加上导出的时间戳); * @author qingfeng.guo * 2016-06-21 */ function daochu($tname="报名表") { $fieldArr = array( 'id' => 'ID', 'name' => '姓名', 'sex' => '性别', 'nation' => '民族', 'dateint' => '出生日期', 'id_number' => '身份证号', 'kaosheng_number' => '考生号', 'examinee_type' => '考生类型', 'new_year' => '是否应届', 'postal_address' => '通讯地址', 'tel' => '联系电话', 'types_of_graduates' => '毕业生类型', 'addtime' => '报名时间', 'status' => '报名状态', 'major1' => '专业1', 'major2' => '专业2', 'major3' => '专业3', 'major4' => '专业4', 'major5' => '专业5', ); $arr = explode(',',$_GET['id']); $sgin = M('signup'); foreach ($arr as $K=>$v){ $map['id'] = $v; $datas[] = $sgin->where ( $map )->find(); } foreach ($datas as $k=>$v){ if($v['sex'] == 1){ $datas[$k]['sex'] = '男'; }else{ $datas[$k]['sex'] = '女'; } } if(!is_array($fieldArr) || empty($fieldArr)) die("导出异常,请重试!"); if(!empty($dataArr) && count($dataArr) > 10000) die("单次导出的数据量过大(≥10000条),请分批导出!"); $thead = ''; //表头 $tbody = ''; //表数据 $ttail = ''; //表尾 $clos = count($fieldArr); //列数(字段量) //生成表头 $thead .= '<tr>'; foreach($fieldArr as $k => $v) { $thead .= '<td style="color:#fff;background-color:green;">'.$v.'</td>'; } $thead .= '</tr>'; //生成表尾 $ttail = '<tr><td colspan="'.$clos.'"> 报表内容:'.$tname.' , 导出时间:'.date("Y-m-d H:i:s").' </td></tr>'; //生成需要导出的数据源 /*$datas = array(); foreach($dataArr as $k => $v) { $fieldFormat = $fieldArr; //这里需要复制原本的字段格式,以防标题和内容没对应上 if(!empty($v) && is_array($v)) { foreach($v as $k2 => $v2) { //根据key值决定当前字段是否需要导出 if(array_key_exists($k2, $fieldArr)) { $fieldFormat[$k2] = $v2; } } } $datas[] = $fieldFormat; }*/ //生成表体 foreach ($datas as $k => $v) { $tbody .= '<tr>'; foreach($v as $k2 => $v2) { if(!is_numeric($v2)) { $tbody .= '<td style="vnd.ms-excel.numberformat:@">'.$v2.'</td>'; }else{ $tbody .= '<td>'.$v2.'</td>'; } } $tbody .= '</tr>'; } $content = $thead.$tbody.$ttail; header("content-type:application/vnd.ms-excel; charset=utf8"); header("Content-Disposition:attachment;filename={$tname}".time().".xls"); echo "<table align='center' border=1 bgcolor='F8F7ED'>"; if(mb_detect_encoding($content) != 'UTF-8') { echo iconv('UTF-8',"GB2312",$content); }else{ echo $content; } echo "</table>"; }