Question

When creating a spreadsheet from a query, the spreadsheet that is produced will not allow a column of a datetime datatype to be formatted.

The spreadsheet creation is:

    <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="noreply@pratt.com" 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>

All the data is correct and in the correct columns, however the "Ship_Start" column comes formatted similar to 2012-11-27 00:00:00.000 and cannot be changed to any other format in Excel.

If I run the query manually in SQL Server Management Studio then copy the results with headers into Excel, the data can be formatted. I have also created an Excel template with the column headers formatted and populated the template with the data, but the column cannot be formatted even though it is formatted with "date."

What could I be missing?

Was it helpful?

Solution

This appears to be a bug. There is some pretty good discussion about this on Raymond Camden's blog - HERE. The discussion is in the user comments at the bottom. It looks like Ray actually entered the bug for this. I think I found it - HERE.

I found two of the comments that might help you. They are workarounds but maybe one will get you moving. I have BOLDED the parts of the comments that I think apply but you might want to read through all of those comments on Ray's blog anyway to get them in context.


*** BEGIN QUOTED BLOG ENTRIES ***

First comment posted by Les Konley Said On 06-20-2011 At 11:12 AM

Ray & James, I have additional information about the CFSPREADSHEET and Excel not recognizing resulting date fields. First of all, our code is both using a query object as well as setting explicit date formats [i.e, cfset temp = QuerySetCell(GetExcelReport, "FollowUpDate", "DateFormat(GetResult.FollowUpDate, "MM/DD/YYYY")) ]. When we open the spreadsheet in Excel (built using CFSPREADSHEET), the date column is formatted as regular text or general (left-justified). On this site (http://www.appnitro.com/forums/topic/excel-does-no...) a work-around is offered, so the business users can highlight the column (within Excel), then select Data > Text to Columns, then click FINISH on the Wizard (without needing to step through it)... and date fields are now recognized by Excel as a DATE. We may need to revert back to installing MS Office on the production servers and continue using the old CFX_Excel tag if the business users decide they don't want this added minor inconvenience, at least until that particular bug is corrected on a future release. Thanks for submitting a bug report to Adobe about this issue.



Second comment posted by Dan Murphy Said On 07-22-2011 At 10:44 AM

James, Ray, and Les - I know I'm a bit late to the game here, but I think I just found a workaround for the date formats in Excel that has been working out well for me. First, I'm creating my own query with querynew based on another query and using that to export to Excel. If I make the datatype in the querynew to cf_sql_varchar instead of date or cf_sql_timestamp and then output the query to the Excel spreadsheet, it will take whatever format you give it. If you stop there, Excel will still see the data as just text, not an actual date (although formatted correctly). Second, just set the dataformat using spreadsheetFormatColumns to match the format you specified in the query, and Excel now recognizes it all as a date. Ray, I'm using your DateTimeFormat function from CFLib, so here is how it all worked out for me. (this is just part of it all obviously, but you'll catch the idea).

Here's the query...

<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>

And then the column format that I use after creating the Excel spreadsheet...

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

I tested this out with all sorts of weird formats just to make sure it works, and I had great success with it. Maybe this is already well known but I couldn't find anything when I was looking for a fix. Hopefully this helps someone out when they go looking for an answer to the date issue.

Thanks, Dan


*** END QUOTED BLOG ENTRIES ***

OTHER TIPS

Unfortunately the formatting support is lacking. If you are running windows, you could simply do it yourself with a loop and 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)>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top