首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CF10 CFSPREADSHEET列名.转义特殊字符

CF10 CFSPREADSHEET列名.转义特殊字符
EN

Stack Overflow用户
提问于 2015-05-07 21:54:45
回答 1查看 1.1K关注 0票数 1

因此,显然我不能使用任何特殊字符,甚至不能用点缩写来设置cfspreadsheet中的列名。有办法绕过这件事吗?我基本上是使用cfspreadsheet将查询结果转储到excel文件中,并且需要保留某些字符,例如点符号来缩写某些术语。如果能把它提升到下一个级别,并在这些列名中使用特殊字符,那就太好了。这附近有工作吗?

代码样本。

代码语言:javascript
复制
<cfset qryColumnList = "CaseNo,Model,Name" />
<cfset qryResultSet = QueryNew(qryColumnList) />

<cfoutput query="getUsedCars">
                <cfset QueryAddRow(qryResultSet, 1) />
                <cfset querySetCell(qryResultSet, "CaseNo", caseno EQ "&nbsp;" ? "" : caseno) />
                <cfset querySetCell(qryResultSet, "Model", model EQ "&nbsp;" ? "" : model) />
                <cfset querySetCell(qryResultSet, "Name", name EQ "&nbsp;" ? "" : name) />                      
</cfoutput>

<cfscript>
                xlsxopencases = SpreadsheetNew("opencases",true);
                SpreadsheetAddRow(xlsxopencases,qryColumnList);
                SpreadsheetAddRows(xlsxopencases,qryResultSet);
                strFileName=GetDirectoryFromPath(GetCurrentTemplatePath()) & "OpenCasesReport.xlsx";
                SpreadsheetFormatRow(xlsxopencases, {bold="true"}, 1);
                SpreadsheetFormatRows(xlsxopencases, {dataformat="text"}, "1-#qryResultSet.recordcount + 1#");
                SpreadsheetFormatColumn(xlsxopencases, {dataformat="dd/MM/yy HH:mm:SS"},18);
</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" />

<cffile action="delete" file="#strFileName#" />
</cfif>
EN

回答 1

Stack Overflow用户

发布于 2015-05-08 18:38:28

因此,我认为您要问的是如何使用类似于Case No…的列名。在这种情况下,当您将The column name Case No… is invalid传递到QueryNew时,您将得到一个类似于:QueryNew的错误。我还假设您试图使用的列名是动态的。

您可以做的是在创建查询时删除任何无效字符,然后使用有效的列名填充数据。然后,在创建电子表格时,只需使用原始列名。

我简化了您的示例,并对其进行了更改,以便它将产生我认为您想要的结果:)

代码语言:javascript
复制
<!--- 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">

这将生成一个电子表格,如下所示:

代码语言:javascript
复制
------------------------------------
| Name   | Case No…                |
------------------------------------
| Hellip | this text is truncated… |
------------------------------------

我希望我正确地理解了你的问题!

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30112346

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档