Export an Array of Arrays to Excel in php

后端 未结 3 1597
半阙折子戏
半阙折子戏 2020-12-09 22:27

I have an Array of arrays at the beginning of each sub array is the header of the column followed by integers that I want to populate the column. It looks something like thi

3条回答
  •  长情又很酷
    2020-12-09 22:37

    The best way of exporting array to excel is here.

        $data = array(
             '0' => array('Name'=> 'Parvez', 'Status' =>'complete', 'Priority'=>'Low', 'Salary'=>'001'),
             '1' => array('Name'=> 'Alam', 'Status' =>'inprogress', 'Priority'=>'Low', 'Salary'=>'111'),
             '2' => array('Name'=> 'Sunnay', 'Status' =>'hold', 'Priority'=>'Low', 'Salary'=>'333'),
             '3' => array('Name'=> 'Amir', 'Status' =>'pending', 'Priority'=>'Low', 'Salary'=>'444'),
             '4' => array('Name'=> 'Amir1', 'Status' =>'pending', 'Priority'=>'Low', 'Salary'=>'777'),
             '5' => array('Name'=> 'Amir2', 'Status' =>'pending', 'Priority'=>'Low', 'Salary'=>'777')
            );
            $filename =  time().".xls";      
            header("Content-Type: application/vnd.ms-excel");
            header("Content-Disposition: attachment; filename=\"$filename\"");
    
            ExportFile($data);
            function ExportFile($records) {
                $heading = false;
                    if(!empty($records))
                      foreach($records as $row) {
                        if(!$heading) {
                          // display field/column names as a first row
                          echo implode("\t", array_keys($row)) . "\n";
                          $heading = true;
                        }
                        echo implode("\t", array_values($row)) . "\n";
                    }
                exit;
            }
    

提交回复
热议问题