34

I understand that I'll need to write a loop inside which I use SetCellValue('cell_name', 'value'); but is there a method in PHPExcel that just accepts a single array and writes that into an Excel sheet row?

Something like:

$testArray = array('testcelltext1', 'testcelltext2', testcelltext3');
PHPExcel::writeArraytoRow($testArray);
//do the other PHPExcel stuff to actually write the file
.
.
.
// outputs an excel file in which the PHP array was written to the first row

I could not find something like that in the included documentation, but that might just be bad PDF search skills...

Aditya M P
  • 5,127
  • 7
  • 41
  • 72

1 Answers1

74
$objPHPExcel->getActiveSheet()->fromArray($testArray, NULL, 'A1');

It's used in a number of the examples

Arguments as described in the API docs

/**
 * Fill worksheet from values in array
 *
 * @param   array   $source                 Source array
 * @param   mixed   $nullValue              Value in source array that stands for blank cell
 * @param   string  $startCell              Insert array starting from this cell address as the top left coordinate
 * @param   boolean $strictNullComparison   Apply strict comparison when testing for null values in the array
 * @throws Exception
 * @return PHPExcel_Worksheet
 */
Mark Baker
  • 209,507
  • 32
  • 346
  • 385
  • Thank you, works perfectly. Thanks also for PHPExcel, what a grand and useful effort. One more question: does this support 2D arrays (this is probably asking too much!)? I tried nesting an array inside one of the array elements, and I got a `invalid argument for foreach` error. Any another arguments I need to pass, maybe (hopefully! :))? – Aditya M P Dec 13 '12 at 22:10
  • 8
    It does support 2d arrays: a 1d array is treated as a single row, a 2-d array is treated as a range of cells; but they must be regular arrays (i.e. same number of columns in each row)... The 33chartcreate and 10autofilter-selection examples all use 2d arrays – Mark Baker Dec 13 '12 at 22:21