Open spreadsheet xml in Excel by default

itsbalur picture itsbalur · May 18, 2011 · Viewed 13.2k times · Source

In a web application, I am generating a spreadsheet XML using an XSL template created from Excel 2010. I want this spreadsheet XML to open in Excel by default. So, I add the below properties to the response

Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", "Attachment;Filename=export.xls");
Response.Charset = "";

This opens the file in Excel, but because of the extension hardening feature of Excel 2010, displays the prompt -

The file you are trying to open, 'export[1].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?

If I click on yes, it opens up fine. But, is there a way the prompt can be completely skipped? I have read about the registry changes to disable the prompt in a user's machine. But, this is a public website and that solution will not work.

I can set the attachment filename to export.xml. If I save it and open in Excel, there is no prompt. But the XML will not open in Excel by default.

Any ideas, to open the spreadsheet xml in Excel, without the prompt?

Answer

itsbalur picture itsbalur · May 19, 2011

This blog helped me workaround my problem.

https://ralph.blog.imixs.com/2013/02/16/how-to-generate-a-excel-sheet-with-xslt/

The trick is to add this processing instruction in your XSL template in the way mentioned in the above post.

<xsl:processing-instruction name="mso-application">   
<xsl:text>progid="Excel.Sheet"</xsl:text>  
</xsl:processing-instruction>

By doing this, you can specify the file extension as .xml, but it will open in Excel by default.