phpexcel下载

发布于 2024-12-22 03:00:56 字数 601 浏览 1 评论 0原文

你好,我是 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");

但这会将其保存到我的服务器,

谢谢

hello i am new to phpexcel,
and i was wondering if there is some way send the excel i have created to the clients download without saving it on my server or to delete it right after he downloads it

i am trying to create an "export button" on a page that will give the user a "pop-up" with the excel that he wants that i have just created.

now after i create the table i do :

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

but that saves it to my server

thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

各自安好 2024-12-29 03:00:56

不要将其保存到文件中,而是将其保存到 php://outputDocs

$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 标头,另请参阅以下问题:为 Excel 文档设置 MIME 类型

Instead of saving it to a file, save it to php://output­Docs:

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

This will send it AS-IS to the browser.

You want to add some headers­Docs first, like it's common with file downloads, so the browser knows which type that file is and how it should be named (the filename):

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

First do the headers, then the save. For the excel headers see as well the following question: Setting mime type for excel document.

笔落惊风雨 2024-12-29 03:00:56
$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');
$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');
夜还是长夜 2024-12-29 03:00:56

使用此调用

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

要将 XLS 工作表输出到您所在的页面,只需确保您所在的页面没有其他 echo、print、输出即可。

Use this call

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

To output the XLS sheet to the page you are on, just make sure that the page you are on has no other echo's,print's, outputs.

oО清风挽发oО 2024-12-29 03:00:56

供 XLSX 使用

设置在带有扩展名的 XLSX 的 $xlsName 名称中。 示例:$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');

FOR XLSX USE

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

FOR XLS USE

SET IN $xlsName name from XLS with extension. Example: $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');
分分钟 2024-12-29 03:00:56
 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');
 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');
﹂绝世的画 2024-12-29 03:00:56

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

下载的所有文件都以空行开头,在我的情况下有四个空行
线,这会带来问题。无论您是否使用 readfile(); 或
save('php://output');,这可以通过在以下位置添加 ob_start(); 来修复
脚本的开头和 ob_end_clean(); 就在 readfile(); 之前或者
保存('php://output');

posible you already solved your problem, any way i hope this help you.

all files downloaded starts with empty line, in my case where four empty
lines, and it make a problem. No matter if you work with readfile(); or
save('php://output');, This can be fixed with adding ob_start(); at the
beginning of the script and ob_end_clean(); just before the readfile(); or
save('php://output');.

寄居人 2024-12-29 03:00:56

我尝试了大多数答案提出的 $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);

I tried the $writer->save('php://output'); command proposed by most answers.

But this happened to download a corrupted file.

To fix it, I had to do this:

    $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);
凝望流年 2024-12-29 03:00:56

需要指出的是,有时对于使用grocery crud v2.9 - v3 的codeigniter 用户来说,您可能会发现导出的excel 文件已损坏且无法打开。

您可能需要实际修改处理 GroceryCrud 中的 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() 。

It is important to state that sometimes for the codeigniter folks using grocery crud v2.9 - v3, you may find that your exported excel files are corrupt and cannot be opened.

You may need to actually modify code sections handling your export to excel in GroceryCrud (even though not recommended to change source code). in particular the exportToExcel() function within ExportState.php, add ob_end_clean() before and after the code block as highlighted below:

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

The obs_end_clean() will clean out outputs and thus wont intefere with what you really want to send to browser which may cause the corrupt file issue.

Hope it helps in your own implementations apart from GroceryCrud. The trick is / or could be the adding of obs_end_clean() before and after your headers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文