2012-11-19 32 views
0

從查詢創建電子表格時,生成的電子表格不允許格式化某個日期時間數據類型的列。Coldfusion生成的電子表格不允許列格式化

電子表格創作是:

<cfset filenametouse = 'AllCaseData' /> 
    <!---Set directory and full file path---> 
    <cfset theDir = GetDirectoryFromPath(GetCurrentTemplatePath()) /> 
    <!---Attach file extension to full file path and file name---> 
    <cfset theFile = theDir & filenametouse & ".xls" /> 
    <cflock name="fileActionSentItems" type="exclusive" timeout="30" throwontimeout="true"> 
     <cfset SpreadsheetObj = spreadsheetNew()> 
     <!---<cfset fcol = {}> 
     <cfset fcol.dataformat = "@">---> 
     <!---Create the column headings---> 
     <cfset SpreadsheetAddRow(SpreadsheetObj, "Case_Number,Ship_Start,Projected_$,VA_$,Customer_ID,Customer,Project_Description,Stage,Sales,PM,J_Count,O_Count,S2_Count,S_Count,I_Count")> 
     <!---add the column data to the spreadsheet---> 
     <cfset SpreadsheetAddRows(SpreadsheetObj, caseData) /> 

     <cfset SpreadsheetFormatColumn(SpreadsheetObj, {dataformat="m/d/yyyy"}, 2)> 


     <!---Generate the spreadsheet---> 
     <cfspreadsheet action="write" filename="#theFile#" name="SpreadsheetObj" sheetname="Sheet1" overwrite="true" /> 

    </cflock> 
    <cftry> 
     <cflock name="fileActionSentItems" type="exclusive" timeout="30" throwontimeout="true"> 
      <cfmail to="#session.authUser.getEmail()#" from="[email protected]" subject="All Case Data" type="html" > 
       <cfmailparam file="#theFile#" remove="true" /> 
       <p>The requested data is attached.</p> 
      </cfmail> 
     </cflock> 
     <cfcatch type="any" > 
      <cffile action="delete" file="#theFile#" /> 
     </cfcatch> 
    </cftry> 
    <cfoutput><h3 style="margin-top: 1em;">Email sent to <cfoutput>#session.authUser.getEmail()#</cfoutput></h3></cfoutput> 

所有的數據是正確的,並在正確列,不過「Ship_Start」欄自帶的格式相似,2012年11月27日00:00:00.000,並且無法在Excel中更改爲任何其他格式。

如果我在SQL Server Management Studio中手動運行查詢,然後將結果與標題複製到Excel,數據可以格式化。我還創建了一個Excel模板,其中的列標題格式化並使用數據填充模板,但即使使用「日期」格式化該列也無法格式化。

我可能會錯過什麼?

+1

您運行的是哪種ColdFusion版本?你能格式化電子表格中的任何列嗎?您是否嘗試過使用[SpreadsheetFormatColumn](http://help.adobe.com/zh_CN/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-6806.html)格式化列? –

+0

這個應用程序駐留在CF9盒子上。我剛剛完成了SpreadsheetFormatColumn的測試,它會影響除「Ship_Start」之外的所有列。我注意到在Excel文件中,看起來像列中的數據後面有一個空格:「2012-11-27 00:00:00.0 」 – aparker81

+0

我的問題已經添加了SpreadsheetFormatColumn更新。完整的CF版本是9,0,1,274733。 – aparker81

回答

1

這似乎是一個錯誤。有關這方面的一些很好的討論 Raymond Camden的博客 - HERE。討論在底部的用戶評論中。看起來Ray真的進入了這個錯誤。我想我找到了它 - HERE

我發現了兩條可能對您有幫助的評論。他們是解決方法,但也許會讓你感動。我有BOLDED我認爲適用的評論部分,但您可能想閱讀Ray的博客上的所有評論,以便將它們置於上下文中。


*** BEGIN引述博客文章 ***

首先評論發表萊斯Konley表示,二零一一年六月二十零日在上午11時12分

雷&詹姆斯,我有關於CFSPREADSHEET和Excel無法識別結果日期字段的其他信息。首先,我們的代碼既使用查詢對象,也設置顯式日期格式[即cfset temp = QuerySetCell(GetExcelReport,「FollowUpDate」,「DateFormat(GetResult.FollowUpDate,」MM/DD/YYYY「))]] 。當我們在Excel中打開電子表格(使用CFSPREADSHEET構建)時,日期列被格式化爲普通文本或常規(左對齊)。在此網站上(http://www.appnitro.com/forums/topic/excel-不要...)提供瞭解決方法,因此業務用戶可以突出顯示該列(在Excel中),然後選擇數據>文本到列,然後單擊向導上的完成(無需通過它)...和日期字段現在被Excel識別爲DATE。我們可能需要恢復在生產服務器上安裝MS Office,並且如果業務用戶決定不再需要這個添加的次要文件,則繼續使用舊的CFX_Excel標記不便之處,至少在未來的版本中糾正特定的錯誤用於向Adobe提交有關此問題的錯誤報告。通過丹墨菲說發表於2011年7月22日在上午10時44

詹姆斯,雷和Les



第二個評論 - 我知道我有點太遲了在這裏,但我想我只是找到了Excel中日期格式的解決方法,這對我來說一直很好。 首先,我使用基於另一個查詢的querynew創建我自己的查詢,並使用它來導出到Excel。如果我將querynew中的數據類型設置爲cf_sql_varchar而不是date或cf_sql_timestamp,然後將查詢輸出到Excel電子表格,它將採用您提供的任何格式。如果您在那裏停留,Excel仍然會將數據視爲文本,而不是實際日期(儘管格式正確)。其次,只需使用spreadsheetFormatColumns設置數據格式以匹配您在查詢中指定的格式,並且Excel現在將其全部識別爲日期。 Ray,我正在使用CFLib中的DateTimeFormat函數,所以下面就是我爲什麼這麼做的。 (這顯然只是其中的一部分,但你會發現這個想法)。

這裏的查詢......

<cfset mys = QueryNew("OrderNumber,InScan,OutScan","cf_sql_varchar,cf_sql_varchar,cf_sql_varchar")> 
<cfloop query="qryThroughputScans"> 
<cfset queryAddRow(mys)> 
<cfset querySetCell(mys,"OrderNumber",NumberFormat(qryThroughputScans.OrderNumber,000000))> 
<cfset querySetCell(mys,"InScan",DateTimeFormat(qryThroughputScans.InScan))> 
<cfset querySetCell(mys,"OutScan",DateTimeFormat(qryThroughputScans.OutScan))> 
</cfloop> 

然後,我創建Excel電子表格後使用列格式...

<cfset spreadsheetFormatColumns(s, 
{ 
alignment="center", 
dataformat="m/d/yyyy h:mm AM/PM" 
}, 
"2-3")> 

我古靈精怪的格式,測試了這一點只是爲了確保它能夠工作,並且我取得了巨大的成功。也許這已經是衆所周知的,但是當我正在尋找修復程序時,我找不到任何東西。希望當他們去尋找日期問題的答案時,這可以幫助別人解決問題。

感謝, 丹


*** END引述博客文章不幸的是,格式支持缺乏 ***

+0

是的,看起來你是對的。整天都在逼我瘋了。我會建議用戶使用文本到列的方法,因爲他需要自己格式化數據。感謝您的幫助! – aparker81

1

。如果您正在運行Windows,您可以簡單地使用循環來執行此操作。SpreadSheetSetCellValue

<cfset obj = spreadsheetNew()> 
<cfset SpreadSheetAddRow(obj, "Case_Number,Ship_Start")> 
<cfset SpreadSheetAddRows(obj, yourQuery)> 
<cfset spreadsheetFormatColumns(obj, {dataformat="m/d/yyyy"}, 2)> 
<cfloop query="yourQuery"> 
    <cfset theValue = yourQuery.Ship_Start[currentRow]> 
    <cfif IsDate(theValue)> 
     <!--- offset by 1 row for header ---> 
     <cfset SpreadSheetSetCellValue(obj, theValue.toDouble(), currentRow+1, 2)> 
    </cfif> 
</cfloop> 

<cfset SpreadSheetWRite(obj, theFilePath, true)>