You are here

public function PHPExcel_Reader_Excel2007::listWorksheetInfo in Loft Data Grids 6.2

Same name and namespace in other branches
  1. 7.2 vendor/phpoffice/phpexcel/Classes/PHPExcel/Reader/Excel2007.php \PHPExcel_Reader_Excel2007::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/Excel2007.php, line 169

Class

PHPExcel_Reader_Excel2007
PHPExcel_Reader_Excel2007

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();
  $zip
    ->open($pFilename);
  $rels = simplexml_load_string($this
    ->securityScan($this
    ->_getFromZipArchive($zip, "_rels/.rels")), 'SimpleXMLElement', PHPExcel_Settings::getLibXmlLoaderOptions());

  //~ http://schemas.openxmlformats.org/package/2006/relationships");
  foreach ($rels->Relationship as $rel) {
    if ($rel["Type"] == "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument") {
      $dir = dirname($rel["Target"]);
      $relsWorkbook = simplexml_load_string($this
        ->securityScan($this
        ->_getFromZipArchive($zip, "{$dir}/_rels/" . basename($rel["Target"]) . ".rels")), 'SimpleXMLElement', PHPExcel_Settings::getLibXmlLoaderOptions());

      //~ http://schemas.openxmlformats.org/package/2006/relationships");
      $relsWorkbook
        ->registerXPathNamespace("rel", "http://schemas.openxmlformats.org/package/2006/relationships");
      $worksheets = array();
      foreach ($relsWorkbook->Relationship as $ele) {
        if ($ele["Type"] == "http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet") {
          $worksheets[(string) $ele["Id"]] = $ele["Target"];
        }
      }
      $xmlWorkbook = simplexml_load_string($this
        ->securityScan($this
        ->_getFromZipArchive($zip, "{$rel['Target']}")), 'SimpleXMLElement', PHPExcel_Settings::getLibXmlLoaderOptions());

      //~ http://schemas.openxmlformats.org/spreadsheetml/2006/main");
      if ($xmlWorkbook->sheets) {
        $dir = dirname($rel["Target"]);
        foreach ($xmlWorkbook->sheets->sheet as $eleSheet) {
          $tmpInfo = array(
            'worksheetName' => (string) $eleSheet["name"],
            'lastColumnLetter' => 'A',
            'lastColumnIndex' => 0,
            'totalRows' => 0,
            'totalColumns' => 0,
          );
          $fileWorksheet = $worksheets[(string) self::array_item($eleSheet
            ->attributes("http://schemas.openxmlformats.org/officeDocument/2006/relationships"), "id")];
          $xml = new XMLReader();
          $res = $xml
            ->xml($this
            ->securityScanFile('zip://' . PHPExcel_Shared_File::realpath($pFilename) . '#' . "{$dir}/{$fileWorksheet}"), null, PHPExcel_Settings::getLibXmlLoaderOptions());
          $xml
            ->setParserProperty(2, true);
          $currCells = 0;
          while ($xml
            ->read()) {
            if ($xml->name == 'row' && $xml->nodeType == XMLReader::ELEMENT) {
              $row = $xml
                ->getAttribute('r');
              $tmpInfo['totalRows'] = $row;
              $tmpInfo['totalColumns'] = max($tmpInfo['totalColumns'], $currCells);
              $currCells = 0;
            }
            elseif ($xml->name == 'c' && $xml->nodeType == XMLReader::ELEMENT) {
              $currCells++;
            }
          }
          $tmpInfo['totalColumns'] = max($tmpInfo['totalColumns'], $currCells);
          $xml
            ->close();
          $tmpInfo['lastColumnIndex'] = $tmpInfo['totalColumns'] - 1;
          $tmpInfo['lastColumnLetter'] = PHPExcel_Cell::stringFromColumnIndex($tmpInfo['lastColumnIndex']);
          $worksheetInfo[] = $tmpInfo;
        }
      }
    }
  }
  $zip
    ->close();
  return $worksheetInfo;
}