用PHPExcel类读取excel文件的内容

这里对PHPExcel类不做介绍,有兴趣的朋友可以自己查阅资料

在classes文件夹下有个PHPExcel.php文件,这个文件是这个类库的主要入口文件,在用之前,要引入这个类

其他的类,在此类中会自动加载

 1       //建立reader对象 ,分别用两个不同的类对象读取2007和2003版本的excel文件
 2         $PHPReader = new PHPExcel_Reader_Excel2007();
 3         if( ! $PHPReader->canRead($filePath))
 4         {
 5             $PHPReader = new PHPExcel_Reader_Excel5();
 6             if( ! $PHPReader->canRead($filePath)){
 7                 echo 'no Excel';
 8                 return ;
 9             }
10         }
11 
12         $PHPExcel = $PHPReader->load($filePath); //读取文件
13         $currentSheet = $PHPExcel->getSheet(0); //读取第一个工作簿
14         $allColumn = $currentSheet->getHighestColumn(); // 所有列数
15         $allRow = $currentSheet->getHighestRow(); // 所有行数
16     
17         $data = array(); //下面是读取想要获取的列的内容
18         for ($rowIndex = 2; $rowIndex <= $allRow; $rowIndex++)
19         {
20             $data[] = array(
21                 'id' => $cell = $currentSheet->getCell('A'.$rowIndex)->getValue(),
22                 'score' => $cell = $currentSheet->getCell('H'.$rowIndex)->getValue(),
23                 'ranking' => $cell = $currentSheet->getCell('I'.$rowIndex)->getValue(),
24             );
25         }
原文地址:https://www.cnblogs.com/songlen/p/4468556.html