将数组写入phpexcel

时间:2014-01-29 07:14:27

标签: php phpexcel addrange

我有一个以下数组,需要使用phpexcel写入excel 我可以直接将数组写入excel。

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

如何使用phpexcel将此内容写入excel? 请帮我写下以下格式的excel ExcelFormat

1 个答案:

答案 0 :(得分:1)

是的!您可以使用工作表的fromArray()方法将数组写入工作表。

/**
 * 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'
);

虽然数组需要反映您要写入的行和列,但是您的数组将(按原样)写入9行,每行4列....如果您希望将数组中的3行写入在Excel中的每一行,您需要重新组织您的数组以匹配