Coldfusion cf1在Excel文件中读取电子表格以查询can';t访问结果中的列
我正在尝试使用ColdFusion 10和CFSpreadSheet自动处理电子表格。到目前为止,我可以在中读取文件并转储查询对象,没有任何问题Coldfusion cf1在Excel文件中读取电子表格以查询can';t访问结果中的列,coldfusion,coldfusion-10,cfspreadsheet,Coldfusion,Coldfusion 10,Cfspreadsheet,我正在尝试使用ColdFusion 10和CFSpreadSheet自动处理电子表格。到目前为止,我可以在中读取文件并转储查询对象,没有任何问题 <cfspreadsheet action="read" src="#theFile#" query="qData" headerrow="1" columns="1,8,9,11,33"/> <cfdump var="#qData#"/> 是空间问题吗?他把事情搞砸了?如果是这样,如何在读取文件时或之后立即删除这些内容并
<cfspreadsheet action="read" src="#theFile#" query="qData" headerrow="1" columns="1,8,9,11,33"/>
<cfdump var="#qData#"/>
是空间问题吗?他把事情搞砸了?如果是这样,如何在读取文件时或之后立即删除这些内容并更新列名?解决方案-列名中有多个空格,ColdFusion不允许这样做。使用正则表达式可能会做得更好,我将在下一步对此进行研究,但这里有一个快速而肮脏的解决方案
<cfset colNameArray = qData.getColumnNames() />
<cfloop from="1" to="#arrayLen(colNameArray)#" index="i">
<cfset colNameArray[i] = colNameArray[i].replace(' ','') />
<cfset colNameArray[i] = colNameArray[i].replace('(','') />
<cfset colNameArray[i] = colNameArray[i].replace(')','') />
<cfset colNameArray[i] = colNameArray[i].replace('/','') />
</cfloop>
<cfset qData.setColumnNames(colNameArray) />
我最初认为这可能是因为“group”是一个保留的SQL关键字。(不要在查询查询中尝试使用该列名称。) 您应该循环并使用isValid(“variablename”,ColumnName)确定列名是否有效,然后使用RenameColumn UDF对其进行重命名,而不是为已知值清理第一行值并重新保存文件。我们更喜欢这种方法,因为不修改客户机的原始Excel文件是至关重要的(特别是因为Adobe ColdFusion在编写文件时存在一些错误,可能会弄乱文件中的其他工作表和/或格式。) 删除非法字符的简单方法是使用“重新替换”:
ReReplace(NewColumnName, "[^a-zA-Z0-9!]", "", "ALL")
但您还需要确保新列名不是空的,以字母开头,并且没有用于其他列。如果希望列按特定顺序排列,只需安全地将其重命名为“col_1”(或使用编号作为任何非唯一和/或非法列名的默认回退)
以下是2011年博客文章中的RenameColumn UDF:
(以防人们不阅读评论…)
括号和斜杠将有问题,因为它们不符合标准。最简单的选项是使用指定有效的列名。(另外,与您的问题无关,但如果您想排除标题行,请使用excludeHeaderRow=“true”
)
在大多数情况下,还可以使用关联数组表示法访问无效列名。但是,使用“columnNames”属性更简单/更干净
<cfoutput query="qData" maxrows="#qData.RecordCount#">
#qData["BTBN(002)"][currentRow]#<br />
....
</cfoutput>
#qData[“BTBN(002)”][currentRow]#
....
只需使用“columnNames”属性来指定您想要的任何列名:)您也可以使用数组表示法来访问无效列名,即#query[“BTBN(002)”][currentRow]#
但是“columnNames”在我看来更简单/更干净。我强烈建议客户端在上传之前使用Excel的“ASAP实用程序”来修剪所有值。它还具有将公式快速转换为值的功能,以便ColdFusion在尝试确定值时不会引发错误。(它还去除了困扰许多Excel文件的不可见的不间断空间,这些文件无法通过trim()删除,并导入到SQL中。)注意:ColdFusion的isValid(“variableName”)
有一个已知的bug/功能,在评估之前会对字符串进行修剪,因此,您还需要手动检查列名中的前导/尾随空格。
ReReplace(NewColumnName, "[^a-zA-Z0-9!]", "", "ALL")
<cffunction name="renameColumn" access="public" output="false" returntype="query" hint="Uses java to rename a given query object column">
<cfargument name="queryObj" required="true" type="query">
<cfargument name="oldColName" required="true" type="string">
<cfargument name="newColName" required="true" type="string">
<!--- Get an array of the current column names --->
<cfset var colNameArray = queryObj.getColumnNames()>
<cfset var i = 0>
<!--- Loop through the name array and try match the current column name with the target col name--->
<cfif arrayLen(colNameArray)>
<cfloop from="1" to="#arrayLen(colNameArray)#" index="i">
<!--- If we find the target col name change to the new name --->
<cfif compareNoCase(colNameArray[i],arguments.oldColName) EQ 0>
<cfset colNameArray[i] = arguments.newColName>
</cfif>
</cfloop>
</cfif>
<!--- Update the column names with the updated name array --->
<cfset queryObj.setColumnNames(colNameArray)>
<cfreturn queryObj />
</cffunction>
<cfspreadsheet action="read" src="c:\path\file.xlsx"
query="qData"
columnNames="BTBN_002,DOB,GROUP_NAME,MEMBER_DEPEND_NAME,REL"
excludeHeaderRow="true"
headerrow="1"
... />
<cfoutput query="qData" maxrows="#qData.RecordCount#">
#qData["BTBN(002)"][currentRow]#<br />
....
</cfoutput>