Export data from php to excel-PHP source code

Source: Internet
Author: User
Export data from php to excel to export data to 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', 'data 3', 'data 4'), array ('data 1', 'data 2', 'data 3', 'data 4 '), array ('data 1', 'data 2', 'data 3', 'data 4'); * $ excel-> downLoad (); */class Excel {private $ head; private $ body;/***** @ param type $ arr one-dimensional array */public function addHeader ($ arr) {foreach ($ arr as $ headVal) {$ headVal = $ this-> charset ($ headVal); $ this-> head. = "{$ headVal} \ t";} $ this-> head. = "\ n";}/***** @ param type $ arr two-dimensional array */public function addBody ($ arr) {foreach ($ arr as $ arrBody) {foreach ($ arrBody as $ bodyVal) {$ bodyVal = $ this-> charset ($ bodyVal ); // filter Special characters $ bodyVal = str_replace (array ('\ n',' \ t ','
','
','
','
'), "", $ BodyVal); $ bodyVal = str_replace (array ("rn", "r", "n"), "", $ bodyVal ); $ bodyVal = preg_replace ("{\ t}", "", $ bodyVal); $ bodyVal = preg_replace ("{\ r \ n}", "", $ bodyVal ); $ bodyVal = preg_replace ("{\ r}", "", $ bodyVal); $ bodyVal = preg_replace ("{\ n}", "", $ bodyVal ); $ bodyVal = str_replace (",", "", $ bodyVal); $ this-> body. = "{$ bodyVal} \ t";} $ this-> body. = "\ n" ;}}/ *** downLoad an excel file */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 ;} /*** encoding conversion * @ param type $ string * @ return string */public function charset ($ string) {return iconv ("UTF-8", "gb2312 ", $ string) ;}}?>

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.