You are here

function phpexcel_import in PHPExcel 7

Same name and namespace in other branches
  1. 8.3 phpexcel.inc \phpexcel_import()
  2. 6.2 phpexcel.api.inc \phpexcel_import()
  3. 6 phpexcel.api.inc \phpexcel_import()
  4. 7.3 phpexcel.inc \phpexcel_import()
  5. 7.2 phpexcel.inc \phpexcel_import()

Simple API function that will load an Excel file from $path and parse it as a multidimensional array.

Parameters

string $path: The path to the Excel file. Must be readable.

boolean $keyed_by_headers = TRUE: If TRUE, will key the row array with the header values and will skip the header row. If FALSE, will contain the headers in the first row and the rows will be keyed numerically.

Return value

array|boolean The parsed data as an array on success, FALSE on error. Look into watchdog logs for information about errors.

File

./phpexcel.api.inc, line 242
Defines the phpexcel api functions that other modules can use.

Code

function phpexcel_import($path, $keyed_by_headers = TRUE) {
  if (is_readable($path)) {
    require_once 'sites/all/libraries/PHPExcel/PHPExcel.php';
    $xls_reader = PHPExcel_IOFactory::createReaderForFile($path);
    $xls_reader
      ->setReadDataOnly(TRUE);
    $xls_data = $xls_reader
      ->load($path);
    $data = array();
    $headers = array();
    $i = 0;
    phpexcel_invoke('import', 'full', $xls_data, $xls_reader, array(
      'keyed_by_headers' => $keyed_by_headers,
    ));
    foreach ($xls_data
      ->getWorksheetIterator() as $worksheet) {
      $j = 0;
      phpexcel_invoke('import', 'sheet', $worksheet, $xls_reader, array(
        'keyed_by_headers' => $keyed_by_headers,
      ));
      foreach ($worksheet
        ->getRowIterator() as $row) {
        $k = 0;
        phpexcel_invoke('import', 'row', $row, $xls_reader, array(
          'keyed_by_headers' => $keyed_by_headers,
        ));
        $cells = $row
          ->getCellIterator();
        $cells
          ->setIterateOnlyExistingCells(FALSE);
        foreach ($cells as $cell) {
          if (!$j && $keyed_by_headers) {
            $value = $cell
              ->getValue() ? trim($cell
              ->getValue()) : $k;
            phpexcel_invoke('import', 'pre cell', $value, $cell, array(
              'keyed_by_headers' => $keyed_by_headers,
            ), $k, $j);
            $headers[$i][] = $value;
          }
          elseif ($keyed_by_headers) {
            $value = $cell
              ->getValue() ? $cell
              ->getValue() : '';
            phpexcel_invoke('import', 'pre cell', $value, $cell, array(
              'keyed_by_headers' => $keyed_by_headers,
            ), $k, $j);
            $data[$i][$j - 1][$headers[$i][$k]] = $value;
            phpexcel_invoke('import', 'post cell', $data[$i][$j - 1][$headers[$i][$k]], $cell, array(
              'keyed_by_headers' => $keyed_by_headers,
            ), $k, $j);
          }
          else {
            $value = $cell
              ->getValue() ? $cell
              ->getValue() : '';
            phpexcel_invoke('import', 'pre cell', $value, $cell, array(
              'keyed_by_headers' => $keyed_by_headers,
            ), $k, $j);
            $data[$i][$j][] = $value;
            phpexcel_invoke('import', 'post cell', $data[$i][$j][$k], $cell, array(
              'keyed_by_headers' => $keyed_by_headers,
            ), $k, $j);
          }
          $k++;
        }
        $j++;
      }
      $i++;
    }
    return $data;
  }
  else {
    watchdog('phpexcel', "The path '!path' is not readable !", array(
      '!path' => $path,
    ));
    return FALSE;
  }
}