phpexcel para descargar


Hola soy nuevo en phpexcel, y me preguntaba si hay alguna manera de enviar el excel que he creado a los clientes descargar sin guardarlo en mi servidor o eliminarlo justo después de que lo descarga

Estoy tratando de crear un "botón de exportación" en una página que le dará al usuario una "ventana emergente" con el excel que quiere que acabo de crear.

Ahora después de crear la tabla lo hago:

$objXLS->getActiveSheet()->getColumnDimension("A")->setAutoSize(true);
$objXLS->getActiveSheet()->getColumnDimension("B")->setAutoSize(true);

$objXLS->getActiveSheet()->setTitle('Test Stats');

$objXLS->setActiveSheetIndex(0);

$objWriter = PHPExcel_IOFactory::createWriter($objXLS, 'Excel5');
$objWriter->save(__DIR__."/test1.xls");

Pero eso lo guarda en mi servidor

Gracias

Author: pnuts, 2011-12-19

6 answers

En Lugar de guardarlo en un archivo, guardarlo en php://outputDocs:

$objWriter->save('php://output');

Esto lo enviará TAL CUAL al navegador.

Desea agregar algunos encabezados Documentos en primer lugar, como es común con las descargas de archivos, por lo que el navegador sabe qué tipo de archivo es y cómo debe ser nombrado (el nombre del archivo):

// We'll be outputting an excel file
header('Content-type: application/vnd.ms-excel');

// It will be called file.xls
header('Content-Disposition: attachment; filename="file.xls"');

// Write file to the browser
$objWriter->save('php://output');

Primero haga las cabeceras, luego guarde. Para los encabezados de excel vea también la siguiente pregunta: Configuración del tipo mime para excel documento.

 157
Author: hakre,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-23 12:34:18
$excel = new PHPExcel();
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="your_name.xls"');
header('Cache-Control: max-age=0');

// Do your stuff here

$writer = PHPExcel_IOFactory::createWriter($excel, 'Excel5');

// This line will force the file to download
$writer->save('php://output');
 21
Author: matino,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-04-17 18:26:46

Use esta llamada

$objWriter->save('php://output');

Para enviar la hoja XLS a la página en la que se encuentra, solo asegúrese de que la página en la que se encuentra no tenga otras salidas de echo,print.

 5
Author: JoshStrange,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-12-19 19:18:19

PARA XLSX USAR

ESTABLECER EN xl xlsName nombre de XLSX con extensión. Ejemplo: xl xlsName = 'teste.xlsx';

$objPHPExcel = new PHPExcel();

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$xlsName.'"');
header('Cache-Control: max-age=0');
$objWriter->save('php://output');

PARA XLS USAR

ESTABLECER EN xl xlsName nombre de XLS con extensión. Ejemplo: xl xlsName = 'teste.xls';

$objPHPExcel = new PHPExcel();

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$xlsName.'"');
header('Cache-Control: max-age=0');
$objWriter->save('php://output');
 5
Author: Rogerio de Moraes,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-12-10 15:12:09

Posible que ya hayas resuelto tu problema, de cualquier manera espero que esto te ayude.

Todos los archivos descargados comienzan con una línea vacía, en mi caso donde cuatro están vacíos líneas, y que hacen un problema. No importa si trabaja con readfile(); o save('php://output');, Esto se puede arreglar con la adición de ob_start(); en el inicio del script y od_end_clean(); justo antes del readfile(); o save('php://output');.

 4
Author: Efren Gutierrez Sosa,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-09-20 18:11:13
 header('Content-type: application/vnd.ms-excel');

 header('Content-Disposition: attachment; filename="file.xlsx"');

 header('Cache-Control: max-age=0');

 header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT');

 header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');

 header ('Cache-Control: cache, must-revalidate');

 header ('Pragma: public');

 $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');

 $objWriter->save('php://output');
 3
Author: harsimer,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-06-18 18:04:41