参数 文件的属性 这里的$info 是利用tp5文件上传获取的
protected function excelPort($info){
//获取后缀
$type = $info->getExtension();//CSV //格式
$path = ROOT_PATH . 'public' . DS . 'uploads'.DS.$info->getSaveName();//文件路径
require_once ROOT_PATH."vendor/PHPExcel/PHPExcel/IOFactory.php";
require_once ROOT_PATH."vendor/PHPExcel/PHPExcel/Cell.php";
// 判断使用哪一种格式
$objReader = \PHPExcel_IOFactory::createReader($type)
->setDelimiter(',')
->setInputEncoding('GBK') //不设置将导致中文列内容返回boolean(false)或乱码
->setEnclosure('"')
->setSheetIndex(0);
$objPHPExcel = $objReader->load($path);
$sheet = $objPHPExcel->getSheet(0);
// 取得总行数
$highestRow = $sheet->getHighestRow();
// 取得总列数
$highestColumn = $sheet->getHighestColumn();
//循环读取excel文件,读取一条,插入一条
$data=array();
//从第一行开始读取数据
for($j=1;$j<=$highestRow;$j++){
for($k='A';$k<=$highestColumn;$k++){
//数据坐标
$address=$k.$j;
$data[$j][]=$objPHPExcel->getActiveSheet()->getCell($address)->getValue();
$data[$j][]=$objPHPExcel->getActiveSheet()->getCell("$k$j")->getValue();
}
}
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
//遍历工作表
foreach ($worksheet->getRowIterator() as $key=>$row) {
$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells( false);
foreach ($cellIterator as $cell) {
$rows[$key][]= $cell->getCalculatedValue();
}
}
}
halt($rows);die;
}
亲测可用
|