10

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?

itsbalur
  • 992
  • 3
  • 17
  • 39
  • Does anything change if you specify "xlsx" as the type? – Mike Woodhouse May 18 '11 at 09:14
  • @Mike:It's worse, Excel wouldn't then provide an option to open. It just displays a prompt that the file extension is not valid, with only an OK button. May be its worthwhile to mention that I saved the XSL template as type "XML Spreadsheet 2003" – itsbalur May 18 '11 at 09:44
  • Is xlsx not a zipped xml? Did you try zipping your result? – pintxo May 18 '11 at 09:50
  • @cmmi xlsx is actually a zipped directory structure, containing various folders & files, including one XML file per worksheet. – Mike Woodhouse May 19 '11 at 08:29

1 Answers1

7

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.

Samuel Liew
  • 76,741
  • 107
  • 159
  • 260
itsbalur
  • 992
  • 3
  • 17
  • 39