You are here

function phpexcel_export in PHPExcel 7.3

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

Simple API function which will generate an XLS file and save it in $path.

Parameters

array $headers: An array containing all headers. If given a two-dimensional array, each first dimension entry will be on a separate worksheet ($headers[sheet][header]).

array $data: A two-dimensional array containing all data ($data[row][column]). If given a three-dimensional array, each first dimension entry will be on a separate worksheet ($data[sheet][row][column]).

string $path: The path where the file must be saved. Must be writable.

array $options: An array which allows to set some specific options. Used keys:

  • ignore_headers: whether the $headers parameter should be ignored or not. Defaults to false.
  • format: The EXCEL format. Can be either 'xls', 'xlsx', 'csv', or 'ods'. Defaults to the extension given in the $path parameter, or 'xls'.
  • creator: The name of the creator.
  • title: The title.
  • subject: The subject.
  • description: The description.
  • template: A path to a file to use as a template.
  • merge_cells: Array with sheets and cell ranges for merge. For example: [sheet][0]='A1:C1'.
  • append: whether to append to the existing file. Defaults to TRUE.

The options array will always be passed to all the hooks. If developers need specific information for their own hooks, they can add any data to this array.

Return value

int PHPEXCEL_SUCCESS on success, PHPEXCEL_ERROR_NO_HEADERS, PHPEXCEL_ERROR_NO_DATA, PHPEXCEL_ERROR_PATH_NOT_WRITABLE or PHPEXCEL_ERROR_LIBRARY_NOT_FOUND on error.

See also

hook_phpexcel_export()

Related topics

7 calls to phpexcel_export()
PHPExcelTest::testCustomMethodCalls in tests/phpexcel.test
Test the ability to pass custom methods and arguments on import.
PHPExcelTest::testIgnoreHeaders in tests/phpexcel.test
Test "ignore_headers" option.
PHPExcelTest::testIssue1988868 in tests/phpexcel.test
A cell with a value of '0' must get exported as such.
PHPExcelTest::testMultipleWorksheetExport in tests/phpexcel.test
Test multiple worksheet Excel export.
PHPExcelTest::testSingleWorksheetExport in tests/phpexcel.test
Test a simple, single worksheet Excel export.

... See full list

File

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

Code

function phpexcel_export($headers = array(), $data = array(), $path = '', $options = NULL) {
  if (empty($headers) && empty($options['ignore_headers'])) {
    watchdog('phpexcel', "No header was provided, and the 'ignore_headers' option was not set to TRUE. Excel export aborted.", array(), WATCHDOG_ERROR);
    return PHPEXCEL_ERROR_NO_HEADERS;
  }

  // Make sure we have an ignore_headers key to prevent Notices.
  $options['ignore_headers'] = isset($options['ignore_headers']) ? $options['ignore_headers'] : empty($headers);
  if (!count($data)) {
    watchdog('phpexcel', "No data was provided. Excel export aborted.", array(), WATCHDOG_ERROR);
    return PHPEXCEL_ERROR_NO_DATA;
  }
  if (!(is_writable($path) || !file_exists($path) && is_writable(dirname($path)))) {
    watchdog('phpexcel', "Path '@path' is not writable. Excel export aborted.", array(
      '@path' => $path,
    ), WATCHDOG_ERROR);
    return PHPEXCEL_ERROR_PATH_NOT_WRITABLE;
  }
  $library = libraries_load('PHPExcel');
  if (empty($library['loaded'])) {
    watchdog('phpexcel', "Couldn't find the PHPExcel library. Excel export aborted.", array(), WATCHDOG_ERROR);
    return PHPEXCEL_ERROR_LIBRARY_NOT_FOUND;
  }
  $path = phpexcel_munge_filename($path);

  // Determine caching method.
  list($cache_method, $cache_settings) = _phpexcel_get_cache_settings();

  // Is it available ? If not, return an error.
  if (empty($cache_method)) {
    return PHPEXCEL_CACHING_METHOD_UNAVAILABLE;
  }
  PHPExcel_Settings::setCacheStorageMethod($cache_method, $cache_settings);
  if (!isset($options['append'])) {
    $options['append'] = TRUE;
  }

  // If appending files enabled, see if the file already exists
  if ($options['append'] && file_exists($path)) {
    $xls = PHPExcel_IOFactory::load($path);
  }
  elseif (!empty($options['template'])) {

    // Must we render from a template file ?
    $xls_reader = PHPExcel_IOFactory::createReaderForFile($options['template']);
    $xls = $xls_reader
      ->load($options['template']);
  }
  else {
    $xls = new PHPExcel();
  }
  _phpexcel_set_properties($xls
    ->getProperties(), $options);

  // Must we ignore the headers ?
  if (empty($options['ignore_headers'])) {
    _phpexcel_set_headers($xls, $headers, $options);
  }
  _phpexcel_set_columns($xls, $data, empty($options['ignore_headers']) ? $headers : NULL, $options);

  // Merge cells.
  if (!empty($options['merge_cells'])) {
    foreach ($options['merge_cells'] as $sheet_name => $merge_cells_list) {
      foreach ($merge_cells_list as $merge_cells) {
        $sheet = $xls
          ->setActiveSheetIndex($sheet_name);
        $style = array(
          'alignment' => array(
            'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
          ),
        );
        $sheet
          ->getStyle($merge_cells)
          ->applyFromArray($style);
        $xls
          ->getActiveSheet()
          ->mergeCells($merge_cells);
      }
    }
  }
  $format = isset($options['format']) ? drupal_strtolower($options['format']) : @end(explode('.', $path));
  switch ($format) {
    case 'xlsx':
      $writer = new PHPExcel_Writer_Excel2007($xls);
      break;
    case 'csv':
      $writer = new PHPExcel_Writer_CSV($xls);
      break;
    case 'ods':
      $writer = new PHPExcel_Writer_OpenDocument($xls);
      break;
    default:
      $writer = new PHPExcel_Writer_Excel5($xls);
  }
  $writer
    ->save($path);
  unset($writer);
  return file_exists($path) ? PHPEXCEL_SUCCESS : PHPEXCEL_ERROR_FILE_NOT_WRITTEN;
}