如何使用 PHPExcel 从 Excel 文件中读取数据?

How do I use PHPExcel to read data from an Excel file?

我想从 PHP 中的 Excel 文件中读取数据,以便我可以处理数据并将其插入数据库。
环顾四周,看起来 PHPExcel 是这项任务的首要图书馆。

我去了 PHPExcel GitHub 页面 (https://github.com/PHPOffice/PHPExcel),但我不知道如何实际使用该库。有大量示例文件,none 我查看的文件似乎与我正在寻找的简单用例相匹配。
此外,我什至无法弄清楚 GitHub 页面中的哪些文件我什至需要下载以及包含文件的文件夹结构需要是什么。

因此,考虑到上面链接的 GitHub 页面现在的结构方式(对于 v1.8),我需要下载哪些文件以及允许我提供的简单代码示例是什么Excel 文件的路径并从文件中读取数据?

马克贝克在指导我找到正确答案方面非常有帮助。我没有将 Composer 与 PHP 一起使用(我可能应该学习),但考虑到这一点,为了让它工作,我去了 GitHub 页面以获得 PHPExcel (https://github.com/PHPOffice/PHPExcel),单击绿色的 克隆并下载 按钮,然后单击 下载 ZIP link.

解压缩文件后,我得到一个名为PHPExcel-1.8的文件夹。我将该文件夹移动到与我想阅读的 Excel 文件(在我下面的代码 test.xlsx 中)和包含下面代码的 PHP 文件相同的文件夹中。

让它工作的关键是输入 IOFactory.php 文件的正确路径。对一些人来说这可能看起来很简单,但它让我感到困惑。

鉴于上述内容和 Mark Ba​​ker 的评论,以下代码对我来说非常有效(注意评论部分):

<?php

  //Had to change this path to point to IOFactory.php.
  //Do not change the contents of the PHPExcel-1.8 folder at all.
  include('PHPExcel-1.8/Classes/PHPExcel/IOFactory.php');

  //Use whatever path to an Excel file you need.
  $inputFileName = 'test.xlsx';

  try {
    $inputFileType = PHPExcel_IOFactory::identify($inputFileName);
    $objReader = PHPExcel_IOFactory::createReader($inputFileType);
    $objPHPExcel = $objReader->load($inputFileName);
  } catch (Exception $e) {
    die('Error loading file "' . pathinfo($inputFileName, PATHINFO_BASENAME) . '": ' . 
        $e->getMessage());
  }

  $sheet = $objPHPExcel->getSheet(0);
  $highestRow = $sheet->getHighestRow();
  $highestColumn = $sheet->getHighestColumn();

  for ($row = 1; $row <= $highestRow; $row++) { 
    $rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, 
                                    null, true, false);

    //Prints out data in each row.
    //Replace this with whatever you want to do with the data.
    echo '<pre>';
      print_r($rowData);
    echo '</pre>';
  }