You are here

public function PHPExcel_Reader_OOCalc::listWorksheetInfo in Loft Data Grids 6.2

Same name and namespace in other branches
  1. 7.2 vendor/phpoffice/phpexcel/Classes/PHPExcel/Reader/OOCalc.php \PHPExcel_Reader_OOCalc::listWorksheetInfo()

* Return worksheet info (Name, Last Column Letter, Last Column Index, Total Rows, Total Columns) * *

Parameters

string $pFilename: * @throws PHPExcel_Reader_Exception

File

vendor/phpoffice/phpexcel/Classes/PHPExcel/Reader/OOCalc.php, line 174

Class

PHPExcel_Reader_OOCalc
PHPExcel_Reader_OOCalc

Code

public function listWorksheetInfo($pFilename) {

  // Check if file exists
  if (!file_exists($pFilename)) {
    throw new PHPExcel_Reader_Exception("Could not open " . $pFilename . " for reading! File does not exist.");
  }
  $worksheetInfo = array();
  $zipClass = PHPExcel_Settings::getZipClass();
  $zip = new $zipClass();
  if (!$zip
    ->open($pFilename)) {
    throw new PHPExcel_Reader_Exception("Could not open " . $pFilename . " for reading! Error opening file.");
  }
  $xml = new XMLReader();
  $res = $xml
    ->xml($this
    ->securityScanFile('zip://' . realpath($pFilename) . '#content.xml'), null, PHPExcel_Settings::getLibXmlLoaderOptions());
  $xml
    ->setParserProperty(2, true);

  //	Step into the first level of content of the XML
  $xml
    ->read();
  while ($xml
    ->read()) {

    //	Quickly jump through to the office:body node
    while ($xml->name !== 'office:body') {
      if ($xml->isEmptyElement) {
        $xml
          ->read();
      }
      else {
        $xml
          ->next();
      }
    }

    //	Now read each node until we find our first table:table node
    while ($xml
      ->read()) {
      if ($xml->name == 'table:table' && $xml->nodeType == XMLReader::ELEMENT) {
        $worksheetNames[] = $xml
          ->getAttribute('table:name');
        $tmpInfo = array(
          'worksheetName' => $xml
            ->getAttribute('table:name'),
          'lastColumnLetter' => 'A',
          'lastColumnIndex' => 0,
          'totalRows' => 0,
          'totalColumns' => 0,
        );

        //	Loop through each child node of the table:table element reading
        $currCells = 0;
        do {
          $xml
            ->read();
          if ($xml->name == 'table:table-row' && $xml->nodeType == XMLReader::ELEMENT) {
            $rowspan = $xml
              ->getAttribute('table:number-rows-repeated');
            $rowspan = empty($rowspan) ? 1 : $rowspan;
            $tmpInfo['totalRows'] += $rowspan;
            $tmpInfo['totalColumns'] = max($tmpInfo['totalColumns'], $currCells);
            $currCells = 0;

            //	Step into the row
            $xml
              ->read();
            do {
              if ($xml->name == 'table:table-cell' && $xml->nodeType == XMLReader::ELEMENT) {
                if (!$xml->isEmptyElement) {
                  $currCells++;
                  $xml
                    ->next();
                }
                else {
                  $xml
                    ->read();
                }
              }
              elseif ($xml->name == 'table:covered-table-cell' && $xml->nodeType == XMLReader::ELEMENT) {
                $mergeSize = $xml
                  ->getAttribute('table:number-columns-repeated');
                $currCells += $mergeSize;
                $xml
                  ->read();
              }
            } while ($xml->name != 'table:table-row');
          }
        } while ($xml->name != 'table:table');
        $tmpInfo['totalColumns'] = max($tmpInfo['totalColumns'], $currCells);
        $tmpInfo['lastColumnIndex'] = $tmpInfo['totalColumns'] - 1;
        $tmpInfo['lastColumnLetter'] = PHPExcel_Cell::stringFromColumnIndex($tmpInfo['lastColumnIndex']);
        $worksheetInfo[] = $tmpInfo;
      }
    }

    //				foreach($workbookData->table as $worksheetDataSet) {
    //					$worksheetData = $worksheetDataSet->children($namespacesContent['table']);
    //					$worksheetDataAttributes = $worksheetDataSet->attributes($namespacesContent['table']);
    //
    //					$rowIndex = 0;
    //					foreach ($worksheetData as $key => $rowData) {
    //						switch ($key) {
    //							case 'table-row' :
    //								$rowDataTableAttributes = $rowData->attributes($namespacesContent['table']);
    //								$rowRepeats = (isset($rowDataTableAttributes['number-rows-repeated'])) ?
    //										$rowDataTableAttributes['number-rows-repeated'] : 1;
    //								$columnIndex = 0;
    //
    //								foreach ($rowData as $key => $cellData) {
    //									$cellDataTableAttributes = $cellData->attributes($namespacesContent['table']);
    //									$colRepeats = (isset($cellDataTableAttributes['number-columns-repeated'])) ?
    //										$cellDataTableAttributes['number-columns-repeated'] : 1;
    //									$cellDataOfficeAttributes = $cellData->attributes($namespacesContent['office']);
    //									if (isset($cellDataOfficeAttributes['value-type'])) {
    //										$tmpInfo['lastColumnIndex'] = max($tmpInfo['lastColumnIndex'], $columnIndex + $colRepeats - 1);
    //										$tmpInfo['totalRows'] = max($tmpInfo['totalRows'], $rowIndex + $rowRepeats);
    //									}
    //									$columnIndex += $colRepeats;
    //								}
    //								$rowIndex += $rowRepeats;
    //								break;
    //						}
    //					}
    //
    //					$tmpInfo['lastColumnLetter'] = PHPExcel_Cell::stringFromColumnIndex($tmpInfo['lastColumnIndex']);
    //					$tmpInfo['totalColumns'] = $tmpInfo['lastColumnIndex'] + 1;
    //
    //				}
    //			}
  }
  return $worksheetInfo;
}