A cfspreadsheet question: I have an app that gener...
# cfml-general
p
A cfspreadsheet question: I have an app that generates complex spreadsheets and streams them to the user’s browser in .xlsx format. The resulting spreadsheets used to work in Excel but now error (invalid file format or extension) due (I think) to extension hardening. Is there a way to generate “real” .xlsx spreadsheets out of CF that Excel will accept?
m
I'd say that likely depends on how you are generating said spreadsheets in CF.
m
There are a few ways that i have seen excel spreadsheets created in ColdFusion 1.) Create HTML table and tell it is an XLS file • This one I don't tend to like because it is not really creating an actual excel spreadsheet. 2.) Create a Complex XML file consisting of a xmlns of urnschemas microsoft comoffice:spreadsheet • This allows formatting and it is an excel-ish file 3.) Create a zip file that is then renamed to xlsx instead of zip inside of this file you will see the formatting. • This is a real file you can even take an XLSX and rename it to zip to browse inside it if you want to understand how it is laid out
p
They are created using SpreadsheetNew(sheet_name,"true"). They are written to using only ColdFusion spreadsheet functions and formatting. They are streamed to the browser using cfheader and cfcontent with SpreadsheetReadBinary(theSpreadsheetObject). Does this help?
m
what do your cfheader and cfcontent tags look like?
This is a sample of mine:
<cfcontent type="application/vnd.ms-excel" reset="true">
<cfheader name="Content-Disposition" value="attachment; filename=report#GetTicKCount()#.xls">
p
<cfheader name="Content-disposition" value="attachment; filename=#spreadheetName#"> <cfcontent type="application/vnd.ms-excel" variable="#SpreadsheetReadBinary(theSpreadsheetObject)#" reset="true"> works fine with .xls but not with .xlsx
c
With .xlsx I use: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
p
I have tried application/vnd.openxmlformats-officedocument.spreadsheetml.sheet as well. It will stream to Excel but then error.
c
What did the "extension hardening" you mention involve?
p
My understanding is that Microsoft "hardened" the file handling in the office apps so that files whose internal format does not match the file name extension can no longer be opened in those apps. The CF .xlsx file are not "real" xlsx files but and the mime type above suggests Open office xml formats that Excel no longer uses. https://support.microsoft.com/en-us/office/a-file-is-in-a-different-format-than-its-ext[…]2dfb3f-b5a9-4a71-9712-0084672921a7?ui=en-us&amp;rs=en-us&amp;ad=us
c
I don't use ACF but I doubt it would be generating invalid .xlsx files. To rule that out though, you might want to compare with https://github.com/cfsimplicity/spreadsheet-cfml. Like ACF it uses the POI java library, but unlike ACF it's on the latest version There's a handy https://github.com/cfsimplicity/spreadsheet-cfml/wiki/download function.
Just fired up an ACF2021 commandbox instance, generated and downloaded an .xlsx and it opened up fine for me in Excel:
Copy code
wb = SpreadsheetNew( "test", true );
SpreadsheetAddRow( wb, "one,two" );
bin = SpreadsheetReadBinary( wb );
cfheader( name="Content-Disposition", value='attachment; filename="test.xlsx"' );
cfcontent( type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", variable="#bin#", reset="true" );
p
Thanks. Very interesting. I have been running very similar code in ACF 2021 but getting errors every time. I will hook up your example and see what I get.
j
Here's 2-cents worth of possibility: If you're using IIS, does it allow the .xlsx extension in the MIME types settings? This is my MIME type associated with xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" (in CF2016, that is).
p
The example above example did not error, although it did not launch Excel on the system I tested it on. There are some minor code difference that I will attempt to apply and see what happens. The help is much appreciated.
c
The
attachment
content-disposition suggests not launching an application to open it, just downloading the file. But it will depend on your browser's mime handling config, i.e. what the browser is set to do with an .xlsx file.
p
Yep. Config specific to the box. The only real difference is that your code reads the content into a variable and runs in script as well as the complexity of the spreadsheet produced. But I can see right away that even in Office Libra (office replacement suite) that the file type is correct which it was not. I am about to test it with the full complex spreadsheets.
And.. it works. Excellent. Many thanks.
👍 2