4

I'm using PHPExcel library to export data to excel. I'm able to get all the data to excel as expected. But how can I set the column names from PHP array. Here is the code I'm using. Please help

    $data=(
    array(10) (
      [0] => array(8) (
        [#] => (string)
        [Name] => (string) Student1
        [ID] => (string) 123456
        [Date] => (string) 2016-02-01
        [Group] => (string) Physics
        [Month] => (string) February
        [Year] => (string) 2016
      )
      [1] => array(8) (
        [#] => (string)
        [Name] => (string) Student2
        [ID] => (string) 569874
        [Date] => (string) 2016-02-01
        [Group] => (string) Biology
        [Month] => (string) February
        [Year] => (string) 2016......);

    $objPHPExcel = new PHPExcel();

    $objPHPExcel->setActiveSheetIndex(0);
    $objPHPExcel->getActiveSheet()->setCellValue('A1', "#");
    $objPHPExcel->getActiveSheet()->setCellValue('B1', "Name");
    $objPHPExcel->getActiveSheet()->setCellValue('C1', "ID");
    $objPHPExcel->getActiveSheet()->setCellValue('D1', "Date");
    $objPHPExcel->getActiveSheet()->setCellValue('E1', "Group");
    $objPHPExcel->getActiveSheet()->setCellValue('F1', "Month");
    $objPHPExcel->getActiveSheet()->setCellValue('G1', "Year");

//How to replace/make dynamic lines above to set Cell values in first row based on array data as column names. i.e Name, ID, Date,.....

//Add Data

$objPHPExcel->getActiveSheet()->fromArray($data,NULL,'A2');
Mr.Kiran
  • 79
  • 1
  • 1
  • 9

3 Answers3

7
// Header
$objPHPExcel->getActiveSheet()->fromArray(array_keys(current($data)), null, 'A1');
// Data
$objPHPExcel->getActiveSheet()->fromArray($data, null, 'A2');
Ben Rhys-Lewis
  • 3,118
  • 8
  • 34
  • 45
Blob
  • 71
  • 1
  • 2
4

like this ?

$objPHPExcel->getActiveSheet()->fromArray(array_keys($data[0]),NULL,'A2');

now that i know that's what you want to do, a short explain.

Array_keys copy's all keys from an Array as value to a numbered Array, so if you have an Array like this:

[#] => (string)
[Name] => (string) Student1
[ID] => (string) 123456
[Date] => (string) 2016-02-01
[Group] => (string) Physics
[Month] => (string) February
[Year] => (string) 2016

It will return following Array:

[0] = "#"
[1] = "Name"
[2] = "ID"
...
Doktor OSwaldo
  • 5,732
  • 20
  • 41
  • @doktoroswaldo can you possibly help me with this question https://stackoverflow.com/questions/54244168/laravel-excel-not-exporting-in-correct-format?noredirect=1#comment95315360_54244168 – Geoff_S Jan 18 '19 at 00:31
0

This code generates the columns from an array

class excelExport {
 public $columns = array(            
        0 => array('id' => "name", 'name' => 'Name'), 
        1 => array('id' => "tlf", 'name' => 'Telephone'),                                   
    );


 public function export(){      


    /** PHPExcel */
    $objPHPExcel = new PHPExcel();

    $objPHPExcel->setActiveSheetIndex(0);
    $objWorkSheet = $objPHPExcel->getActiveSheet();   

        $row =  1;
        $col = 0;
        for ($column = 'A'; ord($column) != ord('A')+count($this->columns); $column++) {

            $cell = $objWorkSheet->getCell($column.$row);
            $cell->setValue($this->columns[$col]["name"]);
            $col++;
        }

        ...
Alejandro Aranda
  • 709
  • 8
  • 16