在phpspreadsheet中更改保存目录

时间:2018-06-04 23:23:31

标签: php phpspreadsheet

我使用PhpSpreadsheet生成XLS文件,除了将本地文件保存在临时目录中并且我想将它们保存在特定文件夹中之外,所有功能都完美无缺。

use PhpOffice\PhpSpreadsheet\IOFactory;
require __DIR__ . '/Header.php';

$spreadsheet = require __DIR__ . '/templates/MyTemplate.php';

$filename = $helper->getFilename("MyFilename", 'xls'); 
$writer = IOFactory::createWriter($spreadsheet, 'Xls');

$callStartTime = microtime(true);
$writer->save($filename);
$helper->logWrite($writer, $filename, $callStartTime);

但输出位于

Write Xls format to /var/folders/pn/lyj970q90lq20mjv39bpgx_80000gn/T/phpspreadsheet/MyFilename.xls in 0.0640 seconds

是否还有其他功能来设置我想要文件的保存目录?

3 个答案:

答案 0 :(得分:1)

在路径中使用点

$writer->save("./templates/MyFilename.xls");

答案 1 :(得分:0)

在这一行:

$filename = $helper->getFilename("MyFilename", 'xls'); 

更改为:

$filename = basename($helper->getFilename("MyFilename", 'xls')); 

这样,$ filename将仅包含文件名。然后,您可以插入路径,并根据需要进行操作,就像我对我的操作一样:

$writer->save('C:/xampp/htdocs/tmp/' . $outputFileName);

答案 2 :(得分:0)

确保将这些行注释掉。由于此参数将告诉服务器将Xlsx导出为可下载文件。不在特定的自定义位置。

// Redirect output to a client’s web browser (Xlsx)
// header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
// header('Content-Disposition: attachment;filename="'.$FileName.'"');
// header('Cache-Control: max-age=0');
// // If you're serving to IE 9, then the following may be needed
// header('Cache-Control: max-age=1');

// 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

最后,您可以保存文件:

$writer->save("Location/Of/YourFolder".$YourFileName);