2016-04-08 25 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

聽起來不錯。 –

相關問題