所以显然我不能使用任何特殊字符,甚至没有用点来缩写来设置cfspreadsheet中的列名。有办法解决这个问题吗?我基本上使用cfspreadsheet将我的查询结果转储到excel文件中,并且需要保留某些字符(如点符号)以缩写某些术语。实际上将它带到下一级并在这些列名中也使用特殊字符会很好。有没有解决这个问题?
代码示例。
{{1}}
答案 0 :(得分:3)
所以我认为你要问的是如何使用像_.toArray()
之类的列名。在这种情况下,当您将Case No…
传递给The column name Case No… is invalid
时,您会收到如下错误:QueryNew
。我还假设您尝试使用的列名是动态的。
您可以执行的操作是在创建查询时删除任何无效字符,然后使用有效列名称使用数据填充查询。然后,当您创建电子表格时,只需使用原始列名称。
我已经简化了您的示例并对其进行了更改,以便生成您想要的结果<)>
<!--- the wanted spreadsheet column headers with special chars in --->
<cfset headersList = "Name,Case No…">
<!--- strip out invalid characters so can use as query column names --->
<cfset qryColumnList = reReplaceNoCase(headersList, "[^a-z0-9,]", "", "all")>
<cfset qryResultSet = QueryNew(qryColumnList)>
<!--- add some data to the query object... --->
<cfset queryAddRow(qryResultSet)>
<cfset querySetCell(qryResultSet, "Name", "Hellip")>
<!--- note that instead of referring to the field as 'Case No…' using 'CaseNo' --->
<cfset querySetCell(qryResultSet, "CaseNo", "this text is truncated…")>
<cfscript>
xlsxopencases = SpreadsheetNew("opencases",true);
// note: using the headersList variable which has special chars in it
SpreadsheetAddRow(xlsxopencases,headersList);
SpreadsheetAddRows(xlsxopencases,qryResultSet);
strFileName=GetDirectoryFromPath(GetCurrentTemplatePath()) & "OpenCasesReport.xlsx";
SpreadsheetFormatRow(xlsxopencases, {bold="true"}, 1);
SpreadsheetFormatRows(xlsxopencases, {dataformat="text"}, "1-#qryResultSet.recordcount + 1#");
</cfscript>
<cfspreadsheet action="write"
filename="#strFileName#"
name="xlsxopencases"
sheet="1"
sheetname="OpenCasesReport"
overwrite="true">
<cfheader name="Content-Disposition" value="attachment; filename=OpenCasesReport.xlsx">
<cfcontent file="#strFileName#" type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet">
这将生成一个如下所示的电子表格:
------------------------------------
| Name | Case No… |
------------------------------------
| Hellip | this text is truncated… |
------------------------------------
我希望我能正确理解你的问题!