Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PhpSpreadsheet is corrupting files

I am using PhpSpreadsheet to modify an existing file and send it to the browser, but every time I download the file excel gives me the following error:

We found a problem with some content in filename.xlsx. Do you want us to try and recover as much as we can? If you trust the source of this workbook, click Yes.

I have stripped back everything to the following code. The template file that I am opening is a brand new excel file, with no edits made to it (to avoid the potential that the error already exists in the template). I can open this file from the drive without any issues.

$spreadsheet = IOFactory::load(storage_path() ."\Template - English.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.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 = IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save('php://output');

Once I go through the repair process I get the following message from Excel, and everything seems to work fine.

Excel completed file level validation and repair. Some parts of this workbook may have been repaired or discarded.

**EDIT: ** The same error occurs when I generate a new file using $spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();

like image 783
alex_6169 Avatar asked Sep 07 '17 09:09

alex_6169


1 Answers

I don't know if you solved your issue but I had the same. My code looks like this :

$strFilename = sprintf('%s_%s_subscriptions', date('Y-m-d-H-i'), $alias);
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="'.$strFilename.'.xlsx"');
header('Cache-Control: max-age=0');
$writer = IOFactory::createWriter($objSpreadsheet, 'Xlsx');
$writer->save('php://output');

And Excel prompt the same error as you. But it looks like PHPSpreadSheet create a buffer and doesn't close it once you save the spreadsheet. By adding a "die;" after the final line, it solved the issue...

So final code :

$strFilename = sprintf('%s_%s_subscriptions', date('Y-m-d-H-i'), $alias);
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="'.$strFilename.'.xlsx"');
header('Cache-Control: max-age=0');
$writer = IOFactory::createWriter($objSpreadsheet, 'Xlsx');
$writer->save('php://output');
die;

Hope it helps !

like image 166
Lupus Avatar answered Oct 20 '22 04:10

Lupus