Error while exporting to excel - File format and extension doesn't match
Copy link to clipboard
Copied
Hi,
I have the below code to generate output to excel. But when i open the excel after downloading, it shows that "file format and extension doesn't match". Please refer the screenshot and generated excel attached.
<cfheader name="content-disposition" VALUE="attachment; filename=sitereport.xls">
<cfset type="application/msexcel">
Is it because it is .xls file and I am opening in office 365 enterprise (where the extension should be .xlsx)? Attached the excel file also for reference.
regards,
Sunil
Copy link to clipboard
Copied
Attached the excel file. Remove .txt from the file name.
Copy link to clipboard
Copied
The value application/msexcel is incorrect. The correct MIME types for Excel are:
- application/vnd.ms-excel (for XLS)
- application/vnd.openxmlformats-officedocument.spreadsheetml.sheet (for XLSX)
Copy link to clipboard
Copied
Example:
<cfheader name="Content-Disposition" value="attachment; filename=MySales.xls">
<cfcontent type="application/vnd.ms-excel">
<table border="1">
<tr><td>Month</td><td>Quantity</td><td>$ Sales</td></tr>
<tr><td>January</td><td>80</td><td >245</td></tr>
<tr><td>February</td><td>100</td><td>699</td></tr>
<tr><td>March</td><td>230</td><td >2036</td></tr>
<tr><td>Total</td><td>=Sum(B2..B4)</td><td>=Sum(C2..C4)</td></tr>
</table>
You may then ignore Microsoft Office's prompt that the file you are trying to open is in a format different from the specified file extension.
Copy link to clipboard
Copied
I used your code but the issue still persist. We can ignore the prompt, but the client doesn't want this message as popup nor they are asking to find a solution to avoid this prompt.
Copy link to clipboard
Copied
I don't understand the logic in your last reply here to bkbk. Can you restate differently, "We can ignore the prompt, but the client doesn't want this message as popup nor they are asking to find a solution to avoid this prompt."
/Charlie (troubleshooter, carehart. org)
Copy link to clipboard
Copied
While you've attached the screenshot, I'm not seeing the xls, at least on mobile. I also don't see it in your follow-up message where you seem to have realized you'd not attached it. Bkbk, are you seeing it?
In any case, I have some questions:
- How did you create the xls file? If in cfml, using what tag(s)? Or was it created outside of cfml?
- Is this process one that worked previously? If so, what changed?
- What cf version and update are you on? You can view this via the variable, server.coldfusion.productversion
/Charlie (troubleshooter, carehart. org)
Copy link to clipboard
Copied
I am not able to attach the excel file. I renamed to .txt but still it failed in upload.
- The excel file is created outside and saved in the same folder. While running the code (code pasted in the first thread), it output the file in browser.
- No change, it was giving the same issue (prompt alert)
- cf version = 2018,0,19,330149
Copy link to clipboard
Copied
Still not clear. Did the code ever work for any excel file? Does it work for some and not others? Did it work for some cf version before your cf2018 update 19?
Also, even if you can't upload the file in this forum, you could put it on something like Dropbox, Google drive, one drive, or otherwise and give us the link. It may make it easier for us to come up with a solution, if it's at all possible for that file.
Finally, as you may know, cf2018 is no longer updated since July 2023. If the cost of licensing cf2023 is an issue (because the 50% upgrade discount is only for those on cf2021), note that there's a special offer for a few more weeks where those on cf2018, cf2016, or earlier can buy a cf license at a 25% discount. More at https://www.carehart.org/blog/2024/7/8/limited_time_upgrade_discount_to_CF2023_from_older_releases
/Charlie (troubleshooter, carehart. org)
Copy link to clipboard
Copied
Please find the excel file. Just open the excel file, you can see the prompt error/warning. If you click on Yes, it will load the records.
Copy link to clipboard
Copied
"Please find it" where?
And please respond to other questions I've asked in this thread and your earlier one with bkbk here. Help us help you. We don't work for Adobe but offer our time for free.
/Charlie (troubleshooter, carehart. org)
Copy link to clipboard
Copied
Please run the code pasted by BKBK, it will output an excel. While opening the excel it will show you that prompt.
Copy link to clipboard
Copied
Please run the code pasted by BKBK, it will output an excel. While opening the excel it will show you that prompt. Tried multiple times uploading excel sheet, it is not being uploaded. Not sure whether there is any restriction to excel file upload here in the forum.
Copy link to clipboard
Copied
A few things:
1) I'm reading this on a phone. As such I can't run the code currently. And I'd rather write you while the following questions remain.
2) Running his code won't answer the questions I asked you above:
"Did the code ever work for any excel file? Does it work for some and not others? Did it work for some cf version before your cf2018 update 19?"
And by "work" I mean without the prompt you care to avoid?
Or are you saying you've never done this before? And that it doesn't matter if it ever worked before. You just want to know how to get it to work without the prompt now?
If so, why did you say of your client "nor [are] they asking to find a solution to avoid this prompt"? Now it seems they DO want you to avoid the prompt. Again we can only go on your words. I asked for this clarification previously and you didn't reply, even when I asked again.
3) it may be important to state that running his code is not the same as the situation you described. He's creating an excel by outputting a table of html. You said you'd been given a spreadsheet that you now wanted to serve.
4) You keep saying you can't upload a file to this forum, and I have said "you could put it on something like Dropbox, Google drive, one drive, or otherwise and give us the link", and I reminded you of that in my last reply.
If you're thinking this would happen for any excel file, have you proven it? I suppose you want us to do that.
5) There ARE indeed other ways to serve a spreadsheet to your users which need not involve cfcontent. While we try things for you and await your reply to these I now ask, have you at least considered other options? Or is getting this working to your satisfaction the only solution you'll accept?
/Charlie (troubleshooter, carehart. org)
Copy link to clipboard
Copied
@sunilk86642652 , there is nothing wrong with that prompt that you see,
"The file you are trying to open xxx.xls is in a different format than specified by the file extension. Verify that the file is not corrupted and is from a trusted source before opening the file. Do you want to open the file now"
That is so by design. It is a security check that recent versions of Excel have.
In our case, ColdFusion generates the spreadsheet from HTML. So the resulting file is not native Excel, hence the prompt. In displaying the prompt, Excel is actually doing the user a favour. It ensures that the user won't inadvertently open a corrupted file or, worse, a malicious file.
You just have to click on Yes for Excel to open the file. However, you can configure Excel to no longer display the prompt, and open the file directly. One way to do so is through the setting ExtensionHardening in Windows Registry. See this Excel 2007 ExtensionHardening example. As I have said, you will be taking a security risk when you disable the prompt.

