CFSavecontent中的CFContent标记会提示用户下载.cfm文件

时间:2012-12-14 16:59:26

标签: coldfusion

我编写了一个ColdFusion文件,它从数据库中获取一些数据并将其存储在数组中并填充一些变量。然后我在html表格式化它并将其输出到保存在服务器上的excel文件。

我遇到的问题是访问页面时,会写入文件,但也会提示浏览器下载cfm文件。例如,我访问了文件编写的页面(bla/bla/get_open_today.cfm),但浏览器也想下载get_open_today.cfm。有谁知道导致这个问题的原因是什么?

以下是excel输出的代码:

<!--- Export to XLS file --->
<cfsavecontent variable="saveContent1">
<cfcontent type="application/msexcel">
<cfoutput>
<table border="1">
</tr>
<th >Hall ID</th>
<th >Room Number</th>
<th >Room Type</th>
<th >In Date</th>
<th >Clean Date</th>
<th >Comments</th>
</tr>
</cfoutput>
<cfoutput>
<cfloop index="x" from="1" to="#ArrayLen(myarray)#">
    <tr><td>#myarray[x][1]#</td><td> #myarray[x][2]# </td><td>#myarray[x][3]# </td><td>#myarray[x][4]#                                  </td><td>#myarray[x][5]#</td><td>#myarray[x][6]#</td></tr>
</td>
</cfloop>
    <tr>
        <td> </td>
        <td> </td>
        <td> </td>
        <td> </td>
    </tr>
    <tr>
        <td>One Bedroom</td>
        <td>#oneBR#</td>
        <td>Units Assigned</td>
        <td>#unitsAssigned#</td>
    </tr>
    <tr>
        <td>Two Bedroom</td>
        <td>#twoBR#</td>
        <td>Units Unassigned</td>
        <td>#unitsUnassigned#</td>
    </tr>
    <tr>
        <td>Three Bedroom</td>
        <td>#threeBR#</td>
        <td>Total Units</td>
        <td>#totalUnits#</td>
    </tr>
</cfoutput>

</table>
</cfsavecontent>
<cffile action="write" mode="777" output="#saveContent1#" file="Example/www/BFILES/Unit_Report.xls">

2 个答案:

答案 0 :(得分:4)

删除cfcontent - 这是将页面的内容类型标题设置为未处理的类型,从而提示浏览器提供下载。

cfcontent的目的是向客户端发送信息 - 使用内部cfsavecontent没有效果,因为savecontent只是在大字符串上执行cfset的一种方便方法。

您无法使用/不需要cfcontent将HTML另存为XLS文件。

答案 1 :(得分:0)

在MS发布Office 2007时,使用html表标签进行excel导出的技术变得很糟糕。它仍然有效,但不是很好。

幸运的是有更好的方法。更高版本的ColdFusion有一个cfspreadsheet标签。如果您的版本不够新,Ben Nadel的博客可以链接到您可以使用的Apache POI。

相关问题