PHP Export Excel,csv

Source: Internet
Author: User
Tags autoload

functionResult_export ($title,$list,$field) {//Call Php_excelVendor ("Phpexcel.phpexcel"); $objExcel=NewPhpexcel (); $objWriter=NewPhpexcel_writer_excel2007 ($objExcel); $objExcel->setactivesheetindex (0); $objActSheet=$objExcel-Getactivesheet (); $objActSheet->settitle ($title); $letter= ' A '; foreach($field  as $values) {        $objActSheet->setcellvalue ($letter. ' 1 ',$values); ++$letter; }    if(Is_array($list)) {        foreach($list  as $k=$v) {            $letter= ' A '; $k=$k+ 2; Reset($field); foreach($field  as $key=$value) {                $objActSheet->setcellvalueexplicit ($letter.$k,$v[$key], phpexcel_cell_datatype::type_string);//Setcellvalue ($letter. $k, $v [$key])++$letter; }        }    }    $objExcel->setactivesheetindex (0); //If AutoLoad is used in the frame, it will conflict with Phpexcel's autoload and need to be re-registered, such as: Spl_autoload_register ( Array (' Think ', ' autoload '));     Header("Content-type:application/force-download"); Header("Content-type:application/octet-stream"); Header("Content-type:application/download"); Header(' Content-disposition:inline;filename= '. mb_convert_encoding ($title, ' gb2312 ', ' UTF-8 '). '. xlsx '); Header("Content-transfer-encoding:binary"); Header("Last-modified:".)gmdate("D, D M Y h:i:s"). "GMT"); Header("Cache-control:must-revalidate,post-check=0,pre-check=0"); Header("Pragma:no-cache"); $objWriter->save (' Php://output ');}functionResult_export_csv ($title,$list,$field) {    $filename=$title. ". csv"; Header("Content-type:text/csv"); Header("Content-disposition:attachment;filename=".$filename); Header(' Cache-control:must-revalidate,post-check=0,pre-check=0 '); Header(' expires:0 '); Header(' Pragma:public '); $data=implode(‘,‘,$field) .Php_eol; if(Is_array($list)) {        foreach($list  as $k=$v) {            Reset($field); $row= ' '; foreach($field  as $key=$value) {                $row.=$v[$key] . ‘,‘; }            $data.=RTrim($row, ‘,‘) .Php_eol; }    }    Echo $data;}/** * Export data from Database * @param type $head $head = Array (' name ' = ' ' name ', ' gender ' = ' gender ', ' age ' = ' ages ', ' E Mail ' = ' Email ', ' phone ' = ' telephone ', ' ... ' * @param type $list*/functionExportcsv ($head,$list,$title= ' ') {    //$this->outputcsv ($head, $list, date (' Ymdhis ')); Output Excel file header, you can replace user.csv with the file name you want    Header(' Content-type:application/vnd.ms-excel '); Header(' Content-disposition:attachment;filename = "'. ($title?$title:Date(' Ymdhis ')) . '. csv '); Header(' Cache-control:max-age=0 '); $fp=fopen(' Php://output ', ' a '); //output Excel column name information    foreach($head  as $i=$v) {        //CSV Excel support GBK encoding, be sure to convert otherwise garbled        $head[$i] =Iconv(' Utf-8 ', ' GBK ',$v); }    //writes data to a file handle via FputcsvFputcsv ($fp,$head); foreach($list  as $val) {        foreach($head  as $i=$v) {            $row[] = mb_convert_encoding ($val[$i], ' GBK ', ' utf-8 '); } fputcsv ($fp,$row); }    fclose($fp);}

PHP Export Excel,csv

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.