<?PHP/** * Export to Excel file (generally exported in Chinese will be garbled, need to encode conversion) * Use the following method * $excel = new Excel (); * $excel->addheader (Array (' column 1 ', ' column 2 ', ' column 3 ', ' column 4 ‘)); * $excel->addbody (Array (' Data 1 ', ' Data 2 ', ' Data 3 ', ' Data 4 '), Array (' Data 1 ', ' Data 2 ', ' number According to 3 ', ' Data 4 '), Array (' Data 1 ', ' Data 2 ', ' Data 3 ', ' Data 4 '), Array (' Data 1 ', ' Data 2 ', ' Data 3 ', ' Data 4 ')) ); * $excel->download (); */classexcel{Private $head; Private $body; /** * * @param type $arr one-dimensional array*/ Public functionAddHeader ($arr){ foreach($arr as $headVal){ $headVal=$this->charset ($headVal); $this->head. = "{$headVal}\t "; } $this->head. = "\ n"; } /** * * @param type $arr two-dimensional array*/ Public functionAddbody ($arr){ foreach($arr as $arrBody){ foreach($arrBody as $bodyVal){ $bodyVal=$this->charset ($bodyVal); $this->body. = "{$bodyVal}\t "; } $this->body. = "\ n"; } } /** * Download Excel file*/ Public functionDownLoad ($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; } /** * Code conversion * @param type $string * @return string*/ Public functionCharSet$string){ return Iconv("Utf-8", "gb2312",$string); }}?>
PHP Export Data to Excel