spec/layout/xls.cfm
2025-06-02 16:16:51 +03:00

55 lines
2.6 KiB
Plaintext

<!---https://www.bennadel.com/blog/461-creating-microsoft-excel-documents-with-coldfusion-and-xml.htm--->
<!---*** locale-specific--->
<cfparam name="ATTRIBUTES.qRead" type="query"/>
<cfparam name="ATTRIBUTES.titleMap" type="struct"/>
<cfparam name="ATTRIBUTES.sheetTitle" type="string" default="Sheet1"/>
<cfparam name="ATTRIBUTES.filename" type="string" default="export.xls"/>
<cfset fieldArray=structSort(ATTRIBUTES.titleMap, "numeric", "ASC", "ordinal")/>
<cfsavecontent variable="strXmlData">
<cfoutput>
<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:x='urn:schemas-microsoft-com:office:excel' xmlns='http://www.w3.org/TR/REC-html40'><meta http-equiv="content-type" content="application/vnd.ms-excel; charset=UTF-8"><meta http-equiv="content-type" content="application/vnd.ms-excel; charset=UTF-8"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>#ATTRIBUTES.sheetTitle#</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table><!---
<cfloop index="i" from="1" to=#structCount(ATTRIBUTES.titleMap)#>
<Column ss:Index="#i#"/>
</cfloop>
--->
<tr>
<cfloop from=1 to=#arrayLen(fieldArray)# index="i">
<cfset title=structFind(ATTRIBUTES.titleMap, fieldArray[i]).title/>
<th><cfif len(title)>#title#<cfelse>#fieldArray[i]#</cfif></th>
</cfloop>
</tr>
<cfloop query=#ATTRIBUTES.qRead#>
<tr>
<cfloop from=1 to=#arrayLen(fieldArray)# index="i">
<cfset data=#ATTRIBUTES.qRead[fieldArray[i]]#/>
<cftry>
<cfif isDate(data)><td>#dateFormat(data,'YYYY-MM-DD')#<cfif hour(data) GT 0 OR minute(data) EQ 0> #timeFormat(data,'HH:mm')#</cfif></td><cfelseif isNumeric(data)><td>#Replace(data, '.', ',')#</td><cfelse><td>#data#</td></cfif>
<cfcatch type="Any"><td>#data#</td></cfcatch>
</cftry>
</cfloop>
</tr>
</cfloop>
</table></body></html>
</cfoutput>
</cfsavecontent>
<cfheader
name="content-disposition"
value="attachment; filename=#ATTRIBUTES.filename#"
/>
<!---
When streaming the Excel XML data, trim the data and
replace all the inter-tag white space. No need to stream
any more content than we have to.
--->
<cfcontent
type="application/msexcel"
variable="#ToBinary( ToBase64( strXmlData.Trim().ReplaceAll( '>\s+', '>' ).ReplaceAll( '\s+<', '<' ) ) )#"
/><cfabort/>
<cfexit method="exittag"/>