PHP Export Data to Excel

Source: Internet
Author: User

<?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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.