Read data from specific tabs in excel sheet using PHP

Read data from specific tabs in excel sheet using PHP

PHPExcel was officially deprecated in 2017. The latest version of PHPExcel is PhpSpreadsheet. If there are many tabs in one excel sheet it takes much time to read data using PHP. To reduce the time phpspreadsheet have a special method to read only specific tabs.


Use composer to install PhpSpreadsheet into your project:

composer require phpoffice/phpspreadsheet

Or also download the documentation and samples if you plan to use them:

composer require phpoffice/phpspreadsheet --prefer-source

After downloading copy the PHP spreadsheet mater folder in your working directory and include the dependency in your php file. And use the below code for reading excel specific tab data in PHP.


require __DIR__ . '/vendor/autoload.php';
$path = 'demo.xlsx';

# Read the same content in  Xlsx formats, showing discrepancy with getPrintArea()

  $reader = PhpOffice\PhpSpreadsheet\IOFactory::createReaderForFile($path);

  # load only sheets that name meet our criteria
  $workbook = $reader->load($path);

  # display print areas
  foreach ($workbook->getWorksheetIterator() as $index => $sheet) {
    echo get_class($reader)." > {$sheet->getTitle()} print area: {$sheet->getPageSetup()->getPrintArea()}\n";

Leave a Reply

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