Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write data from array to sheet using phpspreadsheet library

How can I create excel sheet column headers from array using phpspreadsheet library?

Below is the code I am trying but it's not working:

    // $header is an array containing column headers
    $header = array("Customer Number", "Customer Name", "Address", "City", "State", "Zip");
    $spreadsheet = new Spreadsheet();
    $sheet = $spreadsheet->getActiveSheet();
    $sheet->fromArray($header, NULL, 'A1');     

    // redirect output to client browser
    header('Content-Type: application/vnd.ms-excel');
    header('Content-Disposition: attachment;filename="myfile.xlsx"');
    header('Cache-Control: max-age=0');

    $writer = new Xlsx($spreadsheet);
    $writer->save('php://output');
like image 999
Gurpreet Kaur Avatar asked Feb 08 '18 09:02

Gurpreet Kaur


People also ask

How to write data in Excel file in PHP?

You may use the following code: header('Content-Type: application/vnd. ms-excel'); header('Content-Disposition: attachment;filename="file. xlsx"'); header('Cache-Control: max-age=0'); $writer->save('php://output');

How to use PhpSpreadsheet?

COMMAND-LINE DOWNLOAD Once you have installed Composer: Open the command line (or terminal). Navigate to your project folder and run composer require phpoffice/phpspreadsheet . Composer will automatically download all the required files into the vendor/ folder.

How to get cell value in Phpexcel?

To retrieve the value of a cell, the cell should first be retrieved from the worksheet using the getCell() method. A cell's value can be read again using the following line of code: $objPHPExcel->getActiveSheet()->getCell('B8')->getValue();

How to read Excel file using PhpSpreadsheet?

Read Excel File First, import the needed library and load the Reader of XLSX. Read the excel file using the load() function. Here test. xlsx is the file name.


1 Answers

You need to write

$sheet->fromArray([$header], NULL, 'A1');
like image 50
Denis Avatar answered Oct 19 '22 11:10

Denis