糖尿病康复,内容丰富有趣,生活中的好帮手!
糖尿病康复 > php读取本地xlsx格式文件的数据并按json格式返回

php读取本地xlsx格式文件的数据并按json格式返回

时间:2020-10-13 16:28:30

相关推荐

php读取本地xlsx格式文件的数据并按json格式返回

微信小程序开发交流qq群 173683895

承接微信小程序开发。扫码加微信。

目的:php读取并操作本地xlsx格式的文件;

完整示例代码:

代码讲解:前端发起post网络请求,php接收一个name(姓名)的参数,循环xlsx文件里面的S列(在我的xlsx文件内这一列的数据是姓名) 获取该列的数据循环匹配name, 如果有相同名字就提取该行的几个数据,保存为JSON格式的数组返回给前端。

先下载PHPExcel;

<?phpheader("Content-Type:text/html;charset=utf8"); header("Access-Control-Allow-Origin: *"); //解决跨域header('Access-Control-Allow-Methods:POST');// 响应类型 header('Access-Control-Allow-Headers:*'); // 响应头设置 $val = $_POST['name'];set_time_limit(0);error_reporting(0);require_once dirname(__FILE__) . '/PHPExcel/PHPExcel.php';require_once dirname(__FILE__) . '/PHPExcel/PHPExcel/IOFactory.php';$path = "./rongyux.xlsx";$fileType = PHPExcel_IOFactory::identify($path);$objReader = PHPExcel_IOFactory::createReader($fileType);$objPHPExcel = $objReader->load($path);$currentSheet = $objPHPExcel->getSheet(0);//for ($i = 0; $i <= 11224; $i++){for ($i = 0; $i <= 11; $i++){$ss = (string) ($currentSheet->getCell('S'.$i)->getValue());if($ss==$val){$pp = (string) ($currentSheet->getCell('P'.$i)->getValue());$hh = (string) ($currentSheet->getCell('H'.$i)->getValue());$ii = (string) ($currentSheet->getCell('I'.$i)->getValue());$al = (string) ($currentSheet->getCell('AL'.$i)->getValue());$an = (string) ($currentSheet->getCell('AN'.$i)->getValue());$be = (string) ($currentSheet->getCell('BE'.$i)->getValue());$bg = (string) ($currentSheet->getCell('BG'.$i)->getValue());$bf = (string) ($currentSheet->getCell('BF'.$i)->getValue());$resultList = array();$newItem = array("ss"=>$ss,"hh"=>$hh,"ii"=>$ii,"pp"=>$pp,"al"=>$al,"an"=>$an,"be"=>$be,"bg"=>$bg,"bf"=>$bf);$resultList[] = $newItem;echo json_encode($resultList);}}?>

如果觉得《php读取本地xlsx格式文件的数据并按json格式返回》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。