Hello!!
I have a problem when I want to create a xlsx file. I’d done it in another version of Cake (3.3) but in this (3.4) I can’t.
I did this:
-
I installed PHPExcel - CakeExcel
-
In routes.php Router::extensions(‘xlsx’);
-
Then: Plugin::load(‘CakeExcel’, [‘bootstrap’ => true, ‘routes’ => true]);
4)APPController :
$this->loadComponent(‘RequestHandler’,[
'viewClassMap' => [
'xlsx' => 'CakeExcel.Excel',
],
]);
- Layout /xlsx/default.ctp and in my Controller /xlsx/index.ctp
The ctp:
<?php
$this->PhpExcel->
getProperties()
->setTitle("Cumpleanos $month");
$styleCells = array(
'borders' => array(
'allborders' => array(
'style' => \PHPExcel_Style_Border::BORDER_THIN
)
)
);
$this->PhpExcel->setActiveSheetIndex(0)
->setCellValue('A1', 'Nombre y Apellido')
->setCellValue('B1', 'Día')
->setCellValue('C1', 'Correo')
->setCellValue('D1', 'Correo madre')
->setCellValue('E1', 'Correo padre');
$_row = 1;
foreach ($alumnos as $alumno)
{
$_row = $_row +1;
$this->PhpExcel->setActiveSheetIndex(0)
->setCellValue('A'.$_row,h($alumno->presentacion))
->setCellValue('B'.$_row,h($alumno->fecha_nacimiento->format('j')))
->setCellValue('C'.$_row,h($alumno->email))
->setCellValue('D'.$_row,h($alumno->email_madre))
->setCellValue('E'.$_row,h($alumno->email_padre))
;
// Le aplico a todas las celdas el formato de borde.
$this->PhpExcel->getActiveSheet()->getStyle('A'.$_row)->applyFromArray($styleCells);
$this->PhpExcel->getActiveSheet()->getStyle('B'.$_row)->applyFromArray($styleCells);
$this->PhpExcel->getActiveSheet()->getStyle('C'.$_row)->applyFromArray($styleCells);
$this->PhpExcel->getActiveSheet()->getStyle('D'.$_row)->applyFromArray($styleCells);
$this->PhpExcel->getActiveSheet()->getStyle('E'.$_row)->applyFromArray($styleCells);
}
// Ajusto el ancho de las columnas
foreach (range('A', $this->PhpExcel->getActiveSheet()->getHighestDataColumn()) as $col) {
$this->PhpExcel->getActiveSheet()
->getColumnDimension($col)
->setAutoSize(true);
}
//
// Seteo el formato por default de los bordes para las celdas del encabezado y las pongo en negrita
$styleHeader = array(
'borders' => array(
'allborders' => array(
'style' => \PHPExcel_Style_Border::BORDER_THIN
)
),
'font' => array(
'bold' => true
)
);
$this->PhpExcel->getActiveSheet()->getStyle('A1')->applyFromArray($styleHeader);
$this->PhpExcel->getActiveSheet()->getStyle('B1')->applyFromArray($styleHeader);
$this->PhpExcel->getActiveSheet()->getStyle('C1')->applyFromArray($styleHeader);
$this->PhpExcel->getActiveSheet()->getStyle('D1')->applyFromArray($styleHeader);
$this->PhpExcel->getActiveSheet()->getStyle('E1')->applyFromArray($styleHeader);
// Seteo el nombre del archivo
$_file_name_aux = "Cumple mes de $month";
$objWriter= PHPExcel_IOFactory::createWriter($this->PhpExcel,'Excel5');
Here some errors:
Cake\Http\ResponseEmitter::emit() - CORE\src\Http\ResponseEmitter.php, line 48
Cake\Http\Server::emit() - CORE\src\Http\Server.php, line 105
[main] - ROOT\webroot\index.php, line 37
Warning (2): Cannot modify header information - headers already sent by (output started at C:\wamp\www\siba\vendor\phpoffice\phpexcel\Classes\PHPExcel\Shared\OLE\PPS\Root.php:283) [CORE\src\Http\ResponseEmitter.php, line 149]
Warning (2): Cannot modify header information - headers already sent by (output started at C:\wamp\www\siba\vendor\phpoffice\phpexcel\Classes\PHPExcel\Shared\OLE\PPS\Root.php:283) [CORE\src\Http\ResponseEmitter.php, line 181]
Warning (2): Cannot modify header information - headers already sent by (output started at C:\wamp\www\siba\vendor\phpoffice\phpexcel\Classes\PHPExcel\Shared\OLE\PPS\Root.php:283) [CORE\src\Http\ResponseEmitter.php, line 181]
I tried several ways, but nothing.
The file always is corrupt!!! (Windows)
Thanks, and sorry for my English!