-
-
Save parzibyte/94adecffabe670c234acba61e33990db 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 | |
use Illuminate\Support\Facades\Route; | |
use PhpOffice\PhpSpreadsheet\Spreadsheet; | |
use PhpOffice\PhpSpreadsheet\Writer\Xlsx; | |
use PhpOffice\PhpSpreadsheet\IOFactory; | |
/* | |
|-------------------------------------------------------------------------- | |
| Web Routes | |
|-------------------------------------------------------------------------- | |
| | |
| Here is where you can register web routes for your application. These | |
| routes are loaded by the RouteServiceProvider within a group which | |
| contains the "web" middleware group. Now create something great! | |
| | |
*/ | |
Route::get('/', function () { | |
return view('welcome'); | |
}); | |
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