SpreadSheet-Coding.com

PhpSpreadsheet

Read Excel Files In PHP Using PHPSpreadSheet

June 21, 2020

Using the generated xlsx file from the tutorial Create Xlsx Files as a sample file, learn how to read xlsx files using PhpSpreadsheet in this tutorial.

Requirements:

Step 1.

Setup dependencies.

composer.json
{
    "require": {
        "phpoffice/phpspreadsheet": "^1.3"
    }
}

Step 2.

Install phpspreadsheet.

command line
$ composer install

Step 3.

Create a new PHP file, and start coding.

read-xlsx-files.php
<?php

// Autoload dependencies
require 'vendor/autoload.php';

// Import the Xlsx reader class
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;

// Full path of the file to read
$inputFileName = 'create-xlsx-files.xlsx';

// Create the reader object
$reader = new Xlsx();

// Load the file to read
$spreadsheet = $reader->load($inputFileName);

// Get the active sheet
$sheet = $spreadsheet->getActiveSheet();

/**
 * Get the value of column A, row 1 using the
 * combination of the column letter and row number.
 */
echo $sheet->getCell('A1')->getValue();

echo PHP_EOL;

/**
 * Get the value of column A, row 1 using the
 * combination of the column number and row number.
 */
echo $sheet->getCellByColumnAndRow('1', '1')->getValue();

Test.

Command line testing.

command line
$ php read-xlsx-files.php

Result.

References: