Question:
How can you improve Php Spreadsheet data retrieval timing from local files?

Problem

I'm using PhpSpreadsheet to read a specific value from a local xlsx file, but it's taking 510ms, which is too slow. I need it to be under 20ms, similar to reading from a CSV file with the same data in a CSV format. Any tips to speed up the code?


<?php

require '/vendor/autoload.php'; // Include PhpSpreadsheet library


use PhpOffice\PhpSpreadsheet\IOFactory;


$xlsxFile = 'local.xlsx'; // path to XLSX file

$targetRowNumber = 302; // row number to access

$targetColumn = 3; // column number (C = 3)


try {

    $trystarttime = microtime(true);

    // Load the XLSX file

    $spreadsheet = IOFactory::load($xlsxFile);


    // Get the value of the specified cell (column 2, row 301)

    $value = $spreadsheet->getActiveSheet()->getCellByColumnAndRow($targetColumn, $targetRowNumber)->getValue();

    

    echo "Value in column $targetColumn, row $targetRowNumber: " . $value . "\n";


    $tryendtime = microtime(true);

    $durationInMilliseconds = (number_format(($tryendtime - $trystarttime) * 1000, 2)) . "ms to fetch the required row value" . PHP_EOL;


    echo $durationInMilliseconds;


} catch (Exception $e) {

    echo "Error: " . $e->getMessage() . "\n";

    echo "Trace: " . $e->getTraceAsString() . "\n";

}

?>


Solutions

If you know the file type you may be able to win some performance by creating (in this case) an XLSX reader directly.



$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();

$spreadsheet = $reader->load($inputFileName);


Answered by: >Jeff

Credit: >StackOverflow


Blog Links

>How to extract tags from TMX (XML) content using PHP

>Php Error Solved- Instance deployment failed to install Composer dependencies

>How to do PHP gd Installation on Ubuntu?

>How to Create an Array based on Two Other Arrays in Php?

>How to do PHP Decryption from Node.js Encryption?

>How to Read a CSV file with PHP using cURL?


Nisha Patel

Nisha Patel

Submit
0 Answers