Write array to phpexcel

3.7k Views Asked by At

I have a following array and need to write into excel using phpexcel I can directly write the array to excel.

array(9) {
  [0] =>
  array(4) {
    'hostname' =>
    string(7) 'Dibeesh'
    'hostemail' =>
    string(14) '[email protected]'
    'hostcity' =>
    string(12) 'Kochi, India'
    'host' =>
    string(4) 'host'
  }
  [1] =>
  array(4) {
    'inviteename' =>
    string(5) 'Test1'
    'inviteeemail' =>
    string(12) '[email protected]'
    'inviteecity' =>
    string(12) 'Kochi, India'
    'invitee' =>
    string(7) 'invitee'
  }
  [2] =>
  array(4) {
    'inviteename' =>
    string(8) 'Nishanth'
    'inviteeemail' =>
    string(18) '[email protected]'
    'inviteecity' =>
    string(5) 'Aluva'
    'invitee' =>
    string(7) 'invitee'
  }
  [3] =>
  array(4) {
    'inviteename' =>
    string(9) 'Lal Mohan'
    'inviteeemail' =>
    string(10) '[email protected]'
    'inviteecity' =>
    string(12) 'Kochi, India'
    'invitee' =>
    string(7) 'invitee'
  }
  [4] =>
  array(4) {
    'inviteename' =>
    string(6) 'Mahesh'
    'inviteeemail' =>
    string(14) '[email protected]'
    'inviteecity' =>
    string(10) 'Tvm, INdia'
    'invitee' =>
    string(7) 'invitee'
  }
  [5] =>
  array(4) {
    'hostname' =>
    string(9) 'Lal Mohan'
    'hostemail' =>
    string(10) '[email protected]'
    'hostcity' =>
    string(12) 'Kochi, India'
    'host' =>
    string(4) 'host'
  }
  [6] =>
  array(4) {
    'inviteename' =>
    string(6) 'Arshad'
    'inviteeemail' =>
    string(13) '[email protected]'
    'inviteecity' =>
    string(12) 'Kochi, India'
    'invitee' =>
    string(7) 'invitee'
  }
  [7] =>
  array(4) {
    'inviteename' =>
    string(5) 'Joyal'
    'inviteeemail' =>
    string(12) '[email protected]'
    'inviteecity' =>
    string(12) 'Kochi, India'
    'invitee' =>
    string(7) 'invitee'
  }
  [8] =>
  array(4) {
    'inviteename' =>
    string(7) 'Dibeesh'
    'inviteeemail' =>
    string(14) '[email protected]'
    'inviteecity' =>
    string(12) 'Kochi, India'
    'invitee' =>
    string(7) 'invitee'
  }
}

How can I write this to excel using phpexcel? Please help me to write to excel in following format ExcelFormat

1

There are 1 best solutions below

0
On

Yes! You can write an array to a worksheet, using the worksheet's fromArray() method.

/**
 * 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 PHPExcel_Exception
 * @return PHPExcel_Worksheet
 */

e.g.

$objPHPExcel->getActiveSheet()->fromArray(
    $myArray,
    NULL,
    'A2'
);

Though the array need to reflect the rows and columns that you're writing to, so your array will (as it stands) write 9 rows of 4 columns each.... if you want 3 rows from your array written to each row in your Excel, then you'll need to reorganise you array to match that