1

Using PHPSpreadsheet saving XLSX format works OK running the default code

$writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet); 
$writer->save("filename.xlsx");

But if I want to have the user to select the target directory using

header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="filename.xlsx"');
header('Cache-Control: max-age=0');
$writer = IOFactory::createWriter($spreadSheet, 'Xlsx');
$writer->save('php://output');

The file saves OK but Excel 2016 does not want to open it. Excel returns the following error Excel Error

I looked through all documentation and posts but cannot find the solution.

Thanks !

Edit: Just in case, this solution does not work for me.

Edit 2: The sample provided Simple Download Xlsx works perfectly, but when doing a copy/paste for my spreadsheet, Chrome gives me a Resource interpreted as Document but transferred with MIME type application/octet-stream

Edit 3: Used ob_end_flush(); to clean any left over header in my code. The file now saves OK, but needs repair when opening in Excel. Why ?

Thanks

2
  • What triggers this php? is it an ajax call, a regular submit or other? Commented Feb 6, 2019 at 8:40
  • 1
    It was a regular submit, but the file had 2 blank lines after teh last ?> which triggered the error. (as explained in the phpspreadsheet recipes....) Commented Feb 13, 2019 at 13:48

1 Answer 1

0

Solution:

Bug from PhpSpreadsheet.

When using header("Content-Type: application/vnd.ms-excel");

i.e. compatibility mode for Excel, the file opens OK.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.