如何使用SpreadsheetFormatColumns正确设置日期格式

时间:2016-03-27 20:36:38

标签: coldfusion coldfusion-10

服务器:CF10 平台:Windows和Linux(在PROD的DEV窗口中获胜)

因此,我为客户端生成了一个excel文件,但无法让日期字段正常运行。其他所有东西似乎都运行得很好但是当我将数据发送到excel生成时,excel将其视为文本。因此,当字段被排序时,它就会被处理。

更多信息:我尝试配置的列称为到达日期,它的格式为mm/dd/yyyy。 (我尝试将格式设为m/d/yy,但是当它到达不起作用的表格时。)

代码:

<cfset filename = expandPath("./TDYData_(#DateFormat(now(),'mmddyy')#).xls")>
<!--- Make a spreadsheet object --->
<cfset s = spreadsheetNew("TDYData")>
<!--- Add header row --->
<cfset spreadsheetAddRow(s, "TDY Phase,Full Name,Employment Category,Gender,Originating Agency,Agency Comments,Originating Office,Office Comments,Originating Country,TDY Request Received,Mission Office Supported,Type of TDY Support,eCC Submission,eCC Approval,eCC Point of Contact,Date of Departure from Originating Country,Arrival Date,Departure Date,Accomodation Type,Accomodation Comments,Assigned Desk,Local Mobile Number,TDY Comments")>

<!--- format header --->
<cfset spreadsheetFormatRow(s, {bold=true,fgcolor="lemon_chiffon",fontsize=10}, 1)>
<!--- Add query --->
<cfset spreadsheetAddRows(s, myExcel)>
<cfset SpreadSheetAddFreezePane(s,0,1)>

<cfset SpreadsheetFormatColumn(s, {dataformat="m/d/yy"}, 17) />
<cfset SpreadsheetFormatColumn(s, {alignment="right"}, 16) />


<cfheader name="content-disposition" value="attachment; filename=TDY_Data_(#DateFormat(now(),'mmddyy')#).xls">
<cfcontent type="application/msexcel" variable="#spreadsheetReadBinary(s)#" reset="true">

有什么想法吗?

由于

1 个答案:

答案 0 :(得分:0)

所以我有一个可行的解决方案:

<cfset therow = 0>
<cfoutput query="myExcel" startrow="1">
  <cfset therow = myExcel.currentrow + 1>
  <cfif len(eCCSubDate) GT 0>
  <cfset SpreadsheetSetCellFormula(s,"DATEVALUE(#Chr(34)##eCCSubDate##Chr(34)#)",therow,13)>
  </cfif>
  <cfif len(eCCApproved) GT 0>
  <cfset SpreadsheetSetCellFormula(s,"DATEVALUE(#Chr(34)##eCCApproved##Chr(34)#)",therow,14)>
  </cfif>
  <cfif len(DateDepartCntry) GT 0>
  <cfset SpreadsheetSetCellFormula(s,"DATEVALUE(#Chr(34)##DateDepartCntry##Chr(34)#)",therow,16)>
  </cfif>
  <cfif len(DateArrive) GT 0>
  <cfset SpreadsheetSetCellFormula(s,"DATEVALUE(#Chr(34)##DateArrive##Chr(34)#)",therow,17)>
  </cfif>
  <cfif len(DateDepart) GT 0>
  <cfset SpreadsheetSetCellFormula(s,"DATEVALUE(#Chr(34)##DateDepart##Chr(34)#)",therow,18)>
  </cfif>         


</cfoutput>

所以我为一个计数器设置一个变量然后循环回查询并检查以确保它不是一个空单元格然后将公式应用于chr(34)是引号然后是数据值的单元格。通过使用它,(在写入数据之后)它至少表现得像真实的日期字段。