因此,显然我不能使用任何特殊字符,甚至不能用点缩写来设置cfspreadsheet中的列名。有办法绕过这件事吗?我基本上是使用cfspreadsheet将查询结果转储到excel文件中,并且需要保留某些字符,例如点符号来缩写某些术语。如果能把它提升到下一个级别,并在这些列名中使用特殊字符,那就太好了。这附近有工作吗?
代码样本。
<cfset qryColumnList = "CaseNo,Model,Name" />
<cfset qryResultSet = QueryNew(qryColumnList) />
<cfoutput query="getUsedCars">
<cfset QueryAddRow(qryResultSet, 1) />
<cfset querySetCell(qryResultSet, "CaseNo", caseno EQ " " ? "" : caseno) />
<cfset querySetCell(qryResultSet, "Model", model EQ " " ? "" : model) />
<cfset querySetCell(qryResultSet, "Name", name EQ " " ? "" : 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>发布于 2015-05-08 18:38:28
因此,我认为您要问的是如何使用类似于Case No…的列名。在这种情况下,当您将The column name Case No… is invalid传递到QueryNew时,您将得到一个类似于: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… |
------------------------------------我希望我正确地理解了你的问题!
https://stackoverflow.com/questions/30112346
复制相似问题