2

I am currently working on exporting a php array to a .xlsx file. And this is how the array looks like:

array(9) { [0]=> string(1) "1" [1]=> string(24) "Company" [2]=> string(9) " 12345689" [3]=> string(9) "Name" [4]=> string(1) "4" [5]=> string(26) "careerintern2017@gmail.com"}

I'd like to export this array to format like:

id | ent_name | ent_num  | owner | ent_c | email 
-------------------------------------------------------------------------
1  | Company  | 123456789| Name  | 4     | careerintern2017@gmail.com
-------------------------------------------------------------------------

And this is what I have so far:

$excel_file_path = "ent_info/ent_info_list.xlsx";
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($excel_file_path);
$row_num = $spreadsheet->setActiveSheetIndex(0)->getHighestRow();
$row_num =$row_num+1;
$col_num=0;
$counter=1;
$sheet = $spreadsheet->getActiveSheet();
foreach($array as $data){
    $pos = get_excel_col($col_num).$row_num;
        //$pos = A2, B2, C2 .... (for example)
    $sheet->setCellValue($pos, $data);
    if($counter==6){
        $row_num++;
        $col_num=0;
    }
    $col_num++;
    $counter++;
}
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, "Xlsx");
$writer->save($excel_file_path);  

Yet, after executing this, it showed nothing on the excel .xlsx file. Is there anything missing above for writing data to an excel file?

Lykas
  • 117
  • 10

1 Answers1

1

can you try the below? according to the doc, it retrieves the cell ($pos) and sets the value.

Make sure $pos has value as well

var_dump($pos);//just to see which cell it is
$spreadsheet->getActiveSheet()
    ->getCell($pos)
    ->setValue('Some value');
Shobi
  • 10,374
  • 6
  • 46
  • 82