在 ColdFusion 中导出到 Excel

Export to Excel in ColdFusion

我正在使用 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 时,这里是结果

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

要将二进制数据写入文件只需使用函数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>