Solved

Export CFDirectory result to Excel

Posted on 2010-08-25
4
448 Views
Last Modified: 2013-12-16
I want to obtain the files in a directory and export the result to excel.  I have the following code but all it does is display the results on the website.  An excel file containing the files in the dir is not created

Any ideas?
<HTML>
  <head>
    <title>>> </title>
  </head>
  <body>
    <cfcontent type="application/msexcel">
    <CFDIRECTORY action="LIST" directory="D:\Image" name="myimage">
    <cfheader name="Content-Disposition" value="filename=image.xls">
    <table>
      <tr>
	<th>my images</th>
      </tr>
      <cfoutput query="myimage">
      <TR>
        <cfif #name# neq 'index.cfm'>
          <TD>#name#</TD>
        </cfif>
      </TR>
      </cfoutput>
    </table>		
  </body>
</HTML>

Open in new window

0
Comment
Question by:CipherIS
  • 2
4 Comments
 
LVL 4

Expert Comment

by:JohnHowlett
ID: 33525594
Try this (cfcontent at the top):

 <cfheader name="Content-Disposition" value="filename=image.xls">

    <cfcontent type="application/msexcel">
    <CFDIRECTORY action="LIST" directory="D:\Image" name="myimage">
    <table>
      <tr>
      <th>my images</th>
      </tr>
      <cfoutput query="myimage">
      <TR>
        <cfif name neq 'index.cfm'>
          <TD>#name#</TD>
        </cfif>
      </TR>
      </cfoutput>
    </table>



When you go to this page, you should be asked to open it in Excel.
0
 
LVL 52

Expert Comment

by:_agx_
ID: 33526113
(no points please ...)

I suspect the cause is how the cfheader/cfcontent tags are ordered.  I believe you must list the header _first_, then cfcontent.  

     <cfheader name="Content-Disposition" value="filename=image.xls">
     <cfcontent type="application/msexcel">

>>   <cfcontent type="application/msexcel">
>>    <CFDIRECTORY action="LIST" directory="D:\Image" name="myimage">
>>    <cfheader name="Content-Disposition" value="filename=image.xls">

I noticed you've got the cfdirectory and header code mixed together.  MS Excel can sometimes be picky about how it parses html. So best to keep all display code (ie html) separate from everything else. I'd suggest running the cfdirectory first

     <!--- do any NON-display code first....--->
     <CFDIRECTORY action="LIST" directory="D:\Image" name="myimage">

 ... then generate the "excel" output in one shot.

     <!--- header code ....--->
     <cfheader name="Content-Disposition" value="filename=image.xls">
     <cfcontent type="application/vnd.ms-excel">
     <!--- file content ....--->
     <table>
      <tr>
      .... etc.....
      </TR>
    </table>            

0
 
LVL 1

Author Comment

by:CipherIS
ID: 33531320
Did not work.
0
 
LVL 52

Accepted Solution

by:
_agx_ earned 500 total points
ID: 33533759
>> <cfheader name="Content-Disposition" value="filename=image.xls">

The Content-Disposition value is missing the type. ie inline (display in browser) or attachment (prompt to download).  Also, if you have debugging enabled it can interfere with Excel.  So suppress it if needed.

The attached works correctly (condensed for brevity)

<!--- suppress any debugging output. it can interfere with excel --->
<cfsetting showdebugoutput="false">
      <CFDIRECTORY action="LIST" directory="D:\Image" name="myimage">
      <cfheader name="Content-Disposition" value="inline; filename=image.xls">
      <cfcontent type="application/vnd.msexcel">
      <table>
      <tr><th>my images</th></tr>
      <cfoutput query="myimage">
            <TR><cfif name neq 'index.cfm'><TD>#name#</TD></cfif></TR>
      </cfoutput>
      </table>
</cfsetting>
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hi, Even though I have created this Tutorial on My personal Blog, Some people might not able to find my website, So here i am posting it again Today, from the topic it is very clear that i will be showing you here the very basic usage of how we …
Recently while working on a project I got a very annoying cfdocument has no body error message. I had never seen this error before. So I checked the code. The code was pretty simple; it was Just showing me the cfdocumnt tag and inside that tag a …
The purpose of this video is to demonstrate how to Test the speed of a WordPress Website. Site Speed is an important metric of a site’s health. Slow site speed can result in viewers leaving your site quickly and not seeing your content. This…
The purpose of this video is to demonstrate how to update a WordPress Site’s version. WordPress releases new versions of its software frequently and it is important to update frequently in order to keep your site secure, and to get new WordPress…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question