ColdFusion9如何在<cfscript>中对excel中的列进行格式化

我正在使用CF9。 我已经创build了一个多页工作簿。 我正在尝试格式化每个工作表的列。 格式只是在第一张纸上。 我怎样才能把它应用到所有床单? 此外,我不知道如何获得列宽工作的任何工作表。

这里是我现在得到的代码:

<cfscript> qExecSummary = queryNew(""); queryAddColumn(qExecSummary, "responsible", [1,12,13]); queryAddColumn(qExecSummary, "bud_sum", [100,500,1000]); queryAddColumn(qExecSummary, "Spent_YTD", [10,50,100]); queryAddColumn(qExecSummary, "Name", ["A","B","C"]); queryAddColumn(qExecSummary, "Description", ["Descrip1","Descrip2","Descrip3"]); queryAddColumn(qExecSummary, "Committed", ["Committed1","Committed2","Committed3"]); //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 formating to the cells //adding the Headers SpreadSheetSetCellValue(Workbook,"Executive Summary Report",1,1); Spreadsheetformatcell(Workbook,format2,1,1); SpreadSheetSetCellValue(Workbook,"#dateFormat(now(),'mm/dd/yyyy')#",3,1); Spreadsheetformatcell(Workbook,format1,3,1); SpreadSheetSetCellValue(Workbook,"Data Date",5,1); Spreadsheetformatcell(Workbook,format1,5,1); SpreadSheetSetColumnWidth(Workbook,1,10); SpreadSheetSetCellValue(Workbook,"Level",5,2); Spreadsheetformatcell(Workbook,format1,5,2); SpreadSheetSetColumnWidth(Workbook,2,10); SpreadSheetSetCellValue(Workbook,"Name",5,3); Spreadsheetformatcell(Workbook,format1,5,3); SpreadSheetSetColumnWidth(Workbook,3,17); SpreadSheetSetCellValue(Workbook,"Description",5,4); Spreadsheetformatcell(Workbook,format1,5,4); SpreadSheetSetColumnWidth(Workbook,4,20); SpreadSheetSetCellValue(Workbook,"Budget",5,5); Spreadsheetformatcell(Workbook,format1,5,5); SpreadSheetSetColumnWidth(Workbook,5,15); SpreadSheetSetCellValue(Workbook,"Commited",5,6); Spreadsheetformatcell(Workbook,format1,5,6); SpreadSheetSetColumnWidth(Workbook,6,15); SpreadSheetSetCellValue(Workbook,"Spent YTD",5,7); Spreadsheetformatcell(Workbook,format1,5,7); SpreadSheetSetColumnWidth(Workbook,7,15); SpreadSheetSetCellValue(Workbook,"% Spent",5,8); Spreadsheetformatcell(Workbook,format1,5,8); SpreadSheetSetColumnWidth(Workbook,8,15); //check to make sure that data was pulled back by the query if (qExecSummary.recordCount) { rowNum = 6; do {//if data is pulled back loop through it and add it to the correct cell SpreadSheetSetCellValue(Workbook,dateFormat(now(),'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);//if there is a percentage used then format as a percent 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,"ExecSummary331-333"); SpreadsheetSetActiveSheet(Workbook,"ExecSummary331-333"); //adding the Headers SpreadSheetSetCellValue(Workbook,"Executive Summary Report",1,1); Spreadsheetformatcell(Workbook,{bold="true"},1,1); Spreadsheetformatcell(Workbook,{fontsize=18},1,1); Spreadsheetformatcell(Workbook,{font="Calibri"},1,1); SpreadSheetSetCellValue(Workbook,"#dateFormat(now(),'mm/dd/yyyy')#",3,1); Spreadsheetformatcell(Workbook,format1,3,1); SpreadSheetSetCellValue(Workbook,"Data Date",5,1); Spreadsheetformatcell(Workbook,format1,5,1); SpreadSheetSetColumnWidth(Workbook,1,10); SpreadSheetSetCellValue(Workbook,"Level",5,2); Spreadsheetformatcell(Workbook,format1,5,2); SpreadSheetSetColumnWidth(Workbook,2,10); SpreadSheetSetCellValue(Workbook,"Name",5,3); Spreadsheetformatcell(Workbook,format1,5,3); SpreadSheetSetColumnWidth(Workbook,3,17); SpreadSheetSetCellValue(Workbook,"Description",5,4); Spreadsheetformatcell(Workbook,format1,5,4); SpreadSheetSetColumnWidth(Workbook,4,20); SpreadSheetSetCellValue(Workbook,"Budget",5,5); Spreadsheetformatcell(Workbook,format1,5,5); SpreadSheetSetColumnWidth(Workbook,5,15); SpreadSheetSetCellValue(Workbook,"Commited",5,6); Spreadsheetformatcell(Workbook,format1,5,6); SpreadSheetSetColumnWidth(Workbook,6,15); SpreadSheetSetCellValue(Workbook,"Spent YTD",5,7); Spreadsheetformatcell(Workbook,format1,5,7); SpreadSheetSetColumnWidth(Workbook,7,15); SpreadSheetSetCellValue(Workbook,"% Spent",5,8); Spreadsheetformatcell(Workbook,format1,5,8); SpreadSheetSetColumnWidth(Workbook,8,15); //check to make sure that data was pulled back by the query if (qExecSummary.recordCount) { rowNum = 6; do {//if data is pulled back loop through it and add it to the correct cell SpreadSheetSetCellValue(Workbook,dateFormat(now(),'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);//if there is a percentage used then format as a percent 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."); } //check to make sure that data was pulled back by the query if (qExecSummary.recordCount) { rowNum = 18; do {//if data is pulled back loop through it and add it to the correct cell SpreadSheetSetCellValue(Workbook,dateFormat(now(),'mm/dd/yyy'),rowNum,1); SpreadSheetSetCellValue(Workbook,qExecSummary.responsible[rowNum-17],rowNum,2); SpreadSheetSetCellValue(Workbook,qExecSummary.name[rowNum-17],rowNum,3); SpreadSheetSetCellValue(Workbook,qExecSummary.Description[rowNum-17],rowNum,4); SpreadSheetSetCellValue(Workbook,qExecSummary.bud_sum[rowNum-17],rowNum,5); SpreadSheetSetCellValue(Workbook,qExecSummary.committed[rowNum-17],rowNum,6); SpreadSheetSetCellValue(Workbook,qExecSummary.Spent_YTD[rowNum-17],rowNum,7); if (qExecSummary.bud_sum[rowNum-17] NEQ 0){ Spreadsheetformatcell(Workbook,formatNum,rowNum,8);//if there is a percentage used then format as a percent SpreadSheetSetCellValue(Workbook,(qExecSummary.Spent_YTD[rowNum-17]/qExecSummary.bud_sum[rowNum-17]),rowNum,8); } else { SpreadSheetSetCellValue(Workbook,0,rowNum,8); } rowNum++; } while (rowNum - 17 LT qExecSummary.recordCount); } else { SpreadSheetAddRows(Workbook,"No results for your criteria."); } //check to make sure that data was pulled back by the query if (qExecSummary.recordCount) { rowNum = 29; do {//if data is pulled back loop through it and add it to the correct cell SpreadSheetSetCellValue(Workbook,dateFormat(now(),'mm/dd/yyy'),rowNum,1); SpreadSheetSetCellValue(Workbook,qExecSummary.responsible[rowNum-28],rowNum,2); SpreadSheetSetCellValue(Workbook,qExecSummary.name[rowNum-28],rowNum,3); SpreadSheetSetCellValue(Workbook,qExecSummary.Description[rowNum-28],rowNum,4); SpreadSheetSetCellValue(Workbook,qExecSummary.bud_sum[rowNum-28],rowNum,5); SpreadSheetSetCellValue(Workbook,qExecSummary.committed[rowNum-28],rowNum,6); SpreadSheetSetCellValue(Workbook,qExecSummary.Spent_YTD[rowNum-28],rowNum,7); if (qExecSummary.bud_sum[rowNum-28] NEQ 0){ Spreadsheetformatcell(Workbook,formatNum,rowNum,8);//if there is a percentage used then format as a percent SpreadSheetSetCellValue(Workbook,(qExecSummary.Spent_YTD[rowNum-28]/qExecSummary.bud_sum[rowNum-28]),rowNum,8); } else { SpreadSheetSetCellValue(Workbook,0,rowNum,8); } rowNum++; } while (rowNum - 28 LT qExecSummary.recordCount); } else { SpreadSheetAddRows(Workbook,"No results for your criteria."); } SpreadsheetCreateSheet(Workbook,"ExecSummary334-336"); SpreadsheetSetActiveSheet(Workbook,"ExecSummary334-336"); //adding the Headers SpreadSheetSetCellValue(Workbook,"Executive Summary Report",1,1); Spreadsheetformatcell(Workbook,format2,1,1); SpreadSheetSetCellValue(Workbook,"#dateFormat(now(),'mm/dd/yyyy')#",3,1); Spreadsheetformatcell(Workbook,format1,3,1); SpreadSheetSetCellValue(Workbook,"Data Date",5,1); Spreadsheetformatcell(Workbook,format1,5,1); SpreadSheetSetColumnWidth(Workbook,1,10); SpreadSheetSetCellValue(Workbook,"Level",5,2); Spreadsheetformatcell(Workbook,format1,5,2); SpreadSheetSetColumnWidth(Workbook,2,10); SpreadSheetSetCellValue(Workbook,"Name",5,3); Spreadsheetformatcell(Workbook,format1,5,3); SpreadSheetSetColumnWidth(Workbook,3,17); SpreadSheetSetCellValue(Workbook,"Description",5,4); Spreadsheetformatcell(Workbook,format1,5,4); SpreadSheetSetColumnWidth(Workbook,4,20); SpreadSheetSetCellValue(Workbook,"Budget",5,5); Spreadsheetformatcell(Workbook,format1,5,5); SpreadSheetSetColumnWidth(Workbook,5,15); SpreadSheetSetCellValue(Workbook,"Commited",5,6); Spreadsheetformatcell(Workbook,format1,5,6); SpreadSheetSetColumnWidth(Workbook,6,15); SpreadSheetSetCellValue(Workbook,"Spent YTD",5,7); Spreadsheetformatcell(Workbook,format1,5,7); SpreadSheetSetColumnWidth(Workbook,7,15); SpreadSheetSetCellValue(Workbook,"% Spent",5,8); Spreadsheetformatcell(Workbook,format1,5,8); SpreadSheetSetColumnWidth(Workbook,8,15); //check to make sure that data was pulled back by the query if (qExecSummary.recordCount) { rowNum = 6; do {//if data is pulled back loop through it and add it to the correct cell SpreadSheetSetCellValue(Workbook,dateFormat(now(),'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);//if there is a percentage used then format as a percent 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."); } //check to make sure that data was pulled back by the query if (qExecSummary.recordCount) { rowNum = 18; do {//if data is pulled back loop through it and add it to the correct cell SpreadSheetSetCellValue(Workbook,dateFormat(now(),'mm/dd/yyy'),rowNum,1); SpreadSheetSetCellValue(Workbook,qExecSummary.responsible[rowNum-17],rowNum,2); SpreadSheetSetCellValue(Workbook,qExecSummary.name[rowNum-17],rowNum,3); SpreadSheetSetCellValue(Workbook,qExecSummary.Description[rowNum-17],rowNum,4); SpreadSheetSetCellValue(Workbook,qExecSummary.bud_sum[rowNum-17],rowNum,5); SpreadSheetSetCellValue(Workbook,qExecSummary.committed[rowNum-17],rowNum,6); SpreadSheetSetCellValue(Workbook,qExecSummary.Spent_YTD[rowNum-17],rowNum,7); if (qExecSummary.bud_sum[rowNum-17] NEQ 0){ Spreadsheetformatcell(Workbook,formatNum,rowNum,8);//if there is a percentage used then format as a percent SpreadSheetSetCellValue(Workbook,(qExecSummary.Spent_YTD[rowNum-17]/qExecSummary.bud_sum[rowNum-17]),rowNum,8); } else { SpreadSheetSetCellValue(Workbook,0,rowNum,8); } rowNum++; } while (rowNum - 17 LT qExecSummary.recordCount); } else { SpreadSheetAddRows(Workbook,"No results for your criteria."); } //check to make sure that data was pulled back by the query if (qExecSummary.recordCount) { rowNum = 29; do {//if data is pulled back loop through it and add it to the correct cell SpreadSheetSetCellValue(Workbook,dateFormat(now(),'mm/dd/yyy'),rowNum,1); SpreadSheetSetCellValue(Workbook,qExecSummary.responsible[rowNum-28],rowNum,2); SpreadSheetSetCellValue(Workbook,qExecSummary.name[rowNum-28],rowNum,3); SpreadSheetSetCellValue(Workbook,qExecSummary.Description[rowNum-28],rowNum,4); SpreadSheetSetCellValue(Workbook,qExecSummary.bud_sum[rowNum-28],rowNum,5); SpreadSheetSetCellValue(Workbook,qExecSummary.committed[rowNum-28],rowNum,6); SpreadSheetSetCellValue(Workbook,qExecSummary.Spent_YTD[rowNum-28],rowNum,7); if (qExecSummary.bud_sum[rowNum-28] NEQ 0){ Spreadsheetformatcell(Workbook,formatNum,rowNum,8);//if there is a percentage used then format as a percent SpreadSheetSetCellValue(Workbook,(qExecSummary.Spent_YTD[rowNum-28]/qExecSummary.bud_sum[rowNum-28]),rowNum,8); } else { SpreadSheetSetCellValue(Workbook,0,rowNum,8); } rowNum++; } while (rowNum - 28 LT qExecSummary.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"> 

我知道这真的很长,但是这就是我必须这样做的原因,因为我必须使用数据库,我不得不使用一些查询来获得我想要的数据。

我已经尝试在<cfscript>里面使用<cfscript>来格式化列,但是这也不起作用。 现在格式只能在第一张纸上工作,除了在任何纸张上不起作用的宽度。

编辑

我几乎有这一切工作。 现在只是格式。 它适用于2.5张。 在第三张纸上,半途停止工作。 有8列,最后4列没有格式化。 我已经尝试了所有我能想到的事情。 我已经在这里添加了一个例子,我知道它很长,但是我不能在任何地方重现这个问题。 我只在生产中得到它。 我已经复制了我的计算机和上面链接的示例。 无论是在我的本地计算机上,在这个例子中,它工作正常。 但我有CF 2016和prod服务器是CF 9。

我知道这是很多代码,但是如果有人能帮上忙,那就太棒了。 我把头靠在墙上试图看看我把它搞乱了,但是我把每张纸都复制在它之前,然后改变了查询号码,所以它应该工作。

我也可以更新这里发布的例子,但就像我说的我使用的例子很长。

最终编辑

这里是完整的代码作为例子。 它生成3张,第二张纸运行查询3次填写页面。 每张纸都有相同的标题和格式。

SpreadSheetSetColumnWidth只在活动工作表上运行。 所以最后调用一次就不行了。 必须在每张纸上调用该函数,对于每个希望修改的列。

正如你注意到的那样,列宽只能在向该列添加一些数据才能更改。 原因是列(或单元格)实际上并没有创build,直到您应用一个值或公式。 所以如果你尝试修改它们的属性,在它们存在之前什么都不会发生。 相同的规则适用于“格式”:单元格必须存在才能应用格式。

优化:

一些技巧将大大简化原始代码并提高可读性:

  1. 由于报表将使用相同的查询列,所以对于UDF来说,这是一个完美的工作。 而不是复制每个工作表相同的代码,只需创build一个函数,使用提供的查询来填充任意工作表名称。

     function populateSummarySheet( any workbook , string sheetName , date reportDate , query qryData , boolean createNewSheet ) { 

    然后根据需要多次调用函数:

      Workbook = SpreadsheetNew("FirstSheet"); populateSummarySheet(Workbook, "FirstSheet", reportDate, query1, false); populateSummarySheet(Workbook, "SecondSheet", reportDate, query2, true); populateSummarySheet(Workbook, "ThirdSheet", reportDate, query3, true); // .... 

    如果您对CF中的函数不熟悉,请务必阅读如何正确确定函数局部variables的范围 。 一个常见的问题是忘记把所有的函数局部variables放在一起,这往往会造成奇怪和难以重现的问题。

  2. CF9 +支持结构和数组创build的快捷键。 即

     headerFormat = { bold="true", fontsize=18, font="Calibri" }; 
  3. 如果您需要格式化特定行或列中的所有单元格,则格式化行或列的效率会更高,而不是每个单独的单元格。 请参阅以下文档: SpreadSheetFormatRow , SpreadSheetFormatColumn SpreadSheetFormatColumns

    另外,Excel会限制您可以应用的样式。 格式化单个单元格会使用更多的样式,从而增加您将超出限制的可能性: SpreadsheetFormatRow突然停止工作较新的.xlsx格式的限制高于.xls格式。 因此,如果可能,最好使用.xlsx工作簿,而不是.xls。

我有CF 2016和prod服务器是CF 9

就像我在另一个线程中提到的,在Dev和Prod中使用不同的版本是一个非常糟糕的主意。 因为它会使你无法testing你的代码。 如果您进行search,仍然可以find旧版本的下载。 例如: 直接下载链接为ColdFusion 9安装程序(64位Windows)