You are here

function _phpexcel_set_columns in PHPExcel 7.2

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

Adds the data to the Excel file.

See also

phpexcel_export()

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

File

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

Code

function _phpexcel_set_columns($xls, &$data, $headers = NULL, $options = array()) {
  if (!is_array(reset(reset(array_values($data))))) {
    $data = array(
      $data,
    );
  }
  phpexcel_invoke('export', 'data', $data, $xls, $options);
  $sheet_id = 0;
  foreach ($data as $sheet_name => $sheet_data) {

    // If the headers are not set, we haven't created any sheets yet.
    // Create them now.
    if (!isset($headers)) {
      $offset = 1;
      $xls
        ->createSheet($sheet_id);
      phpexcel_invoke('export', 'new sheet', $sheet_id, $xls, $options);
      $sheet = $xls
        ->setActiveSheetIndex($sheet_id);
      if (!is_numeric($sheet_name)) {
        $sheet
          ->setTitle($sheet_name);
      }
      else {
        $sheet
          ->setTitle("Worksheet {$sheet_id}");
      }
    }
    else {
      $offset = 2;
      $sheet = $xls
        ->setActiveSheetIndex($sheet_id);
    }
    $col_count = NULL;

    // Reset column count.
    for ($i = 0, $len = count($sheet_data); $i < $len; $i++) {

      // Set new column count.
      if (!isset($col_count)) {
        $col_count = isset($headers) ? count($headers[$sheet_name]) : count($sheet_data[$i]);
      }
      for ($j = 0; $j < $col_count; $j++) {
        $value = isset($sheet_data[$i][$j]) ? $sheet_data[$i][$j] : '';

        // We must offset the row count (by 2, because PHPExcel starts the count at 1), because the first row is used by the headers
        phpexcel_invoke('export', 'pre cell', $value, $sheet, $options, $j, $i + $offset);
        $sheet
          ->setCellValueByColumnAndRow($j, $i + $offset, $value);
        phpexcel_invoke('export', 'post cell', $value, $sheet, $options, $j, $i + $offset);
      }
    }
    $sheet_id++;
  }
}