当前位置:Gxlcms > PHP教程 > 使用PHPExcel读取Excel文件时会读出乱码

使用PHPExcel读取Excel文件时会读出乱码

时间:2021-07-01 10:21:17 帮助过:3人阅读

这是我读取文件的代码:
		require_once dirname(__FILE__) . "/PHPExcel/Classes/PHPExcel.php";		$PHPReader = new PHPExcel_Reader_Excel2007();		if (!$PHPReader->canRead($filePath)) {			$PHPReader = new PHPExcel_Reader_Excel5();			if (!$PHPReader->canRead($filePath)) {				echo "no file";				die;			}		}		$PHPExcel = $PHPReader->load($filePath);		$sheetCount = $PHPExcel->getSheetCount();		$sheetNames = $PHPExcel->getSheetNames();		for ($SheetID = 0; $SheetID < $sheetCount; $SheetID++) {			$name = $sheetNames[$SheetID];			$currentSheet = $PHPExcel->getSheetByName($name);			$allColumn = $currentSheet->getHighestColumn();			$allRow = $currentSheet->getHighestRow();			$drive = new DriveCommand();			$data = array();			for ($currentRow = 1; $currentRow <= $allRow; $currentRow++) {				for ($currentColumn = 'A'; $currentColumn <= $allColumn; $currentColumn++) {					$val = $currentSheet->getCellByColumnAndRow(ord($currentColumn) - 65, $currentRow)->getValue();					//$objExcel[$name][$currentRow - 1][ord($currentColumn) - 65] = $val;					$data[$currentRow][] = $val;				}			}			if ($name == "产品信息") $name = "product";			else if ($name == "行业") $name = "industry";			else if ($name == "集团信息"){				continue;				 $name = "bloc";				var_dump($data);				die;			}			else if ($name == "客户信息")			{				var_dump($data);				die;				$name = "customer";			}			else if ($name == "业务员信息") $name = "employeer";			else continue;			$drive->actionUpload($name, $data);		}

这是截图:

读取的内容前一部分没有问题,后面的也没有问题,但是中间这部分会变成这个样子。


回复讨论(解决方案)

没有看到乱码
截图中的是你 var_dump 的结果

Excel文件中的数据都是正常的,但是在读取的时候中间会有一部分读取出类似于“ (12) { ["_name":protected]=> string(6) "宋体" ["_size":protected]=> int(12) ["_bold":protected]=> bool(false) ["_italic":protected]=> bool(false) ["_superScript":protected]=> bool(false) ["_subScript":protected]=> bool(false) ["_underline":protected]=> string(4) "none" ["_strikethrough":protected]=> bool(false) ["_color":protected]=> object(PHPExcel_Style_Color)#3006 (4) { ["_argb":protected]=> string(8) "FF000000" ["_parentPropertyName":protected]=> NULL ["_isSupervisor":protected]=> bool(false) ["_parent":protected]=> NULL } ”这样的数据。正常的数据输出应该是 [2]=> array(13) { [0]=> string(6) "B01001" [1]=> string(6) "超市" [2]=> string(12) "百佳超市" [3]=> string(6) "广东" [4]=> string(6) "广州" [5]=> string(21) "广州中华广场店" [6]=> string(51) "广州越秀区中山三路228号中华广场三层" [7]=> NULL [8]=> NULL [9]=> NULL [10]=> NULL [11]=> float(2735) [12]=> string(6) "caoshi" } [3]=> array(13) { [0]=> string(6) "B01002" [1]=> string(6) "超市" [2]=> string(12) "百佳超市" 这种格式。

我也有时候会碰到乱码,有时候又是好的,估计是微软excel问题。

没有看到乱码
截图中的是你 var_dump 的结果


Excel文件中的数据都是正常的,但是在读取的时候中间会有一部分读取出类似于“(12) { ["_name":protected]=> string(6) "宋体" ["_size":protected]=> int(12) ["_bold":protected]=> bool(false) ["_italic":protected]=> bool(false) ["_superScript":protected]=> bool(false) ["_subScript":protected]=> bool(false) ["_underline":protected]=> string(4) "none" ["_strikethrough":protected]=> bool(false) ["_color":protected]=> object(PHPExcel_Style_Color)#3006 (4) { ["_argb":protected]=> string(8) "FF000000" ["_parentPropertyName":protected]=> NULL ["_isSupervisor":protected]=> bool(false) ["_parent":protected]=> NULL } ”这样的数据。正常的数据输出应该是[2]=> array(13) { [0]=> string(6) "B01001" [1]=> string(6) "超市" [2]=> string(12) "百佳超市" [3]=> string(6) "广东" [4]=> string(6) "广州" [5]=> string(21) "广州中华广场店" [6]=> string(51) "广州越秀区中山三路228号中华广场三层" [7]=> NULL [8]=> NULL [9]=> NULL [10]=> NULL [11]=> float(2735) [12]=> string(6) "caoshi" } [3]=> array(13) { [0]=> string(6) "B01002" [1]=> string(6) "超市" [2]=> string(12) "百佳超市" 这种格式。

我也有时候会碰到乱码,有时候又是好的,估计是微软excel问题。


是的,读取的前一部分内容还是正常的,后面也是正常的,但是中间就会有一部分读出像图片里那样。

问题已经解决了。是因为读取单个cell的内容,读取的结果可能是object类型,也可能是string类型,加上一个判断,修改下类型就好了。

人气教程排行