/** * Address: https://github.com/PHPOffice/PHPExcel download Phpexcel, download only the files under the classes directory can be. * Data export * @param array $title header row name * @param array $data Export data * @param string $fileName file name * @param string $savePath save path * @param $type download false--save true--Download * @return string return file full path
*/ functionExportexcel ($title=Array(),$data=Array(),$fileName= ",$sheet= ",$savePath= './',$isDown=false) {Loader:: Import (' Phpexcel. Phpexcel '); Loader:: Import (' Phpexcel. Phpexcel.iofactory '); $obj=New\phpexcel (); //Horizontal Cell Identification $cellName=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 ', ' AA ', ' AB ', ' AC ', ' AD ', ' AE ', ' AF ', ' AG ', ' AH ', ' AI ', ' AJ ', ' AK ', ' AL ', ' AM ', ' an ', ' AO ', ' AP ', ' AQ ', ' AR ', ' as ', ' At ', ' AU ', ' AV ', ' AW ', ' AX ', ' AY ', ' AZ '); $obj->getactivesheet (0)->settitle ($sheet);//Set sheet name $_row= 1;//Set Portrait cell identification if($title){ $_cnt=Count($title); //$obj->getactivesheet (0)->mergecells (' A '. $_row. ': ' $cellName [$_cnt-1].$_row]; Merge cells//$obj->setactivesheetindex (0)->setcellvalue (' A '. $_row, ' data export: '. Date (' y-m-d h:i:s ')); Set the merged cell contents//$_row++; $i= 0; foreach($title as $v){//Set column headings $obj->setactivesheetindex (0)->setcellvalue ($cellName[$i].$_row,$v); $i++; } $_row++; } //fill in the data if($data){ $i= 0; foreach($data as $_v){ $j= 0; foreach($_v as $_cell){ $obj->getactivesheet (0)->setcellvalue ($cellName[$j] . ($i+$_row),$_cell); $j++; } $i++; } } //file name processing if(!$fileName){ $fileName=uniqid( Time(),true); } $objWrite= \phpexcel_iofactory::createwriter ($obj, ' Excel2007 '); if($isDown){//Web Download Header(' Pragma:public '); Header("Content-disposition:attachment;filename=$fileName. xls "); $objWrite->save (' php://output ');Exit; } $_filename=Iconv("Utf-8", "gb2312",$fileName);//transcoding $_savepath=$savePath.$_filename.‘. xlsx; $objWrite->save ($_savepath); return $savePath.$fileName.‘. xlsx; }
/** * Address: Https://github.com/PHPOffice/PHPExcel * Data import * @param string $file Excel File * @param string $ Sheet*/ functionIMPORTEXECL ($file= ",$sheet=0){ $file=Iconv("Utf-8", "gb2312",$file);//transcoding if(Empty($file) OR!file_exists($file)) { die(' File not exists! '); } Loader:: Import (' Phpexcel. Phpexcel '); $objRead=New\phpexcel_reader_excel2007 ();//Create a Reader object Var_dump($objRead); die; if(!$objRead->canread ($file)){ $objRead=New\phpexcel_reader_excel5 (); if(!$objRead->canread ($file)){ die(' No excel! '); } } $cellName=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 ', ' AA ', ' AB ', ' AC ', ' AD ', ' AE ', ' AF ', ' AG ', ' AH ', ' AI ', ' AJ ', ' AK ', ' AL ', ' AM ', ' an ', ' AO ', ' AP ', ' AQ ', ' AR ', ' as ', ' At ', ' AU ', ' AV ', ' AW ', ' AX ', ' AY ', ' AZ '); $obj=$objRead->load ($file);//Create an Excel object $currSheet=$obj->getsheet ($sheet);//gets the specified sheet table $columnH=$currSheet->gethighestcolumn ();//get the maximum number of columns $columnCnt=Array_search($columnH,$cellName); $rowCnt=$currSheet->gethighestrow ();//get total number of rows $data=Array(); for($_row= 1;$_row<=$rowCnt;$_row++){//Read Content for($_column= 0;$_column<=$columnCnt;$_column++){ $cellId=$cellName[$_column].$_row; $cellValue=$currSheet->getcell ($cellId),GetValue (); //$cellValue = $currSheet->getcell ($cellId)->getcalculatedvalue (); #获取公式计算的值 if($cellValueInstanceof Phpexcel_richtext) {//Rich Text conversion string $cellValue=$cellValue-__tostring (); } $data[$_row][$cellName[$_column]] =$cellValue; } } return $data; }
THINKPHP5 using Phpexcel Export to import Excel data