Reputation: 71
At work, we use PhpSpreadsheet to generate some Excel file (we use it to be able to provide some export).
Here is the simple code I use (based on some example given but PhpSpreadsheet) to generate a file:
<?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);
}
Unfortunately, it returns an error in the generate Excel file:
Warning: ZipArchive::close(): Failure to create temporary file: No such file or directory in /Applications/XAMPP/xamppfiles/htdocs/bdoparticipantenweekend.nl/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Writer/Xlsx.php on line 409
I understand the error, I just don't know how to fix it. I'm the only one to have this error at the office, of course.. I'm the only one to use PHP 7
I made some research, apparently PHP 7 as another behaviour about Zip::close(): https://www.php.net/manual/en/ziparchive.close.php#119960
Is somebody have a clue how to fix this ?
NB: PhpSpreadsheet is update at the version 1.8.2 (the latest one)
Upvotes: 0
Views: 833
Reputation: 875
Your path is wrong or you have not the permission to write into the path. Change the destination path or the permissions.
Upvotes: 0