php读取本地xlsx格式文件的数据

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_35713752/article/details/82814985

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

完整示例代码:

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

先下载PHPExcel;

<?php
    header("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);
		}
	}
?>

猜你喜欢

转载自blog.csdn.net/qq_35713752/article/details/82814985
今日推荐