在工作中,我们使用PhpSpreadsheet生成一些Excel文件(使用它可以提供一些导出)。
这是我使用的简单代码(基于给定的示例,但使用PhpSpreadsheet)生成文件:
<?php
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
include_once '../../../libs/includes.inc';
/** @var array $_POST */
$_POST = db::encodeHTMLPost($_POST);
/** @var string $httpReferer */
$httpReferer = $_SERVER['HTTP_REFERER'];
if (strstr($httpReferer, '?')) {
$httpReferer = explode('?', $httpReferer);
$httpReferer = $httpReferer[0];
}
try {
/** @var Spreadsheet $spreadsheet */
$spreadsheet = new Spreadsheet();
//$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
// Set the document properties
$spreadsheet
->getProperties()
->setCreator('Christina')
->setLastModifiedBy('Christina')
->setTitle('Test XLSX')
->setSubject('Here is the test')
->setDescription('Test document for Office 2007 XLSX, generated using PHP classes.')
->setKeywords('office 2007 openxml php')
->setCategory('Test result file');
$spreadsheet->setActiveSheetIndex(0)
->setCellValue('A1', 'Hello')
->setCellValue('B2', 'World')
->setCellValue('C1', 'Here')
->setCellValue('D2', 'test');
$spreadsheet->getActiveSheet()->setTitle('A simple test');
$spreadsheet->setActiveSheetIndex(0);
// Redirect output to a client’s web browser (Xlsx)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="01simple.xlsx"');
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 = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
$writer->save('php://output');
exit;
} catch (Exception $e) {
var_dump($e);
}
不幸的是,它在生成的Excel文件中返回错误:
警告:ZipArchive :: close():无法创建临时文件:/Applications/XAMPP/xamppfiles/htdocs/bdoparticipantenweekend.nl/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/中没有此类文件或目录409行上的Writer / Xlsx.php
我了解该错误,但我不知道如何解决。 当然,我是唯一一个在办公室出现此错误的人。.我是唯一一个使用PHP 7的人。
我进行了一些研究,显然是PHP 7,这是有关Zip :: close():https://www.php.net/manual/en/ziparchive.close.php#119960
的另一种行为。有人知道如何解决此问题吗?
注意:PhpSpreadsheet是版本1.8.2(最新版本)的更新
答案 0 :(得分:0)
您的路径错误,或者您没有写入路径的权限。更改目标路径或权限。