In a ColdFusion application I need to make it possible for users to save a spreadsheet to their hard drives.
I have been able to generate files on the fly by building them as XML or HTML tables, applying the application/vnd.ms-excel
MIME type, and saving them with a .xls
extension.
<cfheader name="Content-Disposition" value="attachment; filename=MySpreadsheet.xls">
<cfcontent type="application/vnd.ms-excel">
But when users load such a file, they invariably get an annoying message (due to Excel's Extension Hardening Feature):
The file you are trying to open, '_____.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?
I would prefer to avoid this message.
I understand the <cfspreadsheet>
tag and SpreadsheetWrite()
functions will generate a proper Excel file that does not display this message -- but they both require an argument instructing where on the server the file is to be written. Because the download might contain sensitive information, and because I cannot easily enforce security once a file is written to the server, I do not want to create such a file -- even temporarily. (I know that I can provide a password, but this is not an ideal solution in the context of the business process in which the Excel file will be used after it is generated.)
How can I generate the Excel file and prompt for download without first writing it to a file?