-
-
Save parzibyte/de557ad796bde55e300ff629de462488 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 | |
Route::get("/excel", function () { | |
$documento = new Spreadsheet(); | |
$documento | |
->getProperties() | |
->setCreator("Aquí va el creador, como cadena") | |
->setLastModifiedBy('Parzibyte') // última vez modificado por | |
->setTitle('Mi primer documento creado con PhpSpreadSheet') | |
->setSubject('El asunto') | |
->setDescription('Este documento fue generado para parzibyte.me') | |
->setKeywords('etiquetas o palabras clave separadas por espacios') | |
->setCategory('La categoría'); | |
$hoja = $documento->getActiveSheet(); | |
$hoja->setTitle("Productos"); | |
$encabezado = ["Código", "Descripción",]; | |
$hoja->fromArray($encabezado, null, 'A1'); | |
$fila = 2; | |
$productos = [ | |
[ | |
"codigo" => "123", | |
"descripcion" => "Nintendo 3DS" | |
], | |
[ | |
"codigo" => "456", | |
"descripcion" => "Alcohol isopropílico" | |
], | |
[ | |
"codigo" => "1", | |
"descripcion" => "Mouse" | |
], | |
]; | |
foreach ($productos as $producto) { | |
$columna = 1; | |
$hoja->setCellValueByColumnAndRow($columna, $fila, $producto["codigo"]); | |
$columna++; | |
$hoja->setCellValueByColumnAndRow($columna, $fila, $producto["descripcion"]); | |
$fila++; | |
} | |
$nombreDelDocumento = "Productos.xlsx"; | |
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); | |
header('Content-Disposition: attachment;filename="' . $nombreDelDocumento . '"'); | |
header('Cache-Control: max-age=0'); | |
$writer = IOFactory::createWriter($documento, 'Xlsx'); | |
$writer->save('php://output'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment