Phpexcel array output to Excel browser download

Source: Internet
Author: User

MySQL is often found in two-dimensional array, and the array with key is also meaningful, considering often used, it is intended to get a function out, convenient for later use!

Relative is the specification of the array ha, specific to see the $data array;

The first line of the generated Excel is the corresponding key;

Directly on the code:

<?PHP/** * User:klaus * date:2016/5/16 17:04*/Date_default_timezone_set (' PRC ');Header("content-type:text/html; Charset=utf-8 ");include"E:/klaus/work/phpdiscuz/plugin/phpexcel/phpexcel.php";//introduction of Phpexcel$data[] =Array(' uid ' = 1, ' email ' = ' [email protected] ', ' password ' = ' asdsadasd ', ' password1 ' = ' asdsadasd ', ' passwo ' Rd2 ' = ' asdsadasd ');$data[] =Array(' uid ' = 1, ' email ' = ' [email protected] ', ' password ' = ' asdsadasd ', ' password1 ' = ' asdsadasd ', ' passwo ' Rd2 ' = ' asdsadasd ');$data[] =Array(' uid ' = 1, ' email ' = ' [email protected] ', ' password ' = ' asdsadasd ', ' password1 ' = ' asdsadasd ', ' passwo ' Rd2 ' = ' asdsadasd ');$data[] =Array(' uid ' = 1, ' email ' = ' [email protected] ', ' password ' = ' asdsadasd ', ' password1 ' = ' asdsadasd ', ' passwo ' Rd2 ' = ' asdsadasd ');$data[] =Array(' uid ' = 1, ' email ' = ' [email protected] ', ' password ' = ' asdsadasd ', ' password1 ' = ' asdsadasd ', ' passwo ' Rd2 ' = ' asdsadasd ');$data[] =Array(' uid ' = 1, ' email ' = ' [email protected] ', ' password ' = ' asdsadasd ', ' password1 ' = ' asdsadasd ', ' passwo ' Rd2 ' = ' asdsadasd ');$data[] =Array(' uid ' = 1, ' email ' = ' [email protected] ', ' password ' = ' asdsadasd ', ' password1 ' = ' asdsadasd ', ' passwo ' Rd2 ' = ' asdsadasd '); Arrtoexcel ($data, ' Lptest ');functionArrtoexcel ($data,$name){    //process data, get key    $keys=Array_keys($data[0]); $objPHPExcel=NewPhpexcel (); //Modify the sheet name    $objPHPExcel->getactivesheet ()->settitle ($name. ‘_‘ .Date(' Ymd_his ')); //reading an array     for($j= 1;$j<=Count($data);$j++) {         for($k= 1;$k<=Count($data[0]);$k++) {            $colname= Phpexcel_cell::stringfromcolumnindex ($k-1);//start from O            $colname.=$j; if($j= = 1) {                $value=$keys[$k-1]; } Else {                $key=$keys[$k-1]; $value=$data[$j-1][$key]; }            $objPHPExcel->setactivesheetindex (0)                ->setcellvalue ($colname,$value); }    }    //Set column widths    $objPHPExcel->getactivesheet ()->getcolumndimension (' C ')->setwidth (49); //Set Active sheet Index to the first sheet, so Excel opens this as the first sheet    $objPHPExcel->setactivesheetindex (0); //Redirect output to a client ' s Web browser (EXCEL5)    Header(' Content-type:application/vnd.ms-excel '); Header(' Content-disposition:attachment;filename= '.$name. ‘_‘ .Date(' Ymd_his '). '. xls '); Header(' Cache-control:max-age=0 '); $objWriter= Phpexcel_iofactory::createwriter ($objPHPExcel, ' Excel5 '); $objWriter->save (' Php://output ');}

Results

The number of columns is changed, and the number of rows is changed:

have been married

In addition, sheet's name also became Oh!

First time:

Second time:

Done!

Phpexcel array output to Excel browser download

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.