最近用到php導(dǎo)出excel的例子;
我先貼出來,后面慢慢解釋:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | private function excel($data){ header("Content-Type: application/vnd.ms-excel"); header("Content-Disposition: attachment; filename=list.xls"); // 表名 header("Pragma: no-cache"); // 緩存 header("Expires: 0"); $sepcol = " \t "; // 分列; $sepbr = "\n"; // 分行 foreach ($data as $key) { foreach ($key as $value) { // 一行 echo mb_convert_encoding($value,"GBK","utf-8").$sepcol; // 分列 } echo $sepbr;//分行 } exit; } |
上面看到的$data是個二維數(shù)組,意思,你從數(shù)據(jù)庫選出來是什么樣式,導(dǎo)出的excel就是什么樣式,下面是我模擬的二維數(shù)組,調(diào)用上面的方法;
1 2 3 4 5 6 7 8 9 10 11 | private function excelout(){ $info = array(); $info[0] = array('問卷名稱:',$title['sftitle']); $info[1] = array('問卷內(nèi)容:'); $info[2] = array('工單號','受理人','工作內(nèi)容','評價人','評價'); //$info = array_merge($info,$result); //var_dump($info);exit; $this->excel($info); } |