How to generate excel file (xls) and save it to a server location without downloading it using ColdFusion

506 Views Asked by At

We have a page that is generating dynamic content and format is output based on user's selection.

For example, if a user selected HTML format then, page comes as HTML page. That works fine as well as PDF format.

The challenge is, if a user selecting Excel format and we require Excel files to save to the server.

When a user selecting Excel format, the file is generating in excel just fine but, instead of saving it to the server the user is getting Save As dialog box but, as I mentioned we need the file to save to the server.

Any input is greatly appreciated. We are using CF 2016.

The code sample:

<cfsavecontent variable="Page"> 
    <cfif CompareNoCase(FORM.format,"xls")>
       <cfsetting enablecfoutputonly="Yes">
       <cfheader name="Content-Disposition" value="attachment;filename=""#VARIABLES.vcFilename#""">
       <cfcontent type="application/vnd.msexcel">
    <cfif>
    <table><tr><td><cfoutput>#somecontent#</cfoutput></td></tr>...</table>
</cfsavecontent>
<cfif CompareNoCase(FORM.format,"xls")>
   <cffile 
            action="write" 
            destination="d:\somedir\"
            file="#VARIABLES.vcFileName#" 
            output="#Page#" 
            nameconflict="overwrite">
<cfif>
1

There are 1 best solutions below

5
user1706426 On

As mentioned by SOS in the comments his suggestion to remove headers did the trick. Also, in order to save excel file (.xls) added following to cffile: accept="application/vnd.ms-excel,application/xls"