phpexcel读取excel表格文件

//文件类型
$filePath = './xxxx.xlsx';
if(!file_exists($filePath)) exit('文件不存在...');

//引入PHPExcel.php
require_once 'PHPExcel.php';
//建立reader对象
$PHPReader = new PHPExcel_Reader_Excel2007();

//如果excel2007不能识别此文件则用excel5来读取,如果都不能识别 说明文件有问题
if(!$PHPReader->canRead($filePath)) {
$PHPReader = new PHPExcel_Reader_Excel5();
if(!$PHPReader->canRead($filePath)) {
exit('无法识别此文件');
}
}


//读取文件
$excel = $PHPReader->load($filePath);
//获取工作表总数
$sheetCount = $excel->getSheetCount();

//循环每张表的数据 表从0开始
for($a=0;$a<$sheetCount;$a++) {
  //根据索引获取表的数据
$sheet = $excel->getSheet($a);
  //获取总共行数
$maxRows = $sheet->getHighestRow();
  //获取每行的字段数
$maxColumns = $sheet->getHighestColumn();
//字母列转为数字列
$maxColumns = PHPExcel_Cell::columnIndexFromString($maxColumns);
  //数字列转为字母列
  //PHPExcel_Cell::stringFromColumnIndex($i);

//因为第一行有字段名,所以从第二行开始循环
for($r = 2;$r<=$maxRows;$r++) {

//列数是以第0列开始
for($c = 0;$c<=$maxColumns;$c++) {
        //读取字段的数据
echo $sheet->getCellByColumnAndRow($c,$r)->getValue().'<br/>';
}
}
}
原文地址:https://www.cnblogs.com/tudou1223/p/5702881.html