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

时间:2021-09-02 06:07:29

I want to read data from an Excel file in PHP so that I can process the data and insert it into a DB.
Looking around SO, it looks like PHPExcel is the premier library for this task.

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

I went to the PHPExcel GitHub page (https://github.com/PHPOffice/PHPExcel), but I cannot figure out how to actually use the library. There are a ton of example files and none of the ones I looked at seem to match the simple use case I'm looking for.
Furthermore, I cannot even figure out which files from the GitHub page I even need to download and what the folder structure for the include file(s) needs to be.

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

As such, given the way the GitHub page linked above is structured now (for v1.8), what files do I need to download and what is a simple code example that allows me to provide a path to an Excel file and read the data from the file?

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

1 个解决方案

#1


12  

Mark Baker was extremely helpful in guiding me to the right answer. I don't use Composer with PHP (I should probably learn), but given that, in order to get this to work I went to the GitHub page for PHPExcel (https://github.com/PHPOffice/PHPExcel), clicked the green Clone and download button, and then the Download ZIP link.

马克贝克在指导我找到正确答案方面非常有帮助。我不使用Composer with PHP(我应该学习),但鉴于此,为了使其工作,我去了PHPExcel的GitHub页面(https://github.com/PHPOffice/PHPExcel),点击了绿色克隆和下载按钮,然后是下载ZIP链接。

After unzipping the file, I got a folder called PHPExcel-1.8. I moved that folder to the same folder as both the Excel file I wanted to read (in my code below test.xlsx) and the PHP file that has the code below.

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

The key to getting it to work was inputting the correct path to the IOFactory.php file. It may seem simple to some, but it was tripping me up.

让它工作的关键是输入IOFactory.php文件的正确路径。对某些人来说这似乎很简单,但它让我感到沮丧。

Given the above and Mark Baker's comments, the following code worked perfectly for me (note the commented parts):

鉴于以上和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>';
  }

#1


12  

Mark Baker was extremely helpful in guiding me to the right answer. I don't use Composer with PHP (I should probably learn), but given that, in order to get this to work I went to the GitHub page for PHPExcel (https://github.com/PHPOffice/PHPExcel), clicked the green Clone and download button, and then the Download ZIP link.

马克贝克在指导我找到正确答案方面非常有帮助。我不使用Composer with PHP(我应该学习),但鉴于此,为了使其工作,我去了PHPExcel的GitHub页面(https://github.com/PHPOffice/PHPExcel),点击了绿色克隆和下载按钮,然后是下载ZIP链接。

After unzipping the file, I got a folder called PHPExcel-1.8. I moved that folder to the same folder as both the Excel file I wanted to read (in my code below test.xlsx) and the PHP file that has the code below.

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

The key to getting it to work was inputting the correct path to the IOFactory.php file. It may seem simple to some, but it was tripping me up.

让它工作的关键是输入IOFactory.php文件的正确路径。对某些人来说这似乎很简单,但它让我感到沮丧。

Given the above and Mark Baker's comments, the following code worked perfectly for me (note the commented parts):

鉴于以上和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>';
  }