You are here

function phpexcel_export in PHPExcel 6

Same name and namespace in other branches
  1. 8.3 phpexcel.inc \phpexcel_export()
  2. 6.2 phpexcel.api.inc \phpexcel_export()
  3. 7.3 phpexcel.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: [format] = The EXCEL format. Can be either 'xls' or 'xlsx' [creator] = The name of the creator [title] = The title [subject] = The subject [description] = The description 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

bool TRUE on success, FALSE on error. Look in watchdog logs for information about errors.

4 calls to phpexcel_export()
PHPExcelTest::testMultipleWorksheetExport in tests/phpexcel.test
Test multiple worksheet Excel export.
PHPExcelTest::testSingleWorksheetExport in tests/phpexcel.test
Test a simple, single worksheet Excel export.
PHPExcelTest::testTemplateExport in tests/phpexcel.test
Test a simple, single worksheet Excel export.
phpexcel_export_db_result in ./phpexcel.api.inc
Simple API function which allows to export a db_query() result to an Excel file. The headers will be set to the names of the exported columns.

File

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

Code

function phpexcel_export($headers = array(), $data = array(), $path = '', $options = NULL) {
  if (!count($headers) && (!isset($options['ignore_headers']) || isset($options['ignore_headers']) && !$options['ignore_headers'])) {
    watchdog('phpexcel', "You must provide at lease one header entry!", array(), WATCHDOG_ERROR);
    return FALSE;
  }
  if (!count($data)) {
    watchdog('phpexcel', "No data provided!", array(), WATCHDOG_ERROR);
    return FALSE;
  }
  if (!is_writable($path)) {
    watchdog('phpexcel', "Path '@path' is not writable!", array(
      '@path' => $path,
    ), WATCHDOG_ERROR);
    return FALSE;
  }
  if (!file_exists('sites/all/libraries/PHPExcel/PHPExcel.php')) {
    watchdog('phpexcel', "Couldn't find the PHPExcel library! ", array(), WATCHDOG_ERROR);
    return FALSE;
  }
  require_once 'sites/all/libraries/PHPExcel/PHPExcel.php';
  $path = file_munge_filename($path, 'xls xlsx');

  // Can we use Memcache ?
  $memcache = variable_get('phpexcel_memcache', '');
  if (empty($memcache) || !($cache_method = PHPExcel_CachedObjectStorageFactory::cache_to_memcache)) {
    $cache_method = PHPExcel_CachedObjectStorageFactory::cache_in_memory_gzip;
    $cache_settings = array();
  }
  else {
    $cache_settings = array(
      'memcacheServer' => $memcache,
    );
  }
  PHPExcel_Settings::setCacheStorageMethod($cache_method, $cache_settings);

  // Must we render from a template file ?
  if (!empty($options['template'])) {
    $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, count(reset(array_values($headers))), $options);
  if (!isset($options['format']) || $options['format'] == 'xls') {
    $writer = new PHPExcel_Writer_Excel5($xls);
  }
  else {
    $writer = new PHPExcel_Writer_Excel2007($xls);
  }
  $writer
    ->save($path);
  return file_exists($path);
}