PhpSpreadsheet is corrupting files

15.4k views Asked by At

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();

4

There are 4 answers

4
Lupus On

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 !

1
Nazmul Haque On

Just use exit() at the end.

ob_end_clean();
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save('php://output');
exit();
0
Marcin Chojnacki On

For me, the problem was generated by the dump () function, which added debug data to "php: // output" during export, the data was interpreted by PHPSpreadshet as part of the excel file

After removing everything worked

0
MGLeon On

What worked for me was clean the buffer after saving the file.

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