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