Este paquete integra su aplicación Symfony 4/5/6/7 con la biblioteca de productividad PHPOffice PhpSpreadsheet.
Este paquete requiere, además de los requisitos previos de cada biblioteca PHPOffice:
* PHP 7.2 or higher
* Symfony 4 or higher
Nota: Las etiquetas anteriores a v1.0.0 (por ejemplo, v0.2.0) ya no son compatibles debido al estado obsoleto tanto para PHP <= 7.1 como para Symfony <= 4.4.
Utilice Composer para solicitar la última versión estable.
composer require yectep/phpspreadsheet-bundle
Si no está utilizando Flex, habilite el paquete en su archivo AppKernel.php
o bundles.php
.
$ bundles = array (
[...]
new Yectep PhpSpreadsheetBundle PhpSpreadsheetBundle (),
);
Este paquete habilita el servicio phpoffice.spreadsheet
.
Consulte también la documentación oficial de PHPOffice PhpSpreadsheet.
Crea un objeto PhpOfficePhpSpreadsheetSpreadsheet
vacío o, si se pasa un $filename
opcional, crea una instancia de PhpOfficePhpSpreadsheetIOFactory
para detectar y usar automáticamente la clase IWriter
apropiada para leer el archivo.
// In your controller
$ newSpreadsheet = $ this -> get ( ' phpoffice.spreadsheet ' )-> createSpreadsheet ();
$ existingXlsx = $ this -> get ( ' phpoffice.spreadsheet ' )-> createSpreadsheet ( ' /path/to/file.xlsx ' );
string
$tipo) Devuelve una instancia de la clase PhpOfficePhpSpreadsheetReader
del $type
dado.
Los tipos distinguen entre mayúsculas y minúsculas. Los tipos admitidos son:
Xlsx
: Excel 2007Xls
: Excel 5/BIFF (95)Xml
: Excel 2003 XMLSlk
: Enlace simbólico (SYLK)Ods
: Open/Libre Office (ODS)Csv
: CSVHtml
:HTML $ readerXlsx = $ this -> get ( ' phpoffice.spreadsheet ' )-> createReader ( ' Xlsx ' );
$ spreadsheet = $ readerXlsx -> load ( ' /path/to/file.xlsx ' );
Spreadsheet
$hoja de cálculo, string
$tipo) Dado un objeto PhpOfficePhpSpreadsheetSpreadsheet
y un escritor $type
, devuelve una instancia de una clase PhpOfficePhpSpreadsheetWriter
para ese tipo.
Además de los tipos de lectura anteriores, estos tipos también son compatibles con la escritura, si están instaladas las bibliotecas PHP adecuadas.
Tcpdf
Mpdf
Dompdf
$ spreadsheet = $ this -> get ( ' phpoffice.spreadsheet ' )-> createSpreadsheet ();
$ spreadsheet -> getActiveSheet ()-> setCellValue ( ' A1 ' , ' Hello world ' );
$ writerXlsx = $ this -> get ( ' phpoffice.spreadsheet ' )-> createWriter ( $ spreadsheet , ' Xlsx ' );
$ writerXlsx -> save ( ' /path/to/destination.xlsx ' );
Las contribuciones son más que bienvenidas. Bifurca el proyecto y envía un PR cuando hayas terminado.
Todos los restantes incluyen:
Si está migrando desde el componente Symfony Serializer + codificador CSV, puede usar código como
$ spreadsheet = $ this -> get ( ' phpoffice.spreadsheet ' )-> createSpreadsheet ();
$ sheet = $ spreadsheet -> getActiveSheet ();
$ sheet -> setTitle ( $ this -> filterVars [ ' wareCategory ' ]-> getTitle ());
$ columnsMap = [];
$ lineIndex = 2 ;
foreach ( $ data as $ line ) {
foreach ( $ line as $ columnName => $ columnValue ) {
if ( is_int ( $ columnIndex = array_search ( $ columnName , $ columnsMap ))) {
$ columnIndex ++;
} else {
$ columnsMap [] = $ columnName ;
$ columnIndex = count ( $ columnsMap );
}
$ sheet -> getCellByColumnAndRow ( $ columnIndex , $ lineIndex )-> setValue ( $ columnValue );
}
$ lineIndex ++;
}
foreach ( $ columnsMap as $ columnMapId => $ columnTitle ) {
$ sheet -> getCellByColumnAndRow ( $ columnMapId + 1 , 1 )-> setValue ( $ columnTitle );
}
$ writer = $ this -> get ( ' phpoffice.spreadsheet ' )-> createWriter ( $ spreadsheet , ' Xlsx ' );
ob_start ();
$ writer -> save ( ' php://output ' );
$ excelOutput = ob_get_clean ();
return new Response (
$ excelOutput ,
200 ,
[
' content-type ' => ' text/x-csv; charset=windows-1251 ' ,
' Content-Disposition ' => ' attachment; filename="price.xlsx" '
]
);