你好,我是 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');
这会将其按原样发送到浏览器。
您需要首先添加一些 headersDocs,就像文件下载中常见的那样,这样浏览器就知道该文件是什么类型以及应如何命名(文件名):
// 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 标题,请参阅以下问题:Setting mime type for excel document。
$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 来自 XLSX 的名称,带扩展名。 示例:$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');
用于 XLS
SET IN $xlsName 来自 XLS 的名称(带扩展名)。 示例:$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');
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');
可能你已经解决了你的问题,无论如何我希望这对你有帮助。
下载的所有文件都以空行开头,在我的例子中,有四个空行 线,这会带来问题。无论您是否与
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);
需要指出的是,有时对于使用grocery crud v2.9 - v3的codeigniter人员来说,您可能需要实际修改处理导出到excel的代码部分(即使不建议更改源代码),特别是文件ExportState .php,其中有 exportToExcel() 函数,通过在下面突出显示的部分添加 ob_end_clean() 来实现: 例如:
// rest of code above....
ob_end_clean();
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="' . $filename . '.xlsx"');
header('Cache-Control: max-age=0');
// If you're serving to IE over SSL, then the following may be needed
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0
ob_end_clean();
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
obs_end_clean() 将清除输出,因此不会干扰您真正想要发送到浏览器的内容,这可能会导致文件损坏问题。
希望除了 GroceryCrud 之外,它还能对您自己的实现有所帮助。诀窍是或可能是在标头之前和之后添加 obs_end_clean()。