0

In our backend project, we have some features that allows to export files to xlsx.

We have done some tests locally and everything is working fine. However, I deployed the app in an Azure App Service and some of the exports (not all) are not working fine.

I deployed backend (PHP), frontend (React) and database (MySQL) in Azure. I copied the information of the local DB to the DB deployed in azure (just because we don't wanna waste time with the data).

In the backend project, we are using Laravel with Maatwebsite for the features related to the exports. I created a class that implements FromQuery, WithHeading and ShouldAutoSize and that has construct, query and headings functions. Next, in a Controller, I use this structure to get the file:

public function functionName(Request $request){
  $fileName= 'name'.time().'.xlsx';
  return (new ClassThatHasTheLogicOfExporting($request->all()))->download($fileName);
}

All my exports works like that, but I'm getting the following error in some of them:

"Excel cannot open the file because the file format or file extension is not valid"

When I try to open the file.

I've been reading about the issue and some people says that I should use ob_start() (at the beginning of the class) and ob_end_clean() (just before exporting the file) but when I do this, I get a 404 error in the web app.

A curious thing is that when I add a filter to the data for exporting (for instance, downloading only the data of people in a specific city) (I can do it in the frontend app), the file opens without errors.

With this strategy, I've achieved downloading all the data in separate groups, so I don't think that the issue is related to special characters in the data.

Does anybody have any suggestion?

I'm using maatwebsite/excel 3.1 and PHP 8 with Laravel 8

OMi Shah
  • 5,768
  • 3
  • 25
  • 34
  • Does this answer your question? [Laravel Excel is working but the file cannot be opened](https://stackoverflow.com/questions/67609038/laravel-excel-is-working-but-the-file-cannot-be-opened) – OMi Shah Aug 30 '23 at 08:54

2 Answers2

0

I believe the error statement correctly suggests the error is being caused due to an invalid extension. Don’t worry there are several workarounds to this, I am sharing them below:

Method 1 – Change the Default File Format to Save Excel Workbooks

In Excel 2007, click the ‘Microsoft Office’ button and then ‘Excel Options’. In Excel 2010 and higher versions, click File > Options.

Select the Export option.

Select the Change File Type option.

Change the file extension, and then click Save As.

Method 2 – Recover Unsaved Workbook Go to File and select the Info option.

Under Manage Versions, select ‘Recover Unsaved Workbooks’ op

If MS Excel has unsaved files then, it will list them. You can open and save it.

Method 3 – Use the' Open and Repair' Feature of MS Excel

Open MS Excel application.

Go to File and select the Open option.

Select the corrupt file and choose the Open and Repair option.

You can also check this forum:

https://social.technet.microsoft.com/forums/office/en-US/63020ccc-51d7-46d9-b956-121c0e6efcc8/excel-file-error-the-file-format-and-extension-dont-match?forum=Office2016ITPro

Best Regards,

Steve

Steve More
  • 26
  • 1
0

you can add the below


 if (ob_get_length() == 0 ) {
  ob_start();
  $response = Excel::download(new KingdomsExport, 'kingdoms.xlsx',\Maatwebsite\Excel\Excel::XLSX);
   return $response; 
        }