在ColdFusion中导出到Excel

时间:2019-03-21 17:23:27

标签: coldfusion coldfusion-11 cfhttp

我正在使用cfhttp将少量输入发布到url中,并希望将一些数据下载到xls文件中。我正在尝试使用cffile =“ write”来获取数据,这是行不通的。任何人都可以建议我们如何做到这一点。这是下面的代码

<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>

当我执行cfdump时,结果是

enter image description here

当我执行cfdump时,它正在提供一些二进制数据。当然可以,但是我不确定如何将数据提取到xls文件中

1 个答案:

答案 0 :(得分:1)

要将二进制数据写入文件,只需使用函数filewrite。在以下代码段中,我将写入一个临时文件,但是如果要永久存储该文件,您将写入该文件。然后,我将文件读回到电子表格对象中,以验证写入是否按预期进行。

<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>