You are here

function phpexcel_export in PHPExcel 7

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.3 phpexcel.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.

1 call to phpexcel_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)) {
    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);
  $xls = new PHPExcel();
  _phpexcel_set_properties($xls
    ->getProperties(), $options);
  _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);
}