You are here

public function PHPExcel_Worksheet::fromArray in Loft Data Grids 7.2

Same name and namespace in other branches
  1. 6.2 vendor/phpoffice/phpexcel/Classes/PHPExcel/Worksheet.php \PHPExcel_Worksheet::fromArray()

Fill worksheet from values in array

Parameters

array $source Source array:

mixed $nullValue Value in source array that stands for blank cell:

string $startCell Insert array starting from this cell address as the top left coordinate:

boolean $strictNullComparison Apply strict comparison when testing for null values in the array:

Return value

PHPExcel_Worksheet

Throws

PHPExcel_Exception

File

vendor/phpoffice/phpexcel/Classes/PHPExcel/Worksheet.php, line 2405

Class

PHPExcel_Worksheet
PHPExcel_Worksheet

Code

public function fromArray($source = null, $nullValue = null, $startCell = 'A1', $strictNullComparison = false) {
  if (is_array($source)) {

    //    Convert a 1-D array to 2-D (for ease of looping)
    if (!is_array(end($source))) {
      $source = array(
        $source,
      );
    }

    // start coordinate
    list($startColumn, $startRow) = PHPExcel_Cell::coordinateFromString($startCell);

    // Loop through $source
    foreach ($source as $rowData) {
      $currentColumn = $startColumn;
      foreach ($rowData as $cellValue) {
        if ($strictNullComparison) {
          if ($cellValue !== $nullValue) {

            // Set cell value
            $this
              ->getCell($currentColumn . $startRow)
              ->setValue($cellValue);
          }
        }
        else {
          if ($cellValue != $nullValue) {

            // Set cell value
            $this
              ->getCell($currentColumn . $startRow)
              ->setValue($cellValue);
          }
        }
        ++$currentColumn;
      }
      ++$startRow;
    }
  }
  else {
    throw new PHPExcel_Exception("Parameter \$source should be an array.");
  }
  return $this;
}