Laravel export excel files

Code Examples, Laravel

Laravel export excel files

Download PhpSpreadsheet from official website(use composer method).

Paste the downloaded folder at app/Http/Helpers folder.

Create a Function in Controller and include and use the downloaded sdk.

public function phpoffice()
{
	require(app_path() . '/Http/Helpers/phpoffice/vendor/autoload.php');
	$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();

	$sheet = $spreadsheet->getActiveSheet();

	$sheet->setCellValue('A1', 'Laravel Excel !');
	$sheet->getColumnDimension('A')->setAutoSize(true);

	$spreadsheet->getActiveSheet()->getStyle('A1')->getFont()->setSize(16);
			$spreadsheet->getActiveSheet()->getStyle('A1')->getAlignment()->setWrapText(true);

	$writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);

	header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
	header('Content-Disposition: attachment; filename="export.xlsx"');
	$writer->save('php://output');

}

click for more Laravel codes