Last active
October 9, 2020 17:27
-
-
Save yusufbiberoglu/d311eb618e6efe472b4d599df08c4107 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Controller; | |
use App\Entity\User; | |
use Doctrine\ORM\EntityManagerInterface; | |
use PhpOffice\PhpSpreadsheet\Spreadsheet; | |
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; | |
use PhpOffice\PhpSpreadsheet\Writer\Xlsx; | |
use Symfony\Component\Routing\Annotation\Route; | |
class HomeController extends AbstractController | |
{ | |
/** | |
* @var EntityManagerInterface | |
*/ | |
private $entityManager; | |
public function __construct( EntityManagerInterface $entityManager) | |
{ | |
$this->entityManager = $entityManager; | |
} | |
/** | |
* @Route("/", name="home") | |
*/ | |
public function home() | |
{ | |
return $this->render('home/index.html.twig', [ | |
'controller_name' => 'HomeController', | |
]); | |
} | |
private function getData(): array | |
{ | |
/** | |
* @var $user User[] | |
*/ | |
$list = []; | |
$users = $this->entityManager->getRepository(User::class)->findAll(); | |
foreach ($users as $user) { | |
$list[] = [ | |
$user->getFullName(), | |
$user->getEmail(), | |
$user->getBirthday() | |
]; | |
} | |
return $list; | |
} | |
/** | |
* @Route("/export", name="export") | |
*/ | |
public function export() | |
{ | |
$spreadsheet = new Spreadsheet(); | |
$sheet = $spreadsheet->getActiveSheet(); | |
$sheet->setTitle('User List'); | |
$sheet->getCell('A1')->setValue('Full Name'); | |
$sheet->getCell('B1')->setValue('Mail'); | |
$sheet->getCell('C1')->setValue('Birthday'); | |
// Increase row cursor after header write | |
$sheet->fromArray($this->getData(),null, 'A2', true); | |
$writer = new Xlsx($spreadsheet); | |
$writer->save('helloworld.xlsx'); | |
return $this->redirectToRoute('home'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment