当前位置:Gxlcms > PHP教程 > PHPExcel读excel

PHPExcel读excel

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

require_once './library/excel/PHPExcel.php';

//要读的文件
$filePath = 'test.xlsx';

$PHPExcel = new PHPExcel();

/**默认用excel2007读取excel,若格式不对,则用之前的版本进行读取*/
$PHPReader = new PHPExcel_Reader_Excel2007();
if(!$PHPReader->canRead($filePath))
{
$PHPReader = new PHPExcel_Reader_Excel5();
if(!$PHPReader->canRead($filePath))
{
echo 'no Excel';
return ;
}
}

$PHPExcel = $PHPReader->load($filePath);
/**读取excel文件中的第一个工作表*/
$currentSheet = $PHPExcel->getSheet(0);
/**取得最大的列号*/
$allColumn = $currentSheet->getHighestColumn();
/**取得一共有多少行*/
$allRow = $currentSheet->getHighestRow();
/**从第二行开始输出,因为excel表中第一行为列名*/
for($currentRow = 1;$currentRow <= $allRow;$currentRow++)
{
/**从第A列开始输出*/
for($currentColumn= 'A';$currentColumn<= $allColumn; $currentColumn++)
{
$val = $currentSheet->getCellByColumnAndRow(ord($currentColumn) - 65,$currentRow)->getValue();/**ord()将字符转为十进制数*/
echo $val."\t";
}
echo "
";
}

?>

以上就介绍了PHPExcel 读excel,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

人气教程排行