You are here

function _phpexcel_set_headers in PHPExcel 8.3

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

Sets the Excel file headers.

See also

phpexcel_export()

1 call to _phpexcel_set_headers()
phpexcel_export in ./phpexcel.inc
Simple API function which will generate an XLS file and save it in $path.

File

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

Code

function _phpexcel_set_headers($xls, &$headers, $options) {

  // Prior to PHP 5.3, calling current() on an associative array would not work.
  // Get only array values, just in case.
  if (!is_array(current(array_values($headers)))) {
    $headers = array(
      $headers,
    );
  }
  phpexcel_invoke('export', 'headers', $headers, $xls, $options);
  $sheet_id = 0;
  foreach ($headers as $sheet_name => $sheet_headers) {

    // If the sheet name is just an index, assume to create a string name
    if (is_numeric($sheet_name)) {
      $sheet_name = t('Worksheet !id', array(
        '!id' => $sheet_id + 1,
      ));
    }

    // First, attempt to open an existing sheet by the given name.
    if (($sheet = $xls
      ->getSheetByName($sheet_name)) === NULL) {
      if ($sheet_id) {
        $xls
          ->createSheet($sheet_id);
        $sheet = $xls
          ->setActiveSheetIndex($sheet_id);
      }
      else {

        // PHPExcel always creates one sheet.
        $sheet = $xls
          ->getSheet();
      }
      $sheet
        ->setTitle($sheet_name);
      phpexcel_invoke('export', 'new sheet', $sheet_id, $xls, $options);
    }
    for ($i = 0, $len = count($sheet_headers); $i < $len; $i++) {
      $value = trim($sheet_headers[$i]);
      phpexcel_invoke('export', 'pre cell', $value, $sheet, $options, $i, 1);
      $sheet
        ->setCellValueByColumnAndRow($i, 1, $value);
      phpexcel_invoke('export', 'post cell', $value, $sheet, $options, $i, 1);
    }
    $sheet_id++;
  }
}