简体   繁体   English

使用查询创建具有多个工作表的工作簿

[英]Create workbook with multiple sheets from Queries

I am using ColdFusion 2016 and I've discovered that the server that I'm using only has CF9, I'm pretty new to this. 我正在使用ColdFusion 2016,并且发现我所使用的服务器仅具有CF9,对此我还很陌生。 I've been working on updating existing code to fit what the users now want. 我一直在努力更新现有代码,以适应用户现在的需求。 So far I've managed, but this is beyond me. 到目前为止,我已经做到了,但是这超出了我的范围。 I've got a website that generates an excel workbook with one sheet. 我有一个网站,可以用一张纸生成一个Excel工作簿。 It uses HTML and a query to create it. 它使用HTML和查询来创建它。 Starts with the query name in A1 the report date in A3, the table headers in A5:H5 and then the data in A6:H53 (The exact length could vary, but always from column A - H). 从A1中的查询名称开始,A3中的报告日期开始,A5:H5中的表标题然后A6:H53中的数据开始(确切的长度可能有所不同,但始终在A-H列中)。 I'll post what is being used to create the workbook. 我将发布用于创建工作簿的内容。 What i want to do is use 3 more queries to add 3 more sheets to the workbook. 我想做的是使用3个以上的查询向工作簿中添加3个以上的工作表。 I've tried adding a function that I found here and that didn't do any good. 我试图添加一个我在这里找到的功能,但效果不佳。 I tried modifying the existing code some to try and use all 4 queries, no joy. 我尝试对现有代码进行一些修改,以尝试使用所有4个查询,这并不令人高兴。

Any help would be appreciated. 任何帮助,将不胜感激。 Let me know if I need to add more detail. 让我知道是否需要添加更多细节。 Here's the code:(I added comments for what I added trying to get this to work) 这是代码:(我添加了一些注释,试图使它正常工作)

<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--->

EDIT 编辑

I have tried following the answer provided on the question referenced in the duplicate. 我已尝试按照重复项中所提问题的答案进行操作。 I am not able to get it to work with the query that I have. 我无法使其与我拥有的查询一起使用。 I have added the following code in place of the script that I had: 我添加了以下代码来代替我拥有的脚本:

EDIT 2 编辑2

Updated this script, still not working. 更新了此脚本,仍然无法正常工作。 errors on the writeOutput() .I'm not sure how to implement using a query to create the data for the rows? 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>

However it is showing errors on the line with the Output . 但是,它在与Output一起的行中显示错误。 I don't know what to change it to. 我不知道将其更改为什么。 I've searched for what is usable in the cfscript tags and found this , but there's nothing there that looks like it would help? 我已经搜索了cfscript标记中的可用内容,并找到 ,但是没有什么看起来像是有用的?

Made update to the <cfscript> now I'm getting this error: 更新到<cfscript>现在出现此错误:

来自新更新的脚本的错误消息

FINAL EDIT 最后编辑

Here's the working script: 这是工作脚本:

<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. WriteOutput()用于在屏幕上显示数据。 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. 在您的方案中,可以使用for / in循环遍历查询行。 Then use SpreadSheetSetCellValue(sheet, value, row, col) to populate the individual cells. 然后使用SpreadSheetSetCellValue(sheet,value,row,col)填充单个单元格。

Update 1: 更新1:

If you run the code below (from "Edit 2"), in a brand new .cfm script, you will see it works perfectly. 如果您运行下面的代码(来自“ Edit 2”),则在全新的.cfm脚本中,您将看到它运行正常。 So any errors must be coming from a different part of the code. 因此,任何错误都必须来自代码的不同部分。 If you are having trouble tracking down errors, I would recommend starting over with a clean slate. 如果您在查找错误时遇到麻烦,建议您从头开始。 Create a brand new script with very simple code: just the minimum necessary to generate the spreadsheet. 使用非常简单的代码创建一个全新的脚本:仅是生成电子表格所需的最少代码。 Omit anything extra like functions, downloads, etcetera and get the basic code working first. 省略任何其他功能,例如下载,下载等,并让基本代码首先生效。

<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>

Update 2: 更新2:

As noted in the comments, for..in query loops are not supported in CF9. 如注释中所述,CF9不支持for..in查询循环。 If you are using an older version of CF, then you will need to use a from/to loop instead. 如果您使用的是旧版CF,则需要使用from/to循环。

// Loop through query rows
for(x = 1; x lte yourQueryName.recordCount; x++) {
    SpreadSheetSetCellValue(Workbook, yourQueryName.YourColumnName[x], theSheetRowNum, 1);
    // ...

    theSheetRowNum++;
} 

Side note, personally I prefer cfscript. 旁注,我个人更喜欢cfscript。 However, if you feel more comfortable with CFML you can always rewrite the example . 但是,如果您对CFML更加满意,可以随时重写该示例 Just get rid of the cfscript tags and replace: 只需摆脱cfscript标记并替换:

<cfscript>
    SomeFunctionName(....); 
</cfscript>

with: 与:

<cfset SomeFunctionName(....)>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM