phpexcel to download


phpexcel to download

你好,我是phpexcel的新手,我想知道是否有某种方法可以将我创建的excel发送到客户端下载,而不将其保存在我的服务器上或在他下载后立即删除它

我正试图在一个页面上创建一个"导出按钮",这将给用户一个"弹出"与他想要的excel,我刚刚创建。

现在,在我创建表之后,我做:

$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");

但这将它保存到我的服务器

谢谢

不保存到文件,而是保存到php://output ­Docs:

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

这将把它按原样发送给浏览器。

您需要先添加一些header ­Docs,就像在文件下载中常见的那样,这样浏览器就知道该文件是哪种类型以及它应该如何命名(文件名):

// 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');

首先处理标题,然后保存。设置excel文档的mime类型

$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');

使用这个调用

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

要将XLS表输出到您所在的页面,只需确保您所在的页面没有其他回声、打印输出。

XLSX使用

SET IN $xlsName name from XLSX with extension。例子:$ 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');

SET IN $xlsName name from XLS with extension。示例:$xlsName = 'test .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');
 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');

可能你已经解决了你的问题,无论如何我希望这对你有帮助。

所有下载的文件都以空行开头,在我的例子中,其中4行为空线条,这是一个问题。不管你是用readfile();还是save('php://output');,这可以通过添加ob_start();来解决脚本的开头和ob_end_clean();readfile()之前;或save('php://output'); .

我尝试了大多数答案建议的$writer->save('php://output');命令。

但是碰巧下载了一个损坏的文件。

为了解决这个问题,我必须这样做:
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
    header('Content-type: application/vnd.ms-excel');
    header('Content-Disposition: attachment; filename="filename.xlsx"');
    header('Cache-Control: max-age=0');
    $path = 'path/to/temp/file.xlsx';
    // save content to temporary file
    $objWriter->save($path);
    // This header was key to me, in order to get it working
    header("Content-Length: ".filesize($path));
    // output file content
    readfile($path);
    // delete temporary file
    unlink($path);