Search
 
SCRIPT & CODE EXAMPLE
 

PHP

phpoffice create excel and download

use PhpOfficePhpSpreadsheetSpreadsheet;
use PhpOfficePhpSpreadsheetWriterXlsx;

class DownloadExcel
{
    public static function createExcel(array $data, array $headers = [],
                                       $fileName = 'data.xlsx')
    {
        $spreadsheet = new Spreadsheet();
        $sheet = $spreadsheet->getActiveSheet();

        for ($i = 0, $l = sizeof($headers); $i < $l; $i++) {
            $sheet->setCellValueByColumnAndRow($i + 1, 1, $headers[$i]);
        }

        for ($i = 0, $l = sizeof($data); $i < $l; $i++) { // row $i
            $j = 0;
            foreach ($data[$i] as $k => $v) { // column $j
                $sheet->setCellValueByColumnAndRow($j + 1, ($i + 1 + 1), $v);
                $j++;
            }
        }

        $writer = new Xlsx($spreadsheet);
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment; filename="'. urlencode($fileName).'"');
        $writer->save('php://output');
    }

}
Comment

PREVIOUS NEXT
Code Example
Php :: how to check if user is logged in wordpress 
Php :: larave artisan command run in web 
Php :: laravel eloquent to array key value 
Php :: laravel limit foreach 
Php :: redirect back with input laravel in request 
Php :: add blade in blade laravel 
Php :: install laravel in ubuntu 20.04 
Php :: get the current date and time in php 
Php :: wordpress add to cart redirect php 
Php :: multiple middleware laravel 
Php :: session variable in laravel 
Php :: find the highest number from array in php 
Php :: composer create project laravel 
Php :: tl to usd 
Php :: laravel select default old value 
Php :: use latest with first in laravel eloquent 
Php :: php requuire once 
Php :: faker laravel 
Php :: laravel make view 
Php :: laravel menu active class 
Php :: php sort multi dimensional array 
Php :: woocommerce profile photo upload 
Php :: check variable type in php 
Php :: laravel 4.2 migration 
Php :: json to array php 
Php :: laravel unsigned integer 
Php :: create a modal livewire laravel 
Php :: wordpress translate specific text php 
Php :: how to get current location latitude and longitude in php 
Php :: php end session 
ADD CONTENT
Topic
Content
Source link
Name
8+1 =