PHP Export Excel wrapper class
1: Need Phpexcel support
2: Export can automatically switch workbook, default one workbook 2000 lines, test found that 5,000 or so data export may fail and the export time is longer,
Switching the workbook improves the success rate and makes it easy to read
3: Export needs to pass the necessary parameters, the return value is saved after the Excel address, the link can be downloaded
1<?PHP2 /**3 * Public export class @author:zhaoyaei4 * $column: First row header of the export5 * $result: Result sets that need to be exported6 * $path: Save path to export file7 * $sheet _num: Maximum number of rows per workbook (optional)8 * @return: The saved address9 */Ten classexport{ One A Public function__construct () { - //initializing classes, introducing related class files - include"Phpexcel.php"; the include"Phpexcel/iofactory.php"; - } - - //Export Settings + Public functionCreatexcel ($column,$result,$path,$sheet _num=NULL){ - //Check the legality of the data + if(Empty($column) ||Empty($path) ||$path== "" ||Empty($result)){ A return false; at } - - //If the data is large, the export is thinned out - if(Empty($sheet _num) ||$sheet _num<= 0){ - $sheet _num= 2000; - } in - $path=$this->check_encod ("GBK",$path); to //Creating an Phpexcel instance + $objPHPExcel=New\phpexcel (); - //total number of data rows and data columns the $arr _num=Count($result); * $field _count=Count($column); $ //Number of working thin, too many working thin, can adjust the number of data strips exported by the coal industry reduce the number of working thinPanax Notoginseng $get _num=Ceil($arr _num/$sheet _num); - if($get _num> 20){ the return false; + } A the //Generate column Information + $ary=Array("", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X "," Y "," Z "); - for($i= 1;$i<=$field _count;$i++){ $ $ary _info[$i] =$ary[$i/27].$ary[$i%27]; $ $fieldwidth[$i-1] = 20;//Set column width default is - } - the - //Cycle each workbookWuyi for($sheet= 0;$sheet<$get _num;$sheet++){ the //Create a workbook, set the start workbook - $objPHPExcel->createsheet ($sheet); Wu $Sheet=$objPHPExcel->setactivesheetindex ($sheet); - //set the workbook style About $i= 1; $ foreach($column as $key=$value){ - //set the value of the first row - $Sheet->setcellvalue ($ary _info[$i] .‘ 1 ',$value); - //set the first row to bold A $objPHPExcel->getactivesheet ()->getstyle ($ary _info[$i] .‘ 1 ')->getfont ()->setbold (true); + //Set line width the $objPHPExcel->getactivesheet ()->getcolumndimension ($ary _info[$i])->setwidth ($fieldwidth[$i-1]); - $i=$i+ 1; $ } the the //number of lines to start per workbook the $hang _num= 2; the //This workbook ends with a number of lines starting at 0 - $end _num= 0; in //start_num start line number of this workbook the $start _num=$sheet*$sheet _num; the //calculates the number of start and end lines per export (multi-sheet export) About if($arr _num> ($start _num+$sheet _num)){ the $end _num=$start _num+$sheet _num; the}Else{ the $end _num=$arr _num; + } - the //number of loop linesBayi for($i=$start _num;$i<$end _num;$i++){ the //number of circular columns the for($j= 1;$j<=$field _count;$j++){ - //Write Data - $Sheet->setcellvalue ($ary _info[$j]. ($hang _num)," ".$result[$i][$j]); the } the $hang _num++; the } the //set the name of the sheet - $objPHPExcel->getactivesheet ($sheet)->settitle (' sheet ').$sheet,$sheet); the //set the starting position of the sheet the $objPHPExcel->setactivesheetindex ($sheet); the }94 the //write the above data by Phpexcel_iofactory's write function the $objWriter= \phpexcel_iofactory::createwriter ($objPHPExcel, ' Excel5 '); the //Setting the time zone98Date_default_timezone_set ("Asia/shanghai"); About //save and return to save path - $objWriter->save ($path);101 return $this->check_encod ("Utf-8",$path);102 }103 104 Public functionCheck_encod ($encod,$string){ the //Determine character encoding106 $encode= Mb_detect_encoding ($string,Array("ASCII", "UTF-8", "GB2312", "GBK", "BIG5"));107 if($encode!=$encod){108 $string=Iconv($encode,$encod,$string);109 } the return $string;111 } the }113?>
PHP Export Excel wrapper class