LoginSignup
3
2

More than 3 years have passed since last update.

Laravel Excel で 出力したxlsxファイルが開けない時の対処法

Posted at

問題

Laravel Excelで出力したxlsxファイルをExcelで開くと

ファイル形式またはファイル拡張子が正しくありません。
ファイルが破損しておらず、ファイル拡張子とファイル形式が一致していることを確認してください

とエラーが出て開けませんでした。

環境

Laravel 6.6
Laravel-excel 3.1

対処法

公式にも同様のissueが上がっており解決策がありました

<?phpが残っていたり先頭行に空白があると該当のエラーが出るそうです。

Hi everyone,

I spend my day to understand this unworkable download function. PHPSpreadSheet seems to use PHP buffer. And depending on your PHP configuration some warnings or errors could interfer with the output.

LaravelExcelで使われているPHPSpreadSheetのバッファを使用しており、構成によっては出力に影響を与えるとのことです。

示していただけたコードがこちら。

// in controller
ob_end_clean(); // this
ob_start(); // and this
return Excel::download(new MyExport, 'file.xls');

ずっと開けなかったのですが、開けました。

参考

本当に感謝です
https://github.com/Maatwebsite/Laravel-Excel/issues/1673

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2