Read large Excel(.xls or .xlsx) or CSV files with php libraries

I wrote a function which reads large Excel(.xls or .xlsx) or CSV files with php libraries. The function uses following libraries.

phpoffice/phpspreadsheet 1.21.0
box/spout v3.3.0

1. Procedure to install php libraries

1.1. Install Composer (a tool for dependency management in php)

$ mkdir composer
$ cd composer/
$ curl -sS https://getcomposer.org/installer | php
$ sudo mv composer.phar /usr/local/bin/composer
$ sudo chmod +x /usr/local/bin/composer

1.2. Install PhpSpreadsheet (a library for reading and writing spreadsheets such as xlsx, xls, ods and csv)

$ composer require phpoffice/phpspreadsheet

1.3. Install Spout (a library for reading and writing 3 types of spreadsheets xlsx, ods and csv)

$ composer require "box/spout"

2. A function which reads large Excel(.xls or .xlsx) or CSV files

The function below reads spreadsheet data from large Excel(.xls or .xlsx) or CSV files and store them in a 2 dimensional array. It takes following 2 arguments.

$targetFileName an input file name with full path
$fileType a file extension of an input file
require_once('/path-to-composer/composer/vendor/autoload.php');

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Reader\Csv as CsvReader;
use PhpOffice\PhpSpreadsheet\Reader\Xls as XlsReader;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx as XlsxReader;

use Box\Spout\Reader\Common\Creator\ReaderEntityFactory as SpoutReaderEntityFactory;

function getExcelSheetData($targetFileName, $fileType) {

    $sheetData = "";

    if ($fileType == "csv" || $fileType == "xls") {
        $sheetData = getExcelSheetDataWithPhpSpreadsheet($targetFileName, $fileType);
    } else if ($fileType == "xlsx") {
        $sheetData = getExcelSheetDataWithSpout($targetFileName, $fileType);
    } else {
        // Error Handling
    }

    return $sheetData;
}


function getExcelSheetDataWithPhpSpreadsheet($targetFileName, $fileType) {

    $reader = null;

    if ($fileType == "csv") {
        $reader = new CsvReader();
    } else if ($fileType == "xls") {
        $reader = new XlsReader();
    } else {
        // Error Handling
    }

    $reader->setReadDataOnly(TRUE);

    try {
        $spreadSheet = $reader->load($targetFileName);
    } catch (Error | Exception $ex) {
        // Error Handling
    }

    // get data from active sheet
    $sheet = $spreadSheet->getActiveSheet();

    $row = 1;
    foreach ($sheet->getRowIterator() as $eachRow) {
        foreach($sheet->getColumnIterator() as $column) {
            $sheetData[$row - 1][]
                = $sheet->getCell($column->getColumnIndex() . $row)->getValue();
        }
        $row++;
    }

    $spreadSheet->disconnectWorksheets();
    return $sheetData;
}


function getExcelSheetDataWithSpout($targetFileName, $fileType) {

    if ($fileType != "xlsx") {
        // Error Handling
    }

    $reader = SpoutReaderEntityFactory::createXLSXReader();

    try {
        $reader->open($targetFileName);
    } catch (Error | Exception $ex) {
        // Error Handling
    }

    // read data from "active" sheet
    foreach ($reader->getSheetIterator() as $sheet) {
        if ($sheet->isActive()) {
            $row = 1;
            foreach ($sheet->getRowIterator() as $eachRow) {
                $column = 1;
                foreach ($eachRow->getCells() as $cell) {
                    $sheetData[$row - 1][] = $cell->getValue();
                    $column++;
                }
                $row++;
            }
            break; // no need to read more sheets
        }
    }

    $reader->close();
    return $sheetData;
}

Leave a Reply

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

CAPTCHA