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.

Installation

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.

<?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
  $reader->setLoadSheetsOnly(['Sheet1']);
  $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 *