2

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>
rrk
  • 15,677
  • 4
  • 29
  • 45
user1706426
  • 387
  • 1
  • 3
  • 12

1 Answers1

0

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"

user1706426
  • 387
  • 1
  • 3
  • 12
  • 1
    Using html and saving the page as `xls` does not really work if you really want a valid excel file. You can look into [``](https://cfdocs.org/cfspreadsheet) and [`SpreadSheetNew`](https://helpx.adobe.com/coldfusion/cfml-reference/coldfusion-functions/functions-s/spreadsheetnew.html) and build on it. – rrk Sep 10 '20 at 15:02
  • actually it worked when i added to cffile accept="application/vnd.ms-excel,application/xls" i dont need any extra functionality in the generated files, just basic excel – user1706426 Sep 11 '20 at 13:06
  • No, that attribute only applies to *uploading*. Jas nothing to do with saving/writing files. Technically what you're generating is html. You can give the file an .XLS extension, but the contents are still html, not a valid Excel file. Most versions of Excel will warn you about that when you try an open the file, i.e. that the content doesn't match the claimed header. – SOS Sep 11 '20 at 21:26
  • Actually, we are not after valid excel, that indeed is html file with tab as delimiters and saved with xls extension. We tried to write file with xls extension and it did not work. It worked for csv, txt files. But, we need xls. I read the docs and i know about accept="application/vnd.." is not for write action but, I tried and it worked. Files are generated and saved on the server with xls ext. When I open them, all the formatting is working as expected – user1706426 Sep 15 '20 at 12:39
  • For writes you can specify just about any file name you want. If it failed, it's probably because the cffile code has the wrong attributes. It shouldn't be using `destination`. That's only for uploads. – SOS Sep 15 '20 at 18:53