使用cfwheels,coldfusion和cfspreadsheet创建一个带有非标准列名(带空格)的Excel文件导出

Bob*_*ley 7 coldfusion cfwheels coldfusion-10 cfspreadsheet

这更像是一个操作方法,而不是一个实际的问题.(我搜索过,找不到解决办法,所以我想出了这个)

我需要创建一个excel文件导出,允许用户:

  1. 使用原始表格中的表单过滤数据
  2. 将结果从原始表导出到excel文件.
  3. 允许非标准列名称包含空格和一些特殊字符.
  4. 在某些列中格式化导出的数据,同时保留原始表值(用于过滤).

Bob*_*ley 3

我搜索并没有找到解决方案,所以我想出了这个:

使用示例表“薪水”

CREATE TABLE [dbo].[Salary](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [employee_id] [varchar](36) NULL,
    [salary] [decimal](18, 0) NULL,
    [createdat] [datetime] NULL,
    [updatedat] [datetime] NULL,
    [updated_by] [varchar](36) NULL,
    [created_by] [varchar](36) NULL )
Run Code Online (Sandbox Code Playgroud)

首先创建一个用于提取 Excel 数据的特殊模型。示例“导出.cfc”

模型\导出.cfc

<cfcomponent extends="Model" output="false">
    <cffunction name="init">   
      <cfset table("Salary")/>
       <!--- defined properties to allow spaces in column names via [] alias.--->
      <cfset property(sql="employee_id", name="[Employee ID]")>
      <cfset property(sql="dbo.getName(employee_id)", name="[The Employee Name]")>
      <cfset property(sql="salary", name="[He gets paid what?]")>
      <cfset property(sql="CONVERT(VARCHAR, createdAt, 101)", name="[Date Created]")>
    </cffunction>   
</cfcomponent>
Run Code Online (Sandbox Code Playgroud)

然后只需拉出 Excel 导出所需的特定列即可。([] 是必要的)

<cfset columns = "id,[employee id],[The Employee Name],[He gets paid what?],[Date Created]"/>

<cfset excelData = model("export").findAll( 
                                        select=columns,
                                        parameterize=false
                                         ) />
<cfspreadsheet 
        action = "write"  
        filename="#expandpath('files')#\export.xls" 
        query="excelData" 
        overwrite="true">
Run Code Online (Sandbox Code Playgroud)