这更像是一个方法而不是一个实际的问题。(我搜索并找不到解决方案,所以我想出了这个)
我需要创建一个 excel 文件导出,允许用户:
- 使用表单从原始表中过滤数据
- 将结果从原始表导出到 excel 文件。
- 允许带有空格和一些特殊字符的非标准列名。
- 在某些列中格式化导出的数据,同时保留原始表值(用于过滤)。
这更像是一个方法而不是一个实际的问题。(我搜索并找不到解决方案,所以我想出了这个)
我需要创建一个 excel 文件导出,允许用户:
我搜索并找不到解决方案,所以我想出了这个:
使用示例表“薪水”
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">