您好,欢迎访问一九零五行业门户网

php 导出数据到excel类

将数据导出到excel当中,欢迎大家拍砖 addheader(array('列1','列2','列3','列4')); * $excel->addbody( array( array('数据1','数据2','数据3','数据4'), array('数据1','数据2','数据3','数据4'), array('数据1','数据2','数据3','数据4'), array('数据1','数据2','数据3','数据4') ) ); * $excel->download(); */class excel{ private $head; private $body; /** * * @param type $arr 一维数组 */ public function addheader($arr){ foreach($arr as $headval){ $headval = $this->charset($headval); $this->head .= {$headval}\t ; } $this->head .= \n; } /** * * @param type $arr 二维数组 */ public function addbody($arr){ foreach($arr as $arrbody){ foreach($arrbody as $bodyval){ $bodyval = $this->charset($bodyval); $this->body .= {$bodyval}\t ; } $this->body .= \n; } } /** * 下载excel文件 */ public function download($filename=''){ if(!$filename) $filename = date('ymdhis',time()).'.xls'; header(content-type:application/vnd.ms-excel); header(content-disposition:attachment;filename=$filename); header(content-type:charset=gb2312); if($this->head) echo $this->head; echo $this->body; } /** * 编码转换 * @param type $string * @return string */ public function charset($string){ return iconv(utf-8, gb2312, $string); }}?>
复制代码
其它类似信息

推荐信息