Skip navigation
Currently Being Moderated

CFQuery -> Excel file

Sep 18, 2012 6:19 AM

Hello, everyone.

 

I'm trying to take a query object and insert the data into an Excel file.  I've tried the CFCONTENT/CFHEADER route, and I get the error message about the file not being the same type as the extension.

 

Is there a better way to get data into an Excel file?

 

Thank you,

 

^_^

 
Replies
  • Currently Being Moderated
    Sep 18, 2012 6:52 AM   in reply to WolfShade

    start with cfspreadsheet.

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 18, 2012 8:36 AM   in reply to WolfShade

    One step at a time, starting with why cfspreadsheet is producing a blank file.  Please post the code you are using to create this file.  Just the code that creates the file, no queries, formatting stuff or anything else.

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 18, 2012 11:29 AM   in reply to WolfShade

    That's odd.  When I run this:

    <cfquery name="x" datasource="dw">
    select event_code, event_name
    from event

     

    </cfquery>

    <cfspreadsheet action="write" query="x" filename="abc.xls" overwrite="yes">

     

    I get a spreadsheet with column headers and 20 rows of data.

    If I add "where 1=3" to the query, I get a spreadsheet with column headers only.

     

    If I change the query to "select count(*) from event", I get nothing in cell A1 and 20 in cell A2.

     

    I suggest simplifying your query and filename.  Your sql could be "select count(*) records from sometable where 1=3"

    This should give you "records" in cell A1 and "0" in cell A2.  Then start building it up until it goes strange.

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 19, 2012 6:46 AM   in reply to WolfShade

    This block of code creates an excel file from a query and then offers it to the user.

     

    <cfset theFile = "d:\dw\dwtest\dan\abc.xls">

     

    <cfquery name="x" datasource="dw">
    select event_code, event_name
    from event
    </cfquery>

     

    <cfspreadsheet action="write" query="x" filename="#thefile#" overwrite="yes">
    <cfheader name="content-disposition" value="Attachment;filename=#thefile#">

     

    <cfcontent  file="#thefile#" type="application/vnd.ms-excel">

     

    What are you doing differently?

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 19, 2012 11:44 AM   in reply to WolfShade

    With regards to, "<cfcontent file="filename" type="application/msexcel"> (as opposed to "application/vnd.ms-excel").", when I changed my type I match yours, there was no change in behaviour.

     

    Regarding, "

    Before changing the type of the cfcontent, it would say "The file you are opening 'filename.xls' is not the same type as the file extension."

     

    After changing the type of the cfcontent, it now says "The file you are opening 'export.cfm' is not the same type as the file extension."",

     

    That sounds like what we were seeing when we were using html table tags to generate our content.  Things worked well until Office 2007 came out and then it got ugly.  I suspect that when you say that your code is almost exactly the same as mine, you have a different definition of "almost" than do I.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points