Read Excel Files In PHP Using PHPSpreadSheet

Posted

in

by

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:

  • Composer
  • PHP 7.2 or newer

Step 1.

Setup dependencies.

{
    "require": {
        "phpoffice/phpspreadsheet": "^1.3"
    }
}

Step 2.

Install phpspreadsheet.

$ composer install

Step 3.

Create a new PHP file, and start coding.

<?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.

$ php read-xlsx-files.php

Result.

References:


Posted

in

by

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *

Latest Tutorials

Web Dev Tutorials