4

I'm trying to do a simple task in ColdFusion: Generate an excel file and have it downloaded in the browser. I've got this:

<cfset local.sheet = SpreadsheetNew("My Spreadsheet", "true") />
<cfset SpreadsheetAddRow(local.sheet, "Col1,Col2,Col3") />

<cfheader name="content-disposition" value="attachment;filename=NiceName.xlsx" />
<cfcontent type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" variable="#spreadsheetReadBinary(local.sheet)#" reset="true" />

Which did work perfectly on CF9, however it doesn't on CF11. When opening the file, it says:

Die Datei 'NiceName.xlsx' kann von Excel nicht geöffnet werden, da das Dateiformat oder die Dateierweiterung ungültig ist. Überprüfen Sie, ob die Datei beschädigt ist und ob die Dateierweiterung dem Dateiformat entspricht.

Which translates to something like:

Excel cannot open the file 'NiceName.xlsx', because either the file-format or -ending is invalid. Please verify, if the file is corrupted and wether the file-ending corresponds to the file-format.


I can:

  • <cfmailparam> the sheet and receive the file via mail.
  • <cfspreadsheet action="write"> the sheet and receive the file on the server.

In both cases, the files are readable.


I have tried:

  • <cfspreadsheet action="write"> the sheet, and then use <cfcontent file="#pathToFile#">, which doesn't work.
  • Checked my IIS for URL-Rewrite-Rules.
  • Checked my IIS for the .xlsx MIME-Type. It's the same as in my code.
  • Added a <cfabort> at the end, so nothing else would happen somehow.

But nothing helped.


I'm using:

  • ColdFusion 11 (11,0,0,289974) with Tomcat 7.0.52.0
  • Windows Server 2008 R2 Standard 64-bit
  • Microsoft Office 2013
  • IIS 7.5.7600.16385
James A Mohler
  • 11,060
  • 15
  • 46
  • 72
Boris
  • 577
  • 4
  • 15
  • Boris, The only thing I can see is that your "type" is odd to me. I usually see something like application/excel for type. You are using an openoffice document type. I'm not sure that's going to cut it. – Mark A Kruger Dec 03 '14 at 17:30
  • 2
    Sam code works for me with CF11 on IIS 7/7.5 – Anit Kumar Dec 03 '14 at 17:33
  • My guess is that tomcat is passing the data off to IIS incorrectly. Can you stream other type of content like an image to the browser? – Twillen Dec 03 '14 at 20:12
  • @MarkAKruger - No, since he is using `xmlformat=true`, that is the correct [mime type](http://technet.microsoft.com/en-us/library/ee309278%28office.12%29.aspx). The problem is something else. (Edit) Never mind. I see Twillen already mentioned this. – Leigh Dec 03 '14 at 20:57
  • Boris, your example code also works fine for me. Are you saying *that exact code* does *not* work for you? Or is that perhaps an example which actually inadvertently doesn't include whatever it is that gives you the problem? Possibly (but not likely) is that I'm running ColdFusion 11,0,02,291725, whereas you seem to be unpatched..? Which version were you running @AnitKumar? – Adam Cameron Dec 04 '14 at 08:56
  • I am using CF 11,0,01,291346 @Adam – Anit Kumar Dec 04 '14 at 11:49
  • I could stream a .txt file with MIME-type text/plain to the browser. That worked. – Boris Dec 04 '14 at 12:20
  • The .txt file was in the same directory as the .xls file. (Note: The files are outside of the webroot) – Boris Dec 04 '14 at 12:56
  • Will speak to my server admin and post results asap – Boris Dec 04 '14 at 13:29
  • Just out of morbid curiosity, I wonder if creating a `.xls` would work instead. – James A Mohler Dec 04 '14 at 15:50
  • No, creating an .xls didn't work either, tested with all MIME-Type variations. Will try `application/octet-stream` tomorrow and then maybe update the server.. Thanks for all the ideas so far! – Boris Dec 04 '14 at 17:27

1 Answers1

0

Try using application/vnd.ms-excel instead of the openoffice variety. This is going to be an excel specific mime type. Folks using openoffice will convert automatically because they have it set up that way. That's the only thing I see here.

Mark A Kruger
  • 7,183
  • 20
  • 21
  • The mime type is the correct one. `application/vnd.ms-excel` is for xls files, and `application/vnd.openxmlformats-officedocument.spreadsheetml.sheet` is for xlsx [blog.msdn.com](http://blogs.msdn.com/b/vsofficedeveloper/archive/2008/05/08/office-2007-open-xml-mime-types.aspx) – Twillen Dec 03 '14 at 19:58
  • I'd try it anyway :) or try application/octet-stream – Mark A Kruger Dec 03 '14 at 20:00
  • Tried other MIME-Types as well. With xls and xlsx. But no variation worked for me. – Boris Dec 04 '14 at 12:19