我正在使用CF9。我创建了一个多表工作簿。我正在尝试格式化每个工作表的列。格式化仅在第一张纸上进行。如何才能将其应用于所有床单?另外,我无法弄清楚如何让列宽适用于任何工作表。
这是我目前获得的代码:
<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">
我知道它真的很长,但这就是我必须这样做的原因,因为我必须使用的数据库,我不得不使用几个查询来获取我想要的数据。
我尝试在SpreadSheetSetColumnWidth
中使用<cfscript>
来格式化列,但这也不起作用。现在,格式化仅适用于第一张纸,但宽度不适用于任何纸张。
修改
我几乎把它全部都用了。现在只是格式化。它适用于2.5张。在第3张纸上,它停止工作一半。有8列,最后4列没有格式化。我已经尝试了一切我能想到的东西。我已将其添加为示例here,我知道它很长,但我不能在任何地方重现这个问题。我只是在生产中得到它。我已将我所得到的内容复制到我的计算机和上面链接的那个例子中。在我的本地计算机和示例中都可以正常工作。但我有CF 2016,prod服务器是CF 9.
我知道要查看的代码很多,但如果有人可以提供帮助,那就太好了。我正在撞墙试图看到我把它搞砸了,但是对于每张纸,我在它之前复制了纸张然后更改了查询号,所以它应该正常工作。
我也可以更新此处发布的示例,但就像我说的那样,我使用的示例很长。
最终编辑
Here是完成的代码作为示例。它生成3张纸,后两张纸运行查询3次以填写页面。每张纸都有相同的标题和格式。
答案 0 :(得分:1)
SpreadSheetSetColumnWidth仅适用于有效表。因此,在最后调用它一次是行不通的。必须在每个表上调用该函数,对于要修改的每个列。
正如您所注意到的,只有在向该列添加一些数据后才能更改列宽。这样做的原因是,在应用值或公式之前,实际上不会创建列(或单元格)。因此,如果您尝试修改其属性,在它们存在之前,就不会发生任何事情。相同的规则适用于“格式”:在应用格式之前,单元格必须存在。
<强>优化:强>
一些提示将大大简化原始代码并提高可读性:
由于报表将在每个工作表上使用相同的查询列,因此这对于UDF来说是一个完美的工作。不要为每个工作表复制相同的代码,只需创建一个使用提供的查询填充任意工作表名称的函数。
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中的函数,请务必阅读如何正确定位函数局部变量。常见的问题是忘记了所有函数局部变量的范围,这通常会产生奇怪且难以重现的问题。
CF9 +支持结构和数组创建的快捷方式。即
headerFormat = { bold="true", fontsize=18, font="Calibri" };
如果需要格式化特定行或列中的所有单元格,则更有效地格式化行或列,而不是每个单独的单元格。请参阅以下文档:SpreadSheetFormatRow,SpreadSheetFormatColumn SpreadSheetFormatColumns
此外,Excel会限制您可以应用的样式数量。格式化单个单元格会占用更多样式,从而增加超出限制的可能性:SpreadsheetFormatRow abruptly stops working较新的.xlsx格式的限制高于.xls格式。因此,如果可能,最好使用.xlsx工作簿,而不是.xls。
我有CF 2016,prod服务器是CF 9
就像我在另一个帖子中提到的,在Dev和Prod中使用不同的版本是真的坏主意。因为它将无法测试您的代码。如果您进行搜索,仍可以找到旧版本的下载。例如:Direct download link for ColdFusion 9 Installer (64-bit Windows)