[SalesForce] Excel generated from VIsualforce page is not opening in Mobile

I have generated a excel from a VF page as below. When I download it in Desktop and open it through excel it launches fine in MS Excel except that it throws an error-"File you are trying to open – SalesforceExport.xls is in a different format than specified by the file extension.Do you want to open the file now". When I try to open the same page in Mobile, it downloads the file but when I try to open it in excel it says-"Something went wrong and your file can't be opened now. please try again". I am using MS Excel app to open it in Mobile.Please advise

<apex:page controller="ReportService" contentType="application/vnd.ms-excel#SalesForceExport.xls" cache="true">
<apex:pageBlock >
<apex:pageBlockTable value="{!cfrSummaryReport}" var="report">
    <apex:column value="{!report.QuoteNum__c}"/>
    <apex:column value="{!report.Status__c}"/>
    <apex:column value="{!report.OrderNum__c }"/>             
</apex:pageBlockTable>
</apex:pageBlock>

Best Answer

While the documentation in How to export data in Excel or PDF format using Visualforce says to create an Excel file from a Visualforce page all you need to do is set the contentType to application/vnd.ms-excel, it isn't really telling the whole story.

That will indeed prompt the browser to treat the page content as an Excel spreadsheet.

However, the content is actually still HTML (Excel Web Page support). If you save the results and open it in the text editor of your choice you will see that it starts with a <script> tag.

enter image description here

An actual xlsx file is actually a zip file, so it would start with the zip PK header.

Excel on the desktop is tolerant enough (with the prompt) to read the HTML in as a spreadsheet.

The mobile version your using doesn't appear to support this.

I'd suggest exporting as a CSV in this case, as the support will be more universal.


As you commented. The Knowledge Article Content type XLS not supported in Mobile browsers covers the same points.

Related Topic