【问题标题】:CF Save As Dialog box and setting Save Type AsCF 另存为对话框和设置另存类型为
【发布时间】:2016-04-08 14:36:58
【问题描述】:

我正在使用 CF 10,我正在尝试创建一个另存为对话框,并将另存为类型设置为 xls(Excel 扩展),以便可以轻松地将我的报告保存在 excel 中。我在想我也许可以用

<cfelseif FORM.Format IS "xls">
    <cfcontent type="application/vnd.ms-excel">
    <cfheader name="Content-Disposition" value="inline; filename=fileName.xls">

但这并没有打开正确的对话框。有谁知道这是如何实现的?

这是应该出现的:

    <cfelseif FORM.Format IS "xls">
    <cfcontent type="application/vnd.ms-excel">
    <cfheader name="Content-Disposition" value="inline; filename=fileName.xls">


    <cfset result = {} />
<cftry>
    <cfset date1 = CREATEODBCDATETIME(form.StartDate & '00:00:00')>
    <cfset date2 = CREATEODBCDATETIME(form.EndDate & '23:59:59')>

    <cfquery datasource="#application.dsn#" name="GetLocationInfo">
        SELECT  *
        FROM    cl_checklists
        WHERE   date >=  <cfqueryparam value="#date1#" cfsqltype="cf_sql_timestamp" />
                AND date <= <cfqueryparam value="#date2#" cfsqltype="cf_sql_timestamp" />
                AND trans_location IN ( <cfqueryparam value="#FORM.location#" cfsqltype="cf_sql_varchar" list="true" />  )
    </cfquery>

    <cfquery name="allLocCode" dbtype="query">
        SELECT DISTINCT trans_location, COUNT(*) AS locationCount FROM GetLocationInfo Where trans_location is not null GROUP BY trans_location ORDER BY trans_location
    </cfquery>

    <cfset columnSum = ArraySum(allLocCode['locationCount'])>
    <cfset checkListPercentage = arrayNew(1)>

<table border="1" id="Checklist_Stats">
  <thead>
    <th><strong>Location</strong></th>
    <th><strong>Percent of Total Checklists</strong></th>
    <th><strong>Location Total</strong></th> 
  </thead>
  <tbody>
    <cfloop query="allLocCode">
        <cfset thisLocationName = trim(allLocCode.trans_location) />

    <cfquery name="allLocCodeForLocationQry" dbtype="query">
        SELECT trans_location,count(*) AS locCntr FROM GetLocationInfo WHERE trans_location='#thisLocationName#' GROUP BY trans_location ORDER BY trans_location
    </cfquery>

    <cfoutput query="allLocCodeForLocationQry">
        <cfset currentPercentage = (allLocCodeForLocationQry.locCntr / columnSum * 100)>
        <cfset arrayAppend(checkListPercentage, currentPercentage)>
        <cfset totalPercentage = arraySum(checkListPercentage)>
    <tr>
      <td><strong>#thisLocationName#</strong></td>
      <td>#numberFormat(currentPercentage, '9.99')#%</td>
      <td>#allLocCodeForLocationQry.locCntr#</td>
    </tr>
   </cfoutput>
   </cfloop>
   <tr>
   <cfoutput>
    <td><strong>Total</strong></td>
    <td>#numberFormat(totalPercentage, '9.99')#%</td>
    <td>#columnSum#</td>
    </cfoutput>
  </tr>
  </tbody>
</table>

    <cfcatch type="any">
        <cfset result.error = CFCATCH.message >
        <cfset result.detail = CFCATCH.detail >
    </cfcatch>
</cftry>


    </cfcontent>

</cfif>

【问题讨论】:

  • 不是讽刺,但是……您在发布此问题之前是否进行了搜索? :) 询问的原因是关于如何通过使用 cfheader 和 cfcontent 生成 Excel 下载(或 faux-html-Excel 下载)的帖子有 LOT,无论是在 SO 还是主要搜索引擎。例如,stackoverflow.com/questions/4507973/how-can-i-download-to-excel/…
  • 我真的做到了,哈哈。但是我使用这些出现的屏幕不像他们要求的另存为对话框。我还没有找到一个例子:/
  • @Leigh 我添加了我不断收到的不正确类型的盒子的图像 =/
  • 请停止删除所有/大部分 cmets。偶尔是可以的,但是如果所有内容都被删除,那么整个讨论的大量上下文也会被删除,其他 cmets 不再有意义;-) 请记住,保留线程也是为了帮助其他人,而不仅仅是你。
  • 告诉用户这是不可能的。

标签: coldfusion


【解决方案1】:

只需在 cfcontent 之后添加以下带有 XLS 文件名的 cfheader:

【讨论】:

  • HTML 下载对话框与桌面应用程序中的“另存为”对话框不同。出于安全原因,浏览器有更大的限制。 “文件名”只是对浏览器的一个建议,可以随意修改或忽略。然而,眼见为实。自己测试一下,看看会发生什么。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-08-02
  • 1970-01-01
  • 1970-01-01
  • 2021-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多