简体   繁体   English

phpexcel文件下载问题

[英]phpexcel file download issue

I am using the 05featuredemo.php example of the phpexcel, which is downloaded from the codeplex Latest Built PHPExcel_1.8.0_doc by MarkBaker ( SO ) 我现在用的是05featuredemo.php的phpexcel,这是从下载的例子CodePlex上内置最新PHPExcel_1.8.0_docMarkBakerSO

The point i need i look is 我需要看的一点是

$objWriter->save(str_replace('.php', '.xls', __FILE__));

Above is used for saving the file in the script location, But How can i make it as simple downloadable. 上面是用于将文件保存在脚本位置的,但是如何使它变得简单可下载。

I tried with 我尝试过

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

But It didn't download the file that is generated. 但是它没有下载生成的文件。 How can i do that, Is there any other thing i should do ? 我该怎么做,还有什么我应该做的?

If you're sending anything other than straight HTML to a browser, then you need to send the appropriate headers so that the browser can know how to handle it. 如果要向浏览器发送除直接HTML之外的任何内容,则需要发送适当的标头,以便浏览器知道如何处理它。

There is a section of the developer documentation entitled Redirect output to a client's web browser that explains this, and it's also demonstrated in examples like 01simple-download-xls.php and 01simple-download-xlsx.php 开发人员文档中有一个名为“ 将输出重定向到客户端的Web浏览器”的部分对此进行了说明,并在示例01simple-download-xls.php01simple-download-xlsx.php

If you want to give name by yourself then you can specify as I have specified in $a . 如果要自己命名,则可以指定$a指定$a

I wanted to use a filename from where this function is being called so I concat that name to the excel filename and also attached current date to the filename. 我想从调用此函数的地方使用文件名,所以我将该名称连接到excel文件名,并将当前日期附加到文件名。

Note: LogReport is a folder where my files will be downloaded. 注意: LogReport是将下载我的文件的文件夹。

self::$objPHPExcel = new PHPExcel();
$objWriter = PHPExcel_IOFactory::createWriter(self::$objPHPExcel, 'Excel2007');
        $a = 'Log' . self::$filename . date("Y-m-d") . '.xlsx';
        $objWriter->save(__DIR__ . '/LogReport/' . $a);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM