Controleur PDF

http://www.fpdf.org/

use App\\Service\\EZDS_Pdf;

/**
 * @Route("/demo/pdf", name="demo_pdf")
 */
public function pdf(Request $request, Container $container, EntityManagerInterface $em)
{
	// --- Parameters
	$format = 'A4';
	$hauteur_ligne = 5.5;

	// --- Init PDF
	$pdf = new EZDS_Pdf(($format == 'A3' ? 'L' : 'P'), 'mm', $format);
	$pdf->SetFont('helvetica', '', 10);
	$pdf->AliasNbPages();
	$pdf->AddPage();
	$pdf->SetAutoPageBreak(true);
	$pdf->SetFillColor(255, 255, 255);
	$pdf->SetLineWidth(0.1);
	$pdf->SetMargins(5, 5, 5);
	$pdf->setFooterString(utf8_decode("Démo - Footer"));

	// --- Display a text
	$pdf->setXY(5, 10);
	$pdf->SetFont('helvetica', '', 10);
	$pdf->Cell(60, $hauteur_ligne, utf8_decode("Hello world"), 0, 1, 'L', false);

	// --- Display on screen
	$pdf->Output();
	exit();
}

Export CSV

use Symfony\\Component\\Serializer\\Encoder\\CsvEncoder;
use Symfony\\Component\\Serializer\\Normalizer\\ObjectNormalizer;
use Symfony\\Component\\Serializer\\Serializer;
use Symfony\\Component\\HttpFoundation\\Response;

$data_formatted[] = [
    "Employé" => "BRACONNIER Jérôme",
    "Année" => "2022",
    "Semaine" => "46",
    "Jour" => "20221121",
    "Statut" => $jour["statutlib"],
];

$context = [
      CsvEncoder::DELIMITER_KEY => ';',
      CsvEncoder::ENCLOSURE_KEY => '"',
      CsvEncoder::ESCAPE_CHAR_KEY => '\\\\',
      CsvEncoder::KEY_SEPARATOR_KEY => ',',
  ];
$encoders = [new CsvEncoder()];
$normalizers = array(new ObjectNormalizer());
$serializer = new Serializer($normalizers, [new CsvEncoder($context)]);
$csvContent = $serializer->serialize($data, 'csv');
$response = new Response($csvContent);
$response->headers->set('Content-Encoding', 'UTF-8');
$response->headers->set('Content-Type', 'text/csv; charset=UTF-8');
$response->headers->set('Content-Disposition', 'attachment; filename=metis_africa_data.csv');
return $response;

Controleur Excel

https://phpspreadsheet.readthedocs.io/en/latest/

use PhpOffice\\PhpSpreadsheet\\Spreadsheet;
use PhpOffice\\PhpSpreadsheet\\Style\\Alignment;
use PhpOffice\\PhpSpreadsheet\\Writer\\Csv;
use PhpOffice\\PhpSpreadsheet\\Writer\\Ods;
use PhpOffice\\PhpSpreadsheet\\Writer\\Xlsx;
use PhpOffice\\PhpSpreadsheet\\Style\\Border;
use PhpOffice\\PhpSpreadsheet\\Style\\Color;
use PhpOffice\\PhpWord\\Element\\Table;

/**
 * @Route("/formation/commissions/xls", name="formation_commissions_xls")
 */
public function commissions(Request $request, Container $container)
{
    $annee = date("Y");

    $spreadsheet = new Spreadsheet();

    $spreadsheet->getProperties()
        ->setCreator("Jérôme BRACONNIER EzDATA Solutions")
        ->setLastModifiedBy("Jérôme BRACONNIER EzDATA Solutions")
        ->setTitle("Tableau des commissions")
        ->setSubject("Tableau des commissions")
        ->setDescription("")
        ->setKeywords("xls tableau commissions")
        ->setCategory("");

    $activeWorksheet = $spreadsheet->getActiveSheet();

    $activeWorksheet->getStyle('A1')->getAlignment()->setHorizontal(\\PhpOffice\\PhpSpreadsheet\\Style\\Alignment::HORIZONTAL_LEFT);
    $activeWorksheet->getStyle('B1:I1')->getAlignment()->setHorizontal(\\PhpOffice\\PhpSpreadsheet\\Style\\Alignment::HORIZONTAL_CENTER);
    $activeWorksheet->getStyle('A1:I1')->getFill()->setFillType(\\PhpOffice\\PhpSpreadsheet\\Style\\Fill::FILL_SOLID)->getStartColor()->setARGB('4578C9');
    
    // On force la largeur de toutes les colonnes en automatique
    foreach(range('A','I') as $columnID) {
        $activeWorksheet->getColumnDimension($columnID)->setAutoSize(true);
    }

		// Legendes
    $activeWorksheet->setCellValue('A1', 'NOMS');
    $activeWorksheet->setCellValue('B1', 'GROUPE');
    $activeWorksheet->setCellValue('C1', 'DEPT');
    $activeWorksheet->setCellValue('D1', 'DATE FACTURE');
    $activeWorksheet->setCellValue('E1', 'N°');
    $activeWorksheet->setCellValue('F1', 'MONTANT FACTURE');
    $activeWorksheet->setCellValue('G1', 'MONTANT TRANSPORT');
    $activeWorksheet->setCellValue('H1', 'FACTURE HORS TRANSPORT');
    $activeWorksheet->setCellValue('I1', 'COMMISSIONS');
    $activeWorksheet->getStyle("A1:I1")->getFont()->setSize(14);

    // --- Get all factures from $annee
    $annee = 2024;
    $factures = $this->em->getConnection()->fetchAllAssociative("SELECT * from ez_facture_fournisseur where YEAR(EZ_FACTURE_FOURNISSEUR_DATE_PIECE) = ? LIMIT 10", [$annee]);
    if ( count($factures) > 0 ) {

        $last_line = count($factures) + 1;

        // Cell with borders
        $activeWorksheet->getStyle('A1:I' . $last_line)->getBorders()->getAllBorders()->setBorderStyle(Border::BORDER_THIN)->setColor(new Color('000000'));

        // On indique un format Date pour la colonne D
        $activeWorksheet->getStyle('D2:D' . $last_line)->getNumberFormat()->setFormatCode("dd/mm/yyyy");

        // Taille Police pour toutes les lignes
        $activeWorksheet->getStyle('A1:I' . $last_line)->getFont()->setSize(14);

        $ligne = 2;
        foreach ( $factures as $facture ){
            
            // Date de la facture
            $activeWorksheet->setCellValue('D'.$ligne, \\PhpOffice\\PhpSpreadsheet\\Shared\\Date::PHPToExcel(strtotime($facture["EZ_FACTURE_FOURNISSEUR_DATE_PIECE"])));
            $activeWorksheet->getStyle('D')->getAlignment()->setHorizontal(\\PhpOffice\\PhpSpreadsheet\\Style\\Alignment::HORIZONTAL_CENTER);

            // Numéro de facture centré
            $activeWorksheet->setCellValue('E'.$ligne, $facture["EZ_FACTURE_FOURNISSEUR_NUMERO_FACTURE"]); // , \\PhpOffice\\PhpSpreadsheet\\Cell\\DataType::TYPE_STRING
            $activeWorksheet->getStyle('E')->getNumberFormat()->setFormatCode('@');
            $activeWorksheet->getStyle('E')->getAlignment()->setHorizontal(\\PhpOffice\\PhpSpreadsheet\\Style\\Alignment::HORIZONTAL_CENTER);

            // Montant de la facture
            $activeWorksheet->setCellValue('F'.$ligne, $facture["EZ_FACTURE_FOURNISSEUR_MONTANT_TTC"]);
            $activeWorksheet->getStyle('F')->getNumberFormat()->setFormatCode('0.00');
            $activeWorksheet->getStyle('F')->getAlignment()->setHorizontal(\\PhpOffice\\PhpSpreadsheet\\Style\\Alignment::HORIZONTAL_RIGHT);

            $ligne++;
        }
    }

    $writer = new Xlsx($spreadsheet);
    header('Content-Disposition: attachment;filename="commission.xlsx"');
    $writer->save('php://output');
    exit();
}