2016-04-08 22 views
7

这更像是一个实际问题。 (我搜索,但没有找到一个解决方案,所以我想出了这个)使用cfwheels,coldfusion和cfspreadsheet创建带有非标准列名称(带空格)的excel文件导出

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

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

(编辑)感谢发布。按照SO的Q&A格式,您可以将其分解为单独的“问题”,然后将解决方案单独作为“答复”发布? (我知道这有点奇怪,因为你都在问和回答,但这似乎是[回答你自己的问题]的首选方法(http://stackoverflow.com/help/self-answer)/创建一个方法到:-) – Leigh

+0

这真是太棒了!绝对要做@Leigh建议。 –

+0

非常感谢!将对我有用。 – Thorsten

回答

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) 

首先创建一个特殊的模型拉动Excel数据。例如 “export.cfc”

型号\ export.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> 

然后就拉你需要的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"> 
+0

这是超级聪明的。我打算改变我们的一些Excel功能来使用它,而不是我们一直使用的另一个愚蠢的黑客。 –

+0

听起来不错。 –

相关问题