coldfusioncoldfusion-11cfhttp

Export to Excel in ColdFusion


I am posting few inputs into a url using cfhttp and expecting to download some data in a xls file. I am trying to do get data using cffile ="write" which doesn't work. Can any one suggest how can we go with this. Here is the code below

<cfhttp url="#Baseurl#" method="post" result="ExportToExcelresult" redirect="no" resolveurl="true">
    <cfhttpparam type="header" name="REFERER" value="#Baseurl#" >
    <cfhttpparam type="header" name="Cache-Control" value="no-cache">
    <cfhttpparam type="header" name="Content-Type" value="application/x-www-form-urlencoded">
    <cfhttpparam type="header" name="Connection" value="keep-alive" >
    <cfhttpparam type="header" name="User-Agent" value="Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) (KHTML, like Gecko) Chrome/27.0.1453.110 Safari/537.36">
    <cfhttpparam type="header" name="cookie" value="TestCookie=;" encoded="yes">

    <cfloop collection="#CookieList#" item="i">
            <cfhttpparam type="header" name="cookie" value="#CookieList[i]#" encoded="yes">
    </cfloop>
    <cfloop collection="#PostCookieList#" item="i">
            <cfhttpparam type="header" name="cookie" value="#PostCookieList[i]#" encoded="yes">
    </cfloop>

    <cfloop collection="#PostDefaultCookieList#" item="i">
            <cfhttpparam type="header" name="cookie" value="#PostDefaultCookieList[i]#" encoded="yes">
    </cfloop>


    <cfhttpparam name="ToolkitScriptManager1_HiddenField" value="" type="formfield">
    <cfhttpparam name="LeftNav1_LoginView1_treeView1_ExpandState" value="#EXPANDSTATE#" type="formfield">
    <cfhttpparam name="LeftNav1_LoginView1_treeView1_SelectedNode" value="#SELECTNODE#" type="formfield">
    <cfhttpparam name="__EVENTTARGET"  value="" type="formfield">
    <cfhttpparam name="__EVENTARGUMENT"  value="" type="formfield">
    <cfhttpparam name="LeftNav1_LoginView1_treeView1_PopulateLog" value="" type="formfield">
    <cfhttpparam name="__VIEWSTATE"  value="#VIEWSTATE#" type="formfield">
    <cfhttpparam name="__VIEWSTATEGENERATOR"  value="#VIEWSTATEGENERATOR#" type="formfield">
    <cfhttpparam name="__EVENTVALIDATION"  value="#EVENTVALIDATION#" type="formfield">
    <cfhttpparam name="ctl00$MainContent$repMUData$ctl00$btnExport"  value="Export to Excel" type="formfield">
</cfhttp>

When I do cfdump here is the the result

enter image description here

When I do cfdump that it is giving some binary data. For sure I am getting some data but not sure how to extract the data into xls file


Solution

  • To write the binary data to file just use the function filewrite. In the following snippet I write to a temporary file, but you would write to were you want to permanently store the file. I then read file back into spreadsheet object to verify that the write worked as intended.

    <cfhttp url="http://example.com/test.cfm" result="ExportToExcelresult">
    </cfhttp>
    <cfscript>
        //Replace with the file path where you want to permanently store the file
        yourFileLocation = getTempFile(getTempDirectory() ,"xls");
    
        //Save to file system
        filewrite(yourFileLocation, ExportToExcelresult.filecontent.toByteArray());
    
        //Not needed. Only verifying there is a spreadsheet written to the file location
        writeOutput("Is spreadsheet: " & isSpreadsheetFile(yourFileLocation));
    
        //You will not be working with a temp file. Do not delete it.
        fileDelete(yourFileLocation);
    </cfscript>