Excel cannot open the file because the file format or file extension is not valid - PHP Maatwebsite

0 votes

We have several capabilities in our backend project that enable file export to xlsx. We tested everything out locally, and everything is functioning well. However, I installed the app on an Azure App Service, and some (not all) of the exports aren't functioning properly.

I set up the database (MySQL), frontend (React), and backend (PHP) in Azure. Because we don't want to waste time with the data, I moved the data from the local database to the database that was deployed in Azure.

For the export-related functionalities in the backend project, we use Laravel with Maatwebsite. I developed a class with construct, query, and headings functions as well as implementations of FromQuery, WithHeading, and ShouldAutoSize.

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 of my exports function as intended, however, occasionally I receive the message "Excel cannot open the file because the file format or file extension is not correct." I attempt to open the file.

I've been reading about the problem, and some people suggest that I use ob start() (at the start of the class) and ob end clean() (right before exporting the file), however when I do this, the web app returns a 404 error.

It's odd because the file opens without any issues when I add a filter to the data for exporting (for example, downloading only the data of persons in a specified city) (I can do it in the frontend app). I was able to download all the data in different batches using this approach, therefore I don't believe that the problem is due to unusual characters in the data.

Does anybody have any suggestions?

Dec 19, 2022 in Others by Kithuzzz
• 28,900 points
119 views

1 answer to this question.

0 votes

Try this:

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

I hope this helps you.

answered Dec 19, 2022 by narikkadan
• 53,520 points

Related Questions In Others

0 votes
1 answer

PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: (Open Excel File) in Python

Try this: import os import shutil dirpath = os.path.join('C:/Path/Folder', 'Folder') if ...READ MORE

answered Jan 15 in Others by narikkadan
• 53,520 points
196 views
0 votes
0 answers
0 votes
0 answers

Azure DevOps PDF Export Pulgin Suggestion

I built a job using Jenkin and ...READ MORE

Mar 15, 2022 in DevOps Tools by Kichu
• 19,050 points
218 views
0 votes
1 answer
0 votes
1 answer

Export DataTable to Excel File

Add Interop References. First we need to ...READ MORE

answered Jun 9, 2022 in JQuery by gaurav
• 22,960 points
220 views
0 votes
1 answer

Removing specific rows in an Excel file using Azure Data Factory

Under the 'Source' tab, choose the number ...READ MORE

answered Sep 23, 2022 in Others by narikkadan
• 53,520 points
597 views
0 votes
1 answer

PHPExcel file cannot open file because the file format or file extension is not valid

Just change the code to this: // Save Excel ...READ MORE

answered Nov 24, 2022 in Others by narikkadan
• 53,520 points
745 views
0 votes
1 answer

Blazor - ClosedXml - excel cannot open the file beacause the file format or extension is not valid

Changed the following code: function BlazorDownloadFile(filename, bytesBase64) { var ...READ MORE

answered Jan 20 in Others by narikkadan
• 53,520 points
62 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP