本文介绍了phpexcel下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我是新来的phpexcel,
,我想知道是否有某种方式发送我已经创建的客户端下载,而不保存在我的服务器上,或删除它后,他下载它



我正在尝试在一个页面上创建一个导出按钮,这将给用户一个弹出与他刚才创建的excel。 / p>

现在,我创建了我做的表:

  $ objXLS- > getActiveSheet() - > getColumnDimension( A) - > setAutoSize(真); 
$ 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);

但将其保存到我的服务器



谢谢

解决方案

不要将其保存到文件中,请将其保存到 :

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

这将会将AS-IS发送到浏览器。



您要首先添加一些,就像它浏览器知道文件的类型以及应该如何命名(文件名):

  /我们将输出一个excel文件
header('Content-type:application / vnd.ms-excel');

//它将被称为file.xls
header('Content-Disposition:attachment; filename =file.xls');

//将文件写入浏览器
$ objWriter-> save('php:// output');

首先做标题,然后保存。对于excel标题,请参阅以下问题:。


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

解决方案

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

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

This will send it AS-IS to the browser.

You want to add some headers 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.

这篇关于phpexcel下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 18:49