导出xlsx,xls,csv数据

浏览:109 发布日期:2019/07/24 分类:功能实现
导出xlsx,xls,csv数据
public static function GetExcelData($filename='1.xlsx',$startLine=4,$width=['nickname'=>'B','p_nickname'=>'C','phone'=>'D','type'=>'A']){
$extension = strtolower(pathinfo($filename, PATHINFO_EXTENSION) );
switch ($extension){
case 'xlsx':
$objReader = new \PHPExcel_Reader_Excel2007();
$objExcel = $objReader ->load($filename);
break;
case 'xls':
$objReader = new \PHPExcel_Reader_Excel5();
$objExcel = $objReader ->load($filename);
break;
case 'csv':
$PHPReader = new \PHPExcel_Reader_CSV();
//默认输入字符集
$PHPReader->setInputEncoding('GBK');
//默认的分隔符
$PHPReader->setDelimiter(',');
//载入文件
$objExcel = $PHPReader->load($filename);
break;
}
$highestRow = $objExcel ->getSheet(0)->getHighestRow(); // 取得总行数
$getvalue=$objExcel->getActiveSheet();
$data=[];
for($j=$startLine;$j<=(int)$highestRow;$j++){
$value=[];
foreach ($width as $key=>$val){
if($v=$getvalue->getCell($val.$j)->getValue()) $value[$key]=$v;
else $value[$key]='';
}
if($value) $data[]=$value;
}
return $data;
}
开源地址:http://github.crmeb.net/u/lsq
评论( 相关
后面还有条评论,点击查看>>