Box\Spout\Reader\XLSX\Helper\SheetHelper::getSheets PHP Method

getSheets() public method

The paths to the sheets' data are read from the [Content_Types].xml file.
public getSheets ( ) : Sheet[]
return Box\Spout\Reader\XLSX\Sheet[] Sheets within the XLSX file
    public function getSheets()
    {
        $sheets = [];
        $sheetIndex = 0;
        $xmlReader = new XMLReader();
        if ($xmlReader->openFileInZip($this->filePath, self::WORKBOOK_XML_FILE_PATH)) {
            while ($xmlReader->read()) {
                if ($xmlReader->isPositionedOnStartingNode('sheet')) {
                    $sheets[] = $this->getSheetFromSheetXMLNode($xmlReader, $sheetIndex);
                    $sheetIndex++;
                } else {
                    if ($xmlReader->isPositionedOnEndingNode('sheets')) {
                        // stop reading once all sheets have been read
                        break;
                    }
                }
            }
            $xmlReader->close();
        }
        return $sheets;
    }

Usage Example

Example #1
0
 /**
  * @param string $filePath Path of the file to be read
  * @param \Box\Spout\Reader\XLSX\ReaderOptions $options Reader's current options
  * @param \Box\Spout\Reader\XLSX\Helper\SharedStringsHelper $sharedStringsHelper
  * @param \Box\Spout\Common\Helper\GlobalFunctionsHelper $globalFunctionsHelper
  * @throws \Box\Spout\Reader\Exception\NoSheetsFoundException If there are no sheets in the file
  */
 public function __construct($filePath, $options, $sharedStringsHelper, $globalFunctionsHelper)
 {
     // Fetch all available sheets
     $sheetHelper = new SheetHelper($filePath, $options, $sharedStringsHelper, $globalFunctionsHelper);
     $this->sheets = $sheetHelper->getSheets();
     if (count($this->sheets) === 0) {
         throw new NoSheetsFoundException('The file must contain at least one sheet.');
     }
 }