PHP code example of denisok94 / symfony-export-xlsx
1. Go to this page and download the library: Download denisok94/symfony-export-xlsx library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
denisok94 / symfony-export-xlsx example snippets
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use \Denisok94\SymfonyExportXlsxBundle\Service\XlsxService;
class ExportController extends AbstractController
{
/** @var XlsxService */
private $export;
/**
* @param XlsxService $export
*/
public function __construct(XlsxService $export)
{
$this->export = $export;
}
/**
* @return Response
*/
public function index(): Response
{
$fileName = 'my_first_excel.xlsx';
$temp_file = tempnam(sys_get_temp_dir(), $fileName);
$this->export->setFile($temp_file)->open();
$test = [
['header1' => 'value1', 'header2' => 'value2', 'header3' => 'value3'],
['header1' => 'value4', 'header2' => 'value5', 'header3' => 'value6']
];
foreach ($test as $line) {
$this->export->write($line);
}
$this->export->close();
return $this->file($temp_file, $fileName, ResponseHeaderBag::DISPOSITION_INLINE);
}
}