10

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

alex_6169
  • 135
  • 1
  • 1
  • 6
  • Is there anything, any other output that your script is generating? Any whitespace that might be sent to php://output in addition to the spreadsheet binary? Any error messages that might be embedded in it? Open the file in a text editor and look for bom headers, whitespace, plaintext PHP messages, html markup, etc that shouldn't be there – Mark Baker Sep 07 '17 at 14:48

4 Answers4

32

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 !

Lupus
  • 331
  • 3
  • 4
  • 1
    This is the absolute solution!! The main problem is that you can notice this bug until you try to read the same file you modify/save previuosly :( – PrestaAlba May 31 '20 at 16:06
  • 2
    Uhm, `die;` does it. `exit;` works the same. – lin Nov 17 '21 at 12:11
  • I cannot see the difference between 2 codes. – Dimitrios Desyllas Dec 08 '21 at 11:23
  • I just had the same problem and realised (which is why the die probably works - as it kills off all further output) that it was because I had stupidly left some whitespace outside the PHP tags in the script. This was enough to fall into the file content and corrupt it. Just make sure your PHP end tag '?>' is the last character in your script. – Arthur Nicoll Dec 15 '22 at 17:00
2

Just use exit() at the end.

ob_end_clean();
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save('php://output');
exit();
Nazmul Haque
  • 720
  • 8
  • 13
0

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

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();
MGLeon
  • 37
  • 7