yii 如何选择位置保存文件excel与PHPExcel

sh7euo9m  于 2022-11-09  发布在  PHP
关注(0)|答案(4)|浏览(138)

我有一个问题时,使用PHPExel创建一个excel文件。我想选择位置保存文件excel,但我不知道如何做。

$model = new User();
 $labels = $model->attributeNames();
 $data = $model->findAll();
 $objPHPExcel = Yii::app()->excel;

........
$filename = 'text.xlsx';
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter->save($filename);

请帮帮我,非常感谢.

ttvkxqim

ttvkxqim1#

将文件名更改为所需的路径,

$name = '/path/to/folder/xyz.xlsx';
$objWriter->save($name);

对我有效...

acruukt9

acruukt92#

$objWriter->save($filename);

....将$filename的值更改为您要保存文件的位置的文件路径,例如

$filename = '/path/to/folder/test.xlsx';
$objWriter->save($filename);
lx0bsm1f

lx0bsm1f3#

如果您在php文件中包含以下标题,您的用户将弹出一个下载选项:

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");;
header("Content-Disposition: attachment;filename=$filename");
header("Content-Transfer-Encoding: binary ");
4dbbbstv

4dbbbstv4#

使用PHPExcel下载Excel:

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');

对于使用Excel5 Writer创建的.xls文件:

header('Content-Type: application/vnd.ms-excel'); //mime type


对于使用Excel2007 Writer创建的.xlsx文件:

header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); //mime type

header('Content-Disposition: attachment;filename="you-file-name.xlsx"'); //tell browser what's the file name
header('Cache-Control: max-age=0'); //no cache 
ob_end_clean();
$objWriter->save('php://output'`enter code here`);
exit();

相关问题