Symfony - Phpspreadsheet exportar excel

 
Vista:
sin imagen de perfil
Val: 2
Ha mantenido su posición en Symfony (en relación al último mes)
Gráfica de Symfony

Phpspreadsheet exportar excel

Publicado por Gonzalo (3 intervenciones) el 28/05/2021 17:07:08
Buenas tardes!

Tengo el siguiente problema:

Tengo dos metodos en el controlador que me crea un pdf y un excel respectivamente:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//Metodo para generar un pdf, el CSS lo añadimos en el template
/**
 * @Route("/generarpdf", name="electronica_generapdf")
 */
public function generarPdf()
{
 
    $pdfOptions = new Options();
    $pdfOptions->set('defaultFont', 'Arial');
    $pdfOptions->setIsRemoteEnabled(true);
    $dompdf = new Dompdf($pdfOptions);
 
    $context = stream_context_create([
        'ssl' => [
            'verify_peer' => FALSE,
            'verify_peer_name' => FALSE,
            'allow_self_signed' => TRUE
        ]
    ]);
    $dompdf->setHttpContext($context);
 
    $electronicas = $this->getDoctrine()
        ->getRepository(Electronica::class)
        ->findAll();
 
    $html = $this->renderView('electronica/listaelec.html.twig', [
        'electronicas' => $electronicas,
    ]);
 
    $dompdf->loadHtml($html);
    $dompdf->setPaper('A4', 'landscape');
    $dompdf->render();
 
    $fichier = 'SolicitudesElectronica.pdf';
 
    $dompdf->stream($fichier, [
        'Attachment' => true
    ]);
 
    return new Response();
 
}
 
//Metodo para generar excel
/**
 * @Route("/exportarexcel",  name="electronica_exportarexcel")
 */
public function exportarexcel()
{
    $spreadsheet = new Spreadsheet();
 
 
    /* @var $sheet \PhpOffice\PhpSpreadsheet\Writer\Xlsx\Worksheet */
    $sheet = $spreadsheet->getActiveSheet();
    $sheet->setCellValue('A1', 'ID');
    $sheet->setCellValue('B1', 'num_pedido');
    $sheet->setCellValue('C1', 'Cliente');
    $sheet->setCellValue('D1', 'Vehiculo');
    $sheet->setCellValue('E1', 'Kilometros');
    $sheet->setCellValue('F1', 'Matrícula');
    $sheet->setCellValue('G1', 'Motor');
    $sheet->setCellValue('H1', 'Potencia');
    $sheet->setCellValue('I1', 'Cantidad');
    $sheet->setCellValue('J1', 'Sintomas');
    $sheet->setCellValue('K1', 'Alquiler');
    $sheet->setCellValue('L1', 'Presupuesto');
    $sheet->setCellValue('M1', 'Garantia');
    $sheet->setCellValue('N1', 'Reparación');
    $sheet->setCellValue('O1', 'Alquiler');
    $sheet->setCellValue('P1', 'Fecha');
    $sheet->setCellValue('Q1', 'Estado');
 
    $electronicas = $this->getDoctrine()
        ->getRepository(Electronica::class)
        ->findAll();
 
    $sheet = $spreadsheet->getActiveSheet();
    $sheet->setCellValue('A2', $electronicas);
 
    $sheet->setTitle("Solicitudes Electrónicas");
 
    // Create your Office 2007 Excel (XLSX Format)
    $writer = new Xlsx($spreadsheet);
 
    // Create a Temporary file in the system
    $fileName = 'HistóricoSolicitudesElectrónicas.xlsx';
    $temp_file = tempnam(sys_get_temp_dir(), $fileName);
 
    // Create the excel file in the tmp directory of the system
    $writer->save($temp_file);
 
    // Return the excel file as an attachment
    return $this->file($temp_file, $fileName, ResponseHeaderBag::DISPOSITION_INLINE);
}

En el caso del pdf, todo perfecto, llama al archivo html y en este es donde introduzco la tabla que quiero exportar.

En el caso del excel utilizo el bundle PHPSpreadSheet y no consigo de ninguna manera que me cargue los datos de la Entidad Electrónica (como veréis solo tengo la cabecera). Se podría de alguna manera hacer como en el pdf? Me refiero, que llame a un archivo html y ahí introduzca los datos que quiero que saque, o por el contrario tengo que introducirlo en la propia function exportarexcel()

Saludos y gracias de antemano!
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder