我在 Excel 报告中使用电子表格函数,但在导出到 Excel 时它会将日期转换为文本。因此,我在 SpreadSheetSetCellValue 中使用了数据类型“日期” ,因此第一列中的值可以转换为客户想要的任何日期格式。这是代码:
<cfset SpreadsheetSetCellValue(objSheet, '#RecordDate#', iRow, 1, 'Date')>
这工作正常,但是当我稍后将背景颜色应用于交替行时:
<cfset stFormat.AlternateRow = StructNew()>
<cfset stFormat.AlternateRow.fgcolor = 'yellow'>
<cfif (iRow mod 2) IS 0>
<cfset SpreadsheetFormatRow(objSheet, stFormat.AlternateRow, iRow)>
</cfif>
它将该行中的所有列转换为“日期”格式。而我只希望每行中的第一列是“日期”日期类型。
最佳答案
对我来说听起来像是一个错误。我会整理一个重现案例并提交一个错误 bugbase.adobe.com .
虽然并不理想,但一种可能的解决方法是在格式化行之后设置日期列值。不幸的是,设置单元格值也会消除背景颜色,因此您还需要重新应用它。使用 CF11、YMMV 进行测试。
注意:解决方法根本不会改变列的顺序,只会在格式化列时。所以“日期”仍然在第一列中结束。
<cfscript>
objSheet = SpreadSheetNew("Sheet1", true);
for(iRow = 1; iRow <= 100; iRow++) {
// populate everything EXCEPT the date column
SpreadsheetSetCellValue(objSheet, "B", iRow, 2);
SpreadsheetSetCellValue(objSheet, "C", iRow, 3);
SpreadsheetSetCellValue(objSheet, "D", iRow, 4);
SpreadsheetSetCellValue(objSheet, "1234", iRow, 5);
isAlternateRow = (iRow MOD 2) EQ 0;
rowColor = isAlternateRow ? 'yellow' : 'white';
// format whole row
if (isAlternateRow) {
SpreadsheetFormatRow(objSheet, { fgcolor = rowColor }, iRow);
}
// finally apply date and reformat that cell
SpreadsheetSetCellValue(objSheet, now(), iRow, 1, 'date');
SpreadSheetFormatCell(objSheet, { dataformat = 'mm-dd-yyyy', fgcolor = rowColor}, iRow, 1);
}
</cfscript>
结果:
关于excel - 电子表格函数将日期转换为文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50335541/