更改phpspreadsheet中的保存目录

7bsow1i6  于 2023-01-16  发布在  PHP
关注(0)|答案(4)|浏览(128)

我使用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

有没有其他功能可以设置我想保存文件的目录?

ebdffaop

ebdffaop1#

在路径中使用点

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

2cmtqfgy2#

在这一行:

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

变更为:

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

这样,$filename将只包含文件名。然后你可以插入你的路径,并让它按照你想要的方式工作,就像我对我的:

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

mkh04yzy3#

确保这些行被注解掉。因为这个参数会告诉服务器把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);

9udxz4iz

9udxz4iz4#

header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="myfile.xlsx"');
header('Cache-Control: max-age=0');

$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save('php://output');

来源https://phpspreadsheet.readthedocs.io/en/latest/topics/recipes/#redirect-output-to-a-clients-web-browser

相关问题