从查询创建包含多张工作表的工作簿
Create workbook with multiple sheets from Queries
我正在使用 ColdFusion 2016,我发现我使用的服务器只有 CF9,我对此很陌生。我一直致力于更新现有代码以满足用户现在的需求。到目前为止,我已经成功了,但这超出了我的范围。我有一个网站可以生成一个 excel 工作簿,其中有一个 sheet。它使用 HTML 和一个查询来创建它。从 A1 中的查询名称开始,A3 中的报告日期,A5:H5 中的 table headers,然后是 A6:H53 中的数据(确切长度可能有所不同,但始终从A - H 列)。我将 post 用于创建工作簿的内容。我想要做的是再使用 3 个查询向工作簿中再添加 3 个 sheet。我尝试添加一个我发现 here 的函数,但没有任何用处。我尝试修改现有代码以尝试使用所有 4 个查询,但没有任何乐趣。
如有任何帮助,我们将不胜感激。让我知道是否需要添加更多详细信息。
这是代码:(我为我添加的内容添加了评论,试图让它工作)
<cfsilent>
<!--- *******************************************************************
Filename: execSummary_Excel.cfm, v1.0 03/07/2012
Created By: Original Writer
Description: Excel report export for Executive Summary Report.
Change History:
Date........Name...........Description of Change........................
08/01/2012 Original Writer Added committed column.
02/28/2013 Original Writer Added stateGM and GM.
*************************************************************************--->
<cfinvoke component="financial.financial" method="getExecSummary" returnvariable="qExecSummary">
<cfinvokeargument name="level" value="#URL.level#" />
<cfinvokeargument name="stateGM" value="#URL.stateGM#" />
<cfinvokeargument name="GM" value="#URL.GM#" />
<cfinvokeargument name="engVP" value="#URL.engVP#" />
<cfinvokeargument name="engDir" value="#URL.engDir#" />
</cfinvoke>
<!---Added this to test if I can get more than one sheet to the Workbook--->
<cfinvoke component="financial.financial" method="getExecSummary_OLD" returnvariable="qExecSummary_OLD">
<cfinvokeargument name="level" value="#URL.level#" />
<cfinvokeargument name="stateGM" value="#URL.stateGM#" />
<cfinvokeargument name="GM" value="#URL.GM#" />
<cfinvokeargument name="engVP" value="#URL.engVP#" />
<cfinvokeargument name="engDir" value="#URL.engDir#" />
</cfinvoke>
<!--- Get Report Date since qExecSummary is more complex than the other report queries --->
<cfquery name="qRpt_Date" datasource="#application.dsn#">
SELECT DISTINCT rpt_date
FROM fin_data
</cfquery>
<cfsetting requesttimeout="5000" />
</cfsilent>
<!---Added this as a function that should allow me to use multiple queries and create a workbook with more than one sheet--->
<cfscript>
//Create new workbook with one sheet
//by default that sheet is the active sheet
Workbook = SpreadsheetNew("ExecSummary");
//Add Data to the sheet
format1.bold="true";
formatNum.dataformat="0.00%";
Spreadsheetformatcell(Workbook,format1,1,1);
SpreadSheetSetCellValue(Workbook,"Executive Summary Report",1,1);
Spreadsheetformatcell(Workbook,format1,3,1);
SpreadSheetSetCellValue(Workbook,"#dateFormat(now(),'mm/dd/yyyy')#",3,1);
SpreadSheetSetCellValue(Workbook,"Data Date",5,1);
SpreadSheetSetCellValue(Workbook,"Level",5,2);
SpreadSheetSetCellValue(Workbook,"Name",5,3);
SpreadSheetSetCellValue(Workbook,"Description",5,4);
SpreadSheetSetCellValue(Workbook,"Budget",5,5);
SpreadSheetSetCellValue(Workbook,"Commited",5,6);
SpreadSheetSetCellValue(Workbook,"Spent YTD",5,7);
SpreadSheetSetCellValue(Workbook,"% Spent",5,8);
arr="Here";
writedump(arr);
//if (qExecSummary.recordCount) {
// rowNum = 6;
// arr="Here";
// writedump(rowNum);
//alert(qExecSummary.recordCount);
//for(dataRow in qExecSummary){
//SpreadSheetSetCellValue(Workbook,dateFormat(qRpt_Date.rpt_Date, 'mm/dd/yyyy'),rowNum,1);
//SpreadSheetSetCellValue(Workbook,dataRow.responsible,rowNum,2);
//SpreadSheetSetCellValue(Workbook,dataRow.name,rowNum,3);
//SpreadSheetSetCellValue(Workbook,dataRow.Description,rowNum,4);
//SpreadSheetSetCellValue(Workbook,dataRow.bud_sum,rowNum,5);
//SpreadSheetSetCellValue(Workbook,dataRow.committed,rowNum,6);
//SpreadSheetSetCellValue(Workbook,dataRow.Spent_YTD,rowNum,7);
/*if (qExecSummary.bud_sum NEQ 0){
Spreadsheetformatcell(Workbook,formatNum,rowNum,8);
//percentSpent="#(qExecSummary.Spent_YTD/qExecSummary.bud_sum)*100#";
SpreadSheetSetCellValue(Workbook,(dataRow.Spent_YTD/dataRow.bud_sum)*100,rowNum,8);
}
else {*/
//SpreadSheetSetCellValue(Workbook,0,rowNum,8);
//}
//rowNum++;
//}
//End of WriteOutput
//} else {
// SpreadSheetAddRows(Workbook,"No results for your criteria.");
//}
</cfscript>
<!---
<cffunction name="QueriesToXLS" access="public">
<cfargument name="queryArr" required="true">
<cfargument name="sheetNameArr" required="false">
<cfset tempPath="C:\Temp\ExecutiveSummary" & ".xls">
<cfset counter= 1>
<cfloop array="#ARGUMENTS.queryArr#" index="i" >
<cfset sheetName="Sheet#counter#">
<cfif isDefined("ARGUMENTS.sheetNameArr")>
<cfset sheetName=ARGUMENTS.sheetNameArr[counter]>
</cfif>
<cfspreadsheet action="update" filename="#tempPath#" query="i" sheetName="#sheetName#"/>
<cfset counter += 1>
</cfloop>
<cfreturn SpreadsheetRead(tempPath)>
</cffunction>
<cfset xlsData = QueriesToXLS([qExecSummary,qExecSummary],["ExecutiveSummary","ExecutiveSummaryOLD"])>
--->
<cfheader name="Content-Disposition" value='attachment; filename="execSummaryNew.xls"'>
<!---cfcontent type="application/msexcel" variable="#SpreadsheetReadBinary(xlsData)#" reset="true"--->
<cfcontent type="application/vnd.ms-excel"> <!---This is where the application type is being set to Excel--->
<!---html xmlns:x="urn:schemas-microsoft-com:office:excel">
<head>
</head>
<h2>Executive Summary Report</h2>
<cfoutput>
<p>#dateFormat(now(), 'mm/dd/yyyy')#</p>
</cfoutput>
<table id="tableOne" class="yui" cellspacing="0" cellpadding="5">
<thead>
<tr>
<th>Data Date</th>
<th>Level</th>
<th>Name</th>
<th>Description</th>
<th>Budget</th>
<th>Committed</th>
<th>Spent YTD</th>
<th>% Spent</th>
</tr>
</thead>
<cfif qExecSummary.recordCount>
<tbody>
<cfoutput query="qExecSummary">
<tr>
<td>#dateFormat(qRpt_Date.rpt_Date, 'mm/dd/yyyy')#</td>
<td>#qExecSummary.responsible#</td>
<td>#qExecSummary.name#</td>
<td>#qExecSummary.Description#</td>
<td>#qExecSummary.bud_sum#</td>
<td>#qExecSummary.committed#</td>
<td>#qExecSummary.Spent_YTD#</td>
<td><cfif qExecSummary.bud_sum NEQ 0>
#numberFormat((qExecSummary.Spent_YTD/qExecSummary.bud_sum)*100,"9.9")#%
<cfelse>
0
</cfif>
</td>
</tr>
</cfoutput>
</tbody>
<cfelse>
<tr>
<td colspan="9">No results for your criteria.</td>
</tr>
</cfif>
</table>
</html--->
编辑
我已尝试按照副本中引用的问题提供的答案进行操作。我无法让它与我的查询一起使用。我添加了以下代码来代替我的脚本:
编辑 2
已更新此脚本,但仍然无法正常工作。 writeOutput()
上的错误。我不确定如何使用查询来为行创建数据?
<cfscript>
//Create new workbook with one sheet
//by default that sheet is the active sheet
Workbook = SpreadsheetNew("ExecSummary");
//Add Data to the sheet
format1.bold="true";
formatNum.dataformat="0.00%";
Spreadsheetformatcell(Workbook,format1,1,1);
SpreadSheetSetCellValue(Workbook,"Executive Summary Report",1,1);
Spreadsheetformatcell(Workbook,format1,3,1);
SpreadSheetSetCellValue(Workbook,"#dateFormat(now(),'mm/dd/yyyy')#",3,1);
SpreadSheetSetCellValue(Workbook,"Data Date",5,1);
SpreadSheetSetCellValue(Workbook,"Level",5,2);
SpreadSheetSetCellValue(Workbook,"Name",5,3);
SpreadSheetSetCellValue(Workbook,"Description",5,4);
SpreadSheetSetCellValue(Workbook,"Budget",5,5);
SpreadSheetSetCellValue(Workbook,"Commited",5,6);
SpreadSheetSetCellValue(Workbook,"Spent YTD",5,7);
SpreadSheetSetCellValue(Workbook,"% Spent",5,8);
if (qExecSummary.recordCount) {
rowNum = 6;
//writeOutput(query="qExecSummary");
for(dataRow in qExecSummary){
SpreadSheetSetCellValue(Workbook,dateFormat(qRpt_Date.rpt_Date, 'mm/dd/yyyy'),rowNum,1);
SpreadSheetSetCellValue(Workbook,dataRow.responsible,rowNum,2);
SpreadSheetSetCellValue(Workbook,dataRow.name,rowNum,3);
SpreadSheetSetCellValue(Workbook,dataRow.Description,rowNum,4);
SpreadSheetSetCellValue(Workbook,dataRow.bud_sum,rowNum,5);
SpreadSheetSetCellValue(Workbook,dataRow.committed,rowNum,6);
SpreadSheetSetCellValue(Workbook,dataRow.Spent_YTD,rowNum,7);
if (qExecSummary.bud_sum NEQ 0){
Spreadsheetformatcell(Workbook,formatNum,rowNum,8);
percentSpent="#(qExecSummary.Spent_YTD/qExecSummary.bud_sum)*100#";
SpreadSheetSetCellValue(Workbook,(qExecSummary.Spent_YTD/qExecSummary.bud_sum)*100,rowNum,8);
}
else {
SpreadSheetSetCellValue(Workbook,0,rowNum,8);
}
rowNum++;
}
//End of WriteOutput
} else {
SpreadSheetAddRows(Workbook,"No results for your criteria.");
}
</cfscript>
但是它在 Output
行上显示错误。我不知道该改成什么。我搜索了 cfscript
标签中可用的内容并找到了 this,但没有任何内容看起来有帮助?
已更新 <cfscript>
现在我收到此错误:
最终编辑
这是工作脚本:
<cfsilent>
<!--- *******************************************************************
Filename: execSummary_Excel.cfm, v1.0 03/07/2012
Created By: Original Writer
Description: Excel report export for Executive Summary Report.
Change History:
Date........Name...........Description of Change........................
08/01/2012 Original Writer Added committed column.
02/28/2013 Original Writer Added stateGM and GM.
*************************************************************************--->
<cfinvoke component="financial.financial" method="getExecSummary" returnvariable="qExecSummary">
<cfinvokeargument name="level" value="#URL.level#" />
<cfinvokeargument name="stateGM" value="#URL.stateGM#" />
<cfinvokeargument name="GM" value="#URL.GM#" />
<cfinvokeargument name="engVP" value="#URL.engVP#" />
<cfinvokeargument name="engDir" value="#URL.engDir#" />
</cfinvoke>
<!---Added this to test if I can get more than one sheet to the Workbook--->
<cfinvoke component="financial.financial" method="getExecSummary331" returnvariable="qExecSummary331">
<cfinvokeargument name="level" value="#URL.level#" />
<cfinvokeargument name="stateGM" value="#URL.stateGM#" />
<cfinvokeargument name="GM" value="#URL.GM#" />
<cfinvokeargument name="engVP" value="#URL.engVP#" />
<cfinvokeargument name="engDir" value="#URL.engDir#" />
</cfinvoke>
<!--- Get Report Date since qExecSummary is more complex than the other report queries --->
<cfquery name="qRpt_Date" datasource="#application.dsn#">
SELECT DISTINCT rpt_date
FROM fin_data
</cfquery>
<cfsetting requesttimeout="5000" />
</cfsilent>
<!---Added this as a function that should allow me to use multiple queries and create a workbook with more than one sheet--->
<cfscript>
//Create new workbook with one sheet
//by default that sheet is the active sheet
Workbook = SpreadsheetNew("ExecSummary");
//Add Data to the sheet
//Formatting
format1.bold="true";
format1.fontsize=12;
format1.font="Calibri";
format2.bold="true";
format2.fontsize=18;
format2.font="Calibri";
formatNum.dataformat="0.0%";
//adding the Headers
Spreadsheetformatcell(Workbook,format2,1,1);
Spreadsheetformatcell(Workbook,format1,3,1);
Spreadsheetformatcell(Workbook,format1,5,1);
Spreadsheetformatcell(Workbook,format1,5,2);
Spreadsheetformatcell(Workbook,format1,5,3);
Spreadsheetformatcell(Workbook,format1,5,4);
Spreadsheetformatcell(Workbook,format1,5,5);
Spreadsheetformatcell(Workbook,format1,5,6);
Spreadsheetformatcell(Workbook,format1,5,7);
Spreadsheetformatcell(Workbook,format1,5,8);
SpreadSheetSetCellValue(Workbook,"Executive Summary Report",1,1);
SpreadSheetSetCellValue(Workbook,"#dateFormat(now(),'mm/dd/yyyy')#",3,1);
SpreadSheetSetCellValue(Workbook,"Data Date",5,1);
SpreadSheetSetCellValue(Workbook,"Level",5,2);
SpreadSheetSetCellValue(Workbook,"Name",5,3);
SpreadSheetSetCellValue(Workbook,"Description",5,4);
SpreadSheetSetCellValue(Workbook,"Budget",5,5);
SpreadSheetSetCellValue(Workbook,"Commited",5,6);
SpreadSheetSetCellValue(Workbook,"Spent YTD",5,7);
SpreadSheetSetCellValue(Workbook,"% Spent",5,8);
arr=server.ColdFusion.ProductVersion;
if (qExecSummary.recordCount) {
rowNum = 6;
do {
SpreadSheetSetCellValue(Workbook,dateFormat(qRpt_Date.rpt_date,'mm/dd/yyy'),rowNum,1);
SpreadSheetSetCellValue(Workbook,qExecSummary.responsible[rowNum-5],rowNum,2);
SpreadSheetSetCellValue(Workbook,qExecSummary.name[rowNum-5],rowNum,3);
SpreadSheetSetCellValue(Workbook,qExecSummary.Description[rowNum-5],rowNum,4);
SpreadSheetSetCellValue(Workbook,qExecSummary.bud_sum[rowNum-5],rowNum,5);
SpreadSheetSetCellValue(Workbook,qExecSummary.committed[rowNum-5],rowNum,6);
SpreadSheetSetCellValue(Workbook,qExecSummary.Spent_YTD[rowNum-5],rowNum,7);
if (qExecSummary.bud_sum[rowNum-5] NEQ 0){
Spreadsheetformatcell(Workbook,formatNum,rowNum,8);
SpreadSheetSetCellValue(Workbook,(qExecSummary.Spent_YTD[rowNum-5]/qExecSummary.bud_sum[rowNum-5]),rowNum,8);
}
else {
SpreadSheetSetCellValue(Workbook,0,rowNum,8);
}
rowNum++;
} while (rowNum - 6 LT qExecSummary.recordCount);
} else {
SpreadSheetAddRows(Workbook,"No results for your criteria.");
}
SpreadsheetCreateSheet(Workbook,"ExecSummaryTest");
SpreadsheetSetActiveSheet(Workbook,"ExecSummaryTest");
//Formatting
format1.bold="true";
format1.fontsize=12;
format1.font="Calibri";
format2.bold="true";
format2.fontsize=18;
format2.font="Calibri";
formatNum.dataformat="0.0%";
Spreadsheetformatcell(Workbook,format2,1,1);
Spreadsheetformatcell(Workbook,format1,3,1);
Spreadsheetformatcell(Workbook,format1,5,1);
Spreadsheetformatcell(Workbook,format1,5,2);
Spreadsheetformatcell(Workbook,format1,5,3);
Spreadsheetformatcell(Workbook,format1,5,4);
Spreadsheetformatcell(Workbook,format1,5,5);
Spreadsheetformatcell(Workbook,format1,5,6);
Spreadsheetformatcell(Workbook,format1,5,7);
Spreadsheetformatcell(Workbook,format1,5,8);
SpreadSheetSetCellValue(Workbook,"Executive Summary Report",1,1);
SpreadSheetSetCellValue(Workbook,"#dateFormat(now(),'mm/dd/yyyy')#",3,1);
SpreadSheetSetCellValue(Workbook,"Data Date",5,1);
SpreadSheetSetCellValue(Workbook,"Level",5,2);
SpreadSheetSetCellValue(Workbook,"Name",5,3);
SpreadSheetSetCellValue(Workbook,"Description",5,4);
SpreadSheetSetCellValue(Workbook,"Budget",5,5);
SpreadSheetSetCellValue(Workbook,"Commited",5,6);
SpreadSheetSetCellValue(Workbook,"Spent YTD",5,7);
SpreadSheetSetCellValue(Workbook,"% Spent",5,8);
arr=server.ColdFusion.ProductVersion;
if (qExecSummary331.recordCount) {
rowNum = 6;
do {
SpreadSheetSetCellValue(Workbook,dateFormat(qRpt_Date.rpt_date,'mm/dd/yyy'),rowNum,1);
SpreadSheetSetCellValue(Workbook,qExecSummary331.responsible[rowNum-5],rowNum,2);
SpreadSheetSetCellValue(Workbook,qExecSummary331.name[rowNum-5],rowNum,3);
SpreadSheetSetCellValue(Workbook,qExecSummary331.Description[rowNum-5],rowNum,4);
SpreadSheetSetCellValue(Workbook,qExecSummary331.bud_sum[rowNum-5],rowNum,5);
SpreadSheetSetCellValue(Workbook,qExecSummary331.committed[rowNum-5],rowNum,6);
SpreadSheetSetCellValue(Workbook,qExecSummary331.Spent_YTD[rowNum-5],rowNum,7);
if (qExecSummary331.bud_sum[rowNum-5] NEQ 0){
Spreadsheetformatcell(Workbook,formatNum,rowNum,8);
SpreadSheetSetCellValue(Workbook,(qExecSummary331.Spent_YTD[rowNum-5]/qExecSummary331.bud_sum[rowNum-5]),rowNum,8);
}
else {
SpreadSheetSetCellValue(Workbook,0,rowNum,8);
}
rowNum++;
} while (rowNum - 6 LT qExecSummary331.recordCount);
} else {
SpreadSheetAddRows(Workbook,"No results for your criteria.");
}
SpreadsheetSetActiveSheet(Workbook,"ExecSummary");
</cfscript>
<cfheader name="Content-Disposition" value='attachment; filename="execSummaryNew.xls"'>
<cfcontent type="application/msexcel" variable="#SpreadsheetReadBinary(Workbook)#" reset="true">
WriteOutput() is for displaying data on screen. To add data to a spreadsheet, you need to use spreadsheet functions. In your scenario, you can use a for/in loop to iterate through the query rows. Then use SpreadSheetSetCellValue(sheet, value, row, col) 填充单个单元格。
更新 1:
如果您在全新的 .cfm 脚本中 运行 下面的代码(来自 "Edit 2"),您会发现它运行良好。因此,任何错误都必须来自代码的不同部分。如果您在追踪错误时遇到困难,我建议您从头开始。使用非常 简单 代码创建一个全新的脚本:仅是生成电子表格所需的最少代码。省略任何额外的功能、下载等,让基本代码首先运行。
<cfscript>
// FOR DEMO ONLY: Create manual queries
qRpt_Date = queryNew("");
queryAddColumn(qRpt_Date, "Rpt_Date", [now()]);
qExecSummary = queryNew("");
queryAddColumn(qExecSummary, "responsible", [1,12,13]);
queryAddColumn(qExecSummary, "bud_sum", [0,50,100]);
queryAddColumn(qExecSummary, "Spent_YTD", [0,50,100]);
queryAddColumn(qExecSummary, "Name", ["Name A","Name B","Name C"]);
queryAddColumn(qExecSummary, "Description", ["Description A","DescriptionB","Description C"]);
queryAddColumn(qExecSummary, "Committed", [0,50,100]);
//Create new workbook with one sheet
//by default that sheet is the active sheet
Workbook = SpreadsheetNew("ExecSummary");
//Add Headers
headerTextFormat.bold=true;
Spreadsheetformatcell(Workbook,headerTextFormat,1,1);
SpreadSheetSetCellValue(Workbook,"Executive Summary Report",1,1);
Spreadsheetformatcell(Workbook,headerTextFormat,3,1);
SpreadSheetSetCellValue(Workbook, dateFormat(now(),'mm/dd/yyyy'),3,1);
SpreadSheetSetCellValue(Workbook,"Data Date",5,1);
SpreadSheetSetCellValue(Workbook,"Level",5,2);
SpreadSheetSetCellValue(Workbook,"Name",5,3);
SpreadSheetSetCellValue(Workbook,"Description",5,4);
SpreadSheetSetCellValue(Workbook,"Budget",5,5);
SpreadSheetSetCellValue(Workbook,"Commited",5,6);
SpreadSheetSetCellValue(Workbook,"Spent YTD",5,7);
SpreadSheetSetCellValue(Workbook,"% Spent",5,8);
//Add data detail
if (qExecSummary.recordCount) {
// Start populating the spreadsheet on this row number
// Change as needed.
rowNum = 6;
// Loop through query rows
for(dataRow in qExecSummary) {
// add
SpreadSheetSetCellValue(Workbook,dateFormat(qRpt_Date.rpt_Date, 'mm/dd/yyyy'),rowNum,1);
SpreadSheetSetCellValue(Workbook,dataRow.responsible,rowNum,2);
SpreadSheetSetCellValue(Workbook,dataRow.name,rowNum,3);
SpreadSheetSetCellValue(Workbook,dataRow.Description,rowNum,4);
SpreadSheetSetCellValue(Workbook,dataRow.bud_sum,rowNum,5);
SpreadSheetSetCellValue(Workbook,dataRow.committed,rowNum,6);
SpreadSheetSetCellValue(Workbook,dataRow.Spent_YTD,rowNum,7);
if (qExecSummary.bud_sum != 0){
Spreadsheetformatcell(Workbook,formatNum,rowNum,8);
SpreadSheetSetCellValue(Workbook,(qExecSummary.Spent_YTD/qExecSummary.bud_sum)*100,rowNum,8);
}
else {
SpreadSheetSetCellValue(Workbook,0,rowNum,8);
}
rowNum++;
}
//End of WriteOutput
} else {
SpreadSheetAddRows(Workbook,"No results for your criteria.");
}
//For test purposes, save the results to a file
SpreadSheetWrite(Workbook, "c:/path/to/yourFile.xls", true);
WriteOutput("Done!");
</cfscript>
更新二:
如评论中所述,for..in
查询循环在 CF9 中不受支持。如果您使用的是旧版本的 CF,则需要使用 from/to
循环。
// Loop through query rows
for(x = 1; x lte yourQueryName.recordCount; x++) {
SpreadSheetSetCellValue(Workbook, yourQueryName.YourColumnName[x], theSheetRowNum, 1);
// ...
theSheetRowNum++;
}
旁注,我个人更喜欢 cfscript。但是,如果您对 CFML 感觉更舒服,您可以随时重写 。只需删除 cfscript 标签并替换:
<cfscript>
SomeFunctionName(....);
</cfscript>
与:
<cfset SomeFunctionName(....)>
我正在使用 ColdFusion 2016,我发现我使用的服务器只有 CF9,我对此很陌生。我一直致力于更新现有代码以满足用户现在的需求。到目前为止,我已经成功了,但这超出了我的范围。我有一个网站可以生成一个 excel 工作簿,其中有一个 sheet。它使用 HTML 和一个查询来创建它。从 A1 中的查询名称开始,A3 中的报告日期,A5:H5 中的 table headers,然后是 A6:H53 中的数据(确切长度可能有所不同,但始终从A - H 列)。我将 post 用于创建工作簿的内容。我想要做的是再使用 3 个查询向工作簿中再添加 3 个 sheet。我尝试添加一个我发现 here 的函数,但没有任何用处。我尝试修改现有代码以尝试使用所有 4 个查询,但没有任何乐趣。
如有任何帮助,我们将不胜感激。让我知道是否需要添加更多详细信息。 这是代码:(我为我添加的内容添加了评论,试图让它工作)
<cfsilent>
<!--- *******************************************************************
Filename: execSummary_Excel.cfm, v1.0 03/07/2012
Created By: Original Writer
Description: Excel report export for Executive Summary Report.
Change History:
Date........Name...........Description of Change........................
08/01/2012 Original Writer Added committed column.
02/28/2013 Original Writer Added stateGM and GM.
*************************************************************************--->
<cfinvoke component="financial.financial" method="getExecSummary" returnvariable="qExecSummary">
<cfinvokeargument name="level" value="#URL.level#" />
<cfinvokeargument name="stateGM" value="#URL.stateGM#" />
<cfinvokeargument name="GM" value="#URL.GM#" />
<cfinvokeargument name="engVP" value="#URL.engVP#" />
<cfinvokeargument name="engDir" value="#URL.engDir#" />
</cfinvoke>
<!---Added this to test if I can get more than one sheet to the Workbook--->
<cfinvoke component="financial.financial" method="getExecSummary_OLD" returnvariable="qExecSummary_OLD">
<cfinvokeargument name="level" value="#URL.level#" />
<cfinvokeargument name="stateGM" value="#URL.stateGM#" />
<cfinvokeargument name="GM" value="#URL.GM#" />
<cfinvokeargument name="engVP" value="#URL.engVP#" />
<cfinvokeargument name="engDir" value="#URL.engDir#" />
</cfinvoke>
<!--- Get Report Date since qExecSummary is more complex than the other report queries --->
<cfquery name="qRpt_Date" datasource="#application.dsn#">
SELECT DISTINCT rpt_date
FROM fin_data
</cfquery>
<cfsetting requesttimeout="5000" />
</cfsilent>
<!---Added this as a function that should allow me to use multiple queries and create a workbook with more than one sheet--->
<cfscript>
//Create new workbook with one sheet
//by default that sheet is the active sheet
Workbook = SpreadsheetNew("ExecSummary");
//Add Data to the sheet
format1.bold="true";
formatNum.dataformat="0.00%";
Spreadsheetformatcell(Workbook,format1,1,1);
SpreadSheetSetCellValue(Workbook,"Executive Summary Report",1,1);
Spreadsheetformatcell(Workbook,format1,3,1);
SpreadSheetSetCellValue(Workbook,"#dateFormat(now(),'mm/dd/yyyy')#",3,1);
SpreadSheetSetCellValue(Workbook,"Data Date",5,1);
SpreadSheetSetCellValue(Workbook,"Level",5,2);
SpreadSheetSetCellValue(Workbook,"Name",5,3);
SpreadSheetSetCellValue(Workbook,"Description",5,4);
SpreadSheetSetCellValue(Workbook,"Budget",5,5);
SpreadSheetSetCellValue(Workbook,"Commited",5,6);
SpreadSheetSetCellValue(Workbook,"Spent YTD",5,7);
SpreadSheetSetCellValue(Workbook,"% Spent",5,8);
arr="Here";
writedump(arr);
//if (qExecSummary.recordCount) {
// rowNum = 6;
// arr="Here";
// writedump(rowNum);
//alert(qExecSummary.recordCount);
//for(dataRow in qExecSummary){
//SpreadSheetSetCellValue(Workbook,dateFormat(qRpt_Date.rpt_Date, 'mm/dd/yyyy'),rowNum,1);
//SpreadSheetSetCellValue(Workbook,dataRow.responsible,rowNum,2);
//SpreadSheetSetCellValue(Workbook,dataRow.name,rowNum,3);
//SpreadSheetSetCellValue(Workbook,dataRow.Description,rowNum,4);
//SpreadSheetSetCellValue(Workbook,dataRow.bud_sum,rowNum,5);
//SpreadSheetSetCellValue(Workbook,dataRow.committed,rowNum,6);
//SpreadSheetSetCellValue(Workbook,dataRow.Spent_YTD,rowNum,7);
/*if (qExecSummary.bud_sum NEQ 0){
Spreadsheetformatcell(Workbook,formatNum,rowNum,8);
//percentSpent="#(qExecSummary.Spent_YTD/qExecSummary.bud_sum)*100#";
SpreadSheetSetCellValue(Workbook,(dataRow.Spent_YTD/dataRow.bud_sum)*100,rowNum,8);
}
else {*/
//SpreadSheetSetCellValue(Workbook,0,rowNum,8);
//}
//rowNum++;
//}
//End of WriteOutput
//} else {
// SpreadSheetAddRows(Workbook,"No results for your criteria.");
//}
</cfscript>
<!---
<cffunction name="QueriesToXLS" access="public">
<cfargument name="queryArr" required="true">
<cfargument name="sheetNameArr" required="false">
<cfset tempPath="C:\Temp\ExecutiveSummary" & ".xls">
<cfset counter= 1>
<cfloop array="#ARGUMENTS.queryArr#" index="i" >
<cfset sheetName="Sheet#counter#">
<cfif isDefined("ARGUMENTS.sheetNameArr")>
<cfset sheetName=ARGUMENTS.sheetNameArr[counter]>
</cfif>
<cfspreadsheet action="update" filename="#tempPath#" query="i" sheetName="#sheetName#"/>
<cfset counter += 1>
</cfloop>
<cfreturn SpreadsheetRead(tempPath)>
</cffunction>
<cfset xlsData = QueriesToXLS([qExecSummary,qExecSummary],["ExecutiveSummary","ExecutiveSummaryOLD"])>
--->
<cfheader name="Content-Disposition" value='attachment; filename="execSummaryNew.xls"'>
<!---cfcontent type="application/msexcel" variable="#SpreadsheetReadBinary(xlsData)#" reset="true"--->
<cfcontent type="application/vnd.ms-excel"> <!---This is where the application type is being set to Excel--->
<!---html xmlns:x="urn:schemas-microsoft-com:office:excel">
<head>
</head>
<h2>Executive Summary Report</h2>
<cfoutput>
<p>#dateFormat(now(), 'mm/dd/yyyy')#</p>
</cfoutput>
<table id="tableOne" class="yui" cellspacing="0" cellpadding="5">
<thead>
<tr>
<th>Data Date</th>
<th>Level</th>
<th>Name</th>
<th>Description</th>
<th>Budget</th>
<th>Committed</th>
<th>Spent YTD</th>
<th>% Spent</th>
</tr>
</thead>
<cfif qExecSummary.recordCount>
<tbody>
<cfoutput query="qExecSummary">
<tr>
<td>#dateFormat(qRpt_Date.rpt_Date, 'mm/dd/yyyy')#</td>
<td>#qExecSummary.responsible#</td>
<td>#qExecSummary.name#</td>
<td>#qExecSummary.Description#</td>
<td>#qExecSummary.bud_sum#</td>
<td>#qExecSummary.committed#</td>
<td>#qExecSummary.Spent_YTD#</td>
<td><cfif qExecSummary.bud_sum NEQ 0>
#numberFormat((qExecSummary.Spent_YTD/qExecSummary.bud_sum)*100,"9.9")#%
<cfelse>
0
</cfif>
</td>
</tr>
</cfoutput>
</tbody>
<cfelse>
<tr>
<td colspan="9">No results for your criteria.</td>
</tr>
</cfif>
</table>
</html--->
编辑
我已尝试按照副本中引用的问题提供的答案进行操作。我无法让它与我的查询一起使用。我添加了以下代码来代替我的脚本:
编辑 2
已更新此脚本,但仍然无法正常工作。 writeOutput()
上的错误。我不确定如何使用查询来为行创建数据?
<cfscript>
//Create new workbook with one sheet
//by default that sheet is the active sheet
Workbook = SpreadsheetNew("ExecSummary");
//Add Data to the sheet
format1.bold="true";
formatNum.dataformat="0.00%";
Spreadsheetformatcell(Workbook,format1,1,1);
SpreadSheetSetCellValue(Workbook,"Executive Summary Report",1,1);
Spreadsheetformatcell(Workbook,format1,3,1);
SpreadSheetSetCellValue(Workbook,"#dateFormat(now(),'mm/dd/yyyy')#",3,1);
SpreadSheetSetCellValue(Workbook,"Data Date",5,1);
SpreadSheetSetCellValue(Workbook,"Level",5,2);
SpreadSheetSetCellValue(Workbook,"Name",5,3);
SpreadSheetSetCellValue(Workbook,"Description",5,4);
SpreadSheetSetCellValue(Workbook,"Budget",5,5);
SpreadSheetSetCellValue(Workbook,"Commited",5,6);
SpreadSheetSetCellValue(Workbook,"Spent YTD",5,7);
SpreadSheetSetCellValue(Workbook,"% Spent",5,8);
if (qExecSummary.recordCount) {
rowNum = 6;
//writeOutput(query="qExecSummary");
for(dataRow in qExecSummary){
SpreadSheetSetCellValue(Workbook,dateFormat(qRpt_Date.rpt_Date, 'mm/dd/yyyy'),rowNum,1);
SpreadSheetSetCellValue(Workbook,dataRow.responsible,rowNum,2);
SpreadSheetSetCellValue(Workbook,dataRow.name,rowNum,3);
SpreadSheetSetCellValue(Workbook,dataRow.Description,rowNum,4);
SpreadSheetSetCellValue(Workbook,dataRow.bud_sum,rowNum,5);
SpreadSheetSetCellValue(Workbook,dataRow.committed,rowNum,6);
SpreadSheetSetCellValue(Workbook,dataRow.Spent_YTD,rowNum,7);
if (qExecSummary.bud_sum NEQ 0){
Spreadsheetformatcell(Workbook,formatNum,rowNum,8);
percentSpent="#(qExecSummary.Spent_YTD/qExecSummary.bud_sum)*100#";
SpreadSheetSetCellValue(Workbook,(qExecSummary.Spent_YTD/qExecSummary.bud_sum)*100,rowNum,8);
}
else {
SpreadSheetSetCellValue(Workbook,0,rowNum,8);
}
rowNum++;
}
//End of WriteOutput
} else {
SpreadSheetAddRows(Workbook,"No results for your criteria.");
}
</cfscript>
但是它在 Output
行上显示错误。我不知道该改成什么。我搜索了 cfscript
标签中可用的内容并找到了 this,但没有任何内容看起来有帮助?
已更新 <cfscript>
现在我收到此错误:
最终编辑
这是工作脚本:
<cfsilent>
<!--- *******************************************************************
Filename: execSummary_Excel.cfm, v1.0 03/07/2012
Created By: Original Writer
Description: Excel report export for Executive Summary Report.
Change History:
Date........Name...........Description of Change........................
08/01/2012 Original Writer Added committed column.
02/28/2013 Original Writer Added stateGM and GM.
*************************************************************************--->
<cfinvoke component="financial.financial" method="getExecSummary" returnvariable="qExecSummary">
<cfinvokeargument name="level" value="#URL.level#" />
<cfinvokeargument name="stateGM" value="#URL.stateGM#" />
<cfinvokeargument name="GM" value="#URL.GM#" />
<cfinvokeargument name="engVP" value="#URL.engVP#" />
<cfinvokeargument name="engDir" value="#URL.engDir#" />
</cfinvoke>
<!---Added this to test if I can get more than one sheet to the Workbook--->
<cfinvoke component="financial.financial" method="getExecSummary331" returnvariable="qExecSummary331">
<cfinvokeargument name="level" value="#URL.level#" />
<cfinvokeargument name="stateGM" value="#URL.stateGM#" />
<cfinvokeargument name="GM" value="#URL.GM#" />
<cfinvokeargument name="engVP" value="#URL.engVP#" />
<cfinvokeargument name="engDir" value="#URL.engDir#" />
</cfinvoke>
<!--- Get Report Date since qExecSummary is more complex than the other report queries --->
<cfquery name="qRpt_Date" datasource="#application.dsn#">
SELECT DISTINCT rpt_date
FROM fin_data
</cfquery>
<cfsetting requesttimeout="5000" />
</cfsilent>
<!---Added this as a function that should allow me to use multiple queries and create a workbook with more than one sheet--->
<cfscript>
//Create new workbook with one sheet
//by default that sheet is the active sheet
Workbook = SpreadsheetNew("ExecSummary");
//Add Data to the sheet
//Formatting
format1.bold="true";
format1.fontsize=12;
format1.font="Calibri";
format2.bold="true";
format2.fontsize=18;
format2.font="Calibri";
formatNum.dataformat="0.0%";
//adding the Headers
Spreadsheetformatcell(Workbook,format2,1,1);
Spreadsheetformatcell(Workbook,format1,3,1);
Spreadsheetformatcell(Workbook,format1,5,1);
Spreadsheetformatcell(Workbook,format1,5,2);
Spreadsheetformatcell(Workbook,format1,5,3);
Spreadsheetformatcell(Workbook,format1,5,4);
Spreadsheetformatcell(Workbook,format1,5,5);
Spreadsheetformatcell(Workbook,format1,5,6);
Spreadsheetformatcell(Workbook,format1,5,7);
Spreadsheetformatcell(Workbook,format1,5,8);
SpreadSheetSetCellValue(Workbook,"Executive Summary Report",1,1);
SpreadSheetSetCellValue(Workbook,"#dateFormat(now(),'mm/dd/yyyy')#",3,1);
SpreadSheetSetCellValue(Workbook,"Data Date",5,1);
SpreadSheetSetCellValue(Workbook,"Level",5,2);
SpreadSheetSetCellValue(Workbook,"Name",5,3);
SpreadSheetSetCellValue(Workbook,"Description",5,4);
SpreadSheetSetCellValue(Workbook,"Budget",5,5);
SpreadSheetSetCellValue(Workbook,"Commited",5,6);
SpreadSheetSetCellValue(Workbook,"Spent YTD",5,7);
SpreadSheetSetCellValue(Workbook,"% Spent",5,8);
arr=server.ColdFusion.ProductVersion;
if (qExecSummary.recordCount) {
rowNum = 6;
do {
SpreadSheetSetCellValue(Workbook,dateFormat(qRpt_Date.rpt_date,'mm/dd/yyy'),rowNum,1);
SpreadSheetSetCellValue(Workbook,qExecSummary.responsible[rowNum-5],rowNum,2);
SpreadSheetSetCellValue(Workbook,qExecSummary.name[rowNum-5],rowNum,3);
SpreadSheetSetCellValue(Workbook,qExecSummary.Description[rowNum-5],rowNum,4);
SpreadSheetSetCellValue(Workbook,qExecSummary.bud_sum[rowNum-5],rowNum,5);
SpreadSheetSetCellValue(Workbook,qExecSummary.committed[rowNum-5],rowNum,6);
SpreadSheetSetCellValue(Workbook,qExecSummary.Spent_YTD[rowNum-5],rowNum,7);
if (qExecSummary.bud_sum[rowNum-5] NEQ 0){
Spreadsheetformatcell(Workbook,formatNum,rowNum,8);
SpreadSheetSetCellValue(Workbook,(qExecSummary.Spent_YTD[rowNum-5]/qExecSummary.bud_sum[rowNum-5]),rowNum,8);
}
else {
SpreadSheetSetCellValue(Workbook,0,rowNum,8);
}
rowNum++;
} while (rowNum - 6 LT qExecSummary.recordCount);
} else {
SpreadSheetAddRows(Workbook,"No results for your criteria.");
}
SpreadsheetCreateSheet(Workbook,"ExecSummaryTest");
SpreadsheetSetActiveSheet(Workbook,"ExecSummaryTest");
//Formatting
format1.bold="true";
format1.fontsize=12;
format1.font="Calibri";
format2.bold="true";
format2.fontsize=18;
format2.font="Calibri";
formatNum.dataformat="0.0%";
Spreadsheetformatcell(Workbook,format2,1,1);
Spreadsheetformatcell(Workbook,format1,3,1);
Spreadsheetformatcell(Workbook,format1,5,1);
Spreadsheetformatcell(Workbook,format1,5,2);
Spreadsheetformatcell(Workbook,format1,5,3);
Spreadsheetformatcell(Workbook,format1,5,4);
Spreadsheetformatcell(Workbook,format1,5,5);
Spreadsheetformatcell(Workbook,format1,5,6);
Spreadsheetformatcell(Workbook,format1,5,7);
Spreadsheetformatcell(Workbook,format1,5,8);
SpreadSheetSetCellValue(Workbook,"Executive Summary Report",1,1);
SpreadSheetSetCellValue(Workbook,"#dateFormat(now(),'mm/dd/yyyy')#",3,1);
SpreadSheetSetCellValue(Workbook,"Data Date",5,1);
SpreadSheetSetCellValue(Workbook,"Level",5,2);
SpreadSheetSetCellValue(Workbook,"Name",5,3);
SpreadSheetSetCellValue(Workbook,"Description",5,4);
SpreadSheetSetCellValue(Workbook,"Budget",5,5);
SpreadSheetSetCellValue(Workbook,"Commited",5,6);
SpreadSheetSetCellValue(Workbook,"Spent YTD",5,7);
SpreadSheetSetCellValue(Workbook,"% Spent",5,8);
arr=server.ColdFusion.ProductVersion;
if (qExecSummary331.recordCount) {
rowNum = 6;
do {
SpreadSheetSetCellValue(Workbook,dateFormat(qRpt_Date.rpt_date,'mm/dd/yyy'),rowNum,1);
SpreadSheetSetCellValue(Workbook,qExecSummary331.responsible[rowNum-5],rowNum,2);
SpreadSheetSetCellValue(Workbook,qExecSummary331.name[rowNum-5],rowNum,3);
SpreadSheetSetCellValue(Workbook,qExecSummary331.Description[rowNum-5],rowNum,4);
SpreadSheetSetCellValue(Workbook,qExecSummary331.bud_sum[rowNum-5],rowNum,5);
SpreadSheetSetCellValue(Workbook,qExecSummary331.committed[rowNum-5],rowNum,6);
SpreadSheetSetCellValue(Workbook,qExecSummary331.Spent_YTD[rowNum-5],rowNum,7);
if (qExecSummary331.bud_sum[rowNum-5] NEQ 0){
Spreadsheetformatcell(Workbook,formatNum,rowNum,8);
SpreadSheetSetCellValue(Workbook,(qExecSummary331.Spent_YTD[rowNum-5]/qExecSummary331.bud_sum[rowNum-5]),rowNum,8);
}
else {
SpreadSheetSetCellValue(Workbook,0,rowNum,8);
}
rowNum++;
} while (rowNum - 6 LT qExecSummary331.recordCount);
} else {
SpreadSheetAddRows(Workbook,"No results for your criteria.");
}
SpreadsheetSetActiveSheet(Workbook,"ExecSummary");
</cfscript>
<cfheader name="Content-Disposition" value='attachment; filename="execSummaryNew.xls"'>
<cfcontent type="application/msexcel" variable="#SpreadsheetReadBinary(Workbook)#" reset="true">
WriteOutput() is for displaying data on screen. To add data to a spreadsheet, you need to use spreadsheet functions. In your scenario, you can use a for/in loop to iterate through the query rows. Then use SpreadSheetSetCellValue(sheet, value, row, col) 填充单个单元格。
更新 1:
如果您在全新的 .cfm 脚本中 运行 下面的代码(来自 "Edit 2"),您会发现它运行良好。因此,任何错误都必须来自代码的不同部分。如果您在追踪错误时遇到困难,我建议您从头开始。使用非常 简单 代码创建一个全新的脚本:仅是生成电子表格所需的最少代码。省略任何额外的功能、下载等,让基本代码首先运行。
<cfscript>
// FOR DEMO ONLY: Create manual queries
qRpt_Date = queryNew("");
queryAddColumn(qRpt_Date, "Rpt_Date", [now()]);
qExecSummary = queryNew("");
queryAddColumn(qExecSummary, "responsible", [1,12,13]);
queryAddColumn(qExecSummary, "bud_sum", [0,50,100]);
queryAddColumn(qExecSummary, "Spent_YTD", [0,50,100]);
queryAddColumn(qExecSummary, "Name", ["Name A","Name B","Name C"]);
queryAddColumn(qExecSummary, "Description", ["Description A","DescriptionB","Description C"]);
queryAddColumn(qExecSummary, "Committed", [0,50,100]);
//Create new workbook with one sheet
//by default that sheet is the active sheet
Workbook = SpreadsheetNew("ExecSummary");
//Add Headers
headerTextFormat.bold=true;
Spreadsheetformatcell(Workbook,headerTextFormat,1,1);
SpreadSheetSetCellValue(Workbook,"Executive Summary Report",1,1);
Spreadsheetformatcell(Workbook,headerTextFormat,3,1);
SpreadSheetSetCellValue(Workbook, dateFormat(now(),'mm/dd/yyyy'),3,1);
SpreadSheetSetCellValue(Workbook,"Data Date",5,1);
SpreadSheetSetCellValue(Workbook,"Level",5,2);
SpreadSheetSetCellValue(Workbook,"Name",5,3);
SpreadSheetSetCellValue(Workbook,"Description",5,4);
SpreadSheetSetCellValue(Workbook,"Budget",5,5);
SpreadSheetSetCellValue(Workbook,"Commited",5,6);
SpreadSheetSetCellValue(Workbook,"Spent YTD",5,7);
SpreadSheetSetCellValue(Workbook,"% Spent",5,8);
//Add data detail
if (qExecSummary.recordCount) {
// Start populating the spreadsheet on this row number
// Change as needed.
rowNum = 6;
// Loop through query rows
for(dataRow in qExecSummary) {
// add
SpreadSheetSetCellValue(Workbook,dateFormat(qRpt_Date.rpt_Date, 'mm/dd/yyyy'),rowNum,1);
SpreadSheetSetCellValue(Workbook,dataRow.responsible,rowNum,2);
SpreadSheetSetCellValue(Workbook,dataRow.name,rowNum,3);
SpreadSheetSetCellValue(Workbook,dataRow.Description,rowNum,4);
SpreadSheetSetCellValue(Workbook,dataRow.bud_sum,rowNum,5);
SpreadSheetSetCellValue(Workbook,dataRow.committed,rowNum,6);
SpreadSheetSetCellValue(Workbook,dataRow.Spent_YTD,rowNum,7);
if (qExecSummary.bud_sum != 0){
Spreadsheetformatcell(Workbook,formatNum,rowNum,8);
SpreadSheetSetCellValue(Workbook,(qExecSummary.Spent_YTD/qExecSummary.bud_sum)*100,rowNum,8);
}
else {
SpreadSheetSetCellValue(Workbook,0,rowNum,8);
}
rowNum++;
}
//End of WriteOutput
} else {
SpreadSheetAddRows(Workbook,"No results for your criteria.");
}
//For test purposes, save the results to a file
SpreadSheetWrite(Workbook, "c:/path/to/yourFile.xls", true);
WriteOutput("Done!");
</cfscript>
更新二:
如评论中所述,for..in
查询循环在 CF9 中不受支持。如果您使用的是旧版本的 CF,则需要使用 from/to
循环。
// Loop through query rows
for(x = 1; x lte yourQueryName.recordCount; x++) {
SpreadSheetSetCellValue(Workbook, yourQueryName.YourColumnName[x], theSheetRowNum, 1);
// ...
theSheetRowNum++;
}
旁注,我个人更喜欢 cfscript。但是,如果您对 CFML 感觉更舒服,您可以随时重写
<cfscript>
SomeFunctionName(....);
</cfscript>
与:
<cfset SomeFunctionName(....)>