Solved

how to create a excel/csv file on the fly?

Posted on 2004-03-31
6
1,297 Views
Last Modified: 2013-12-24
Hello, I did my search around here and find Shooksm's code.

<cfheader name="content-disposition" value="attachment;filename=#Left(CreateUUID(), 8)#.html">
<cfcontent type="application/octet-stream">
<html>
     <head>
     <title>Your Custom Query</title>
</head>
<body>
<cfoutput>
<table border="1" width="100%"><cfloop index="i" from="0" to="95" step="5">
     <tr>
          <td>#i+1#</td>
          <td>#i+2#</td>
          <td>#i+3#</td>
          <td>#i+4#</td>
          <td>#i+5#</td>
     </tr></cfloop>
</table>
</cfoutput>
</body>
</html>


But after I copied and pasted into a blank cfm file, when I call it from browser. it returns the table in the browser though I was expecting a download box pop up?

I am using CF4.5 by the way, could that be the reason?

Also it seems the <cfsavecontent> is introduced in 5.0. What should I do to get the equvilent?

I am trying to develop a page to show a table report and if clients want, they can click a button/link to download it into excel. I hope there is a way I don't need to write the report twice.

Thanks,
lyrix
0
Comment
Question by:lyrix1999
  • 3
  • 2
6 Comments
 
LVL 35

Accepted Solution

by:
mrichmon earned 125 total points
ID: 10727314
Try this :

<cfcontent type="application/msexcel">

instead of octet-stream
0
 

Author Comment

by:lyrix1999
ID: 10727432
Great! It works!

Do you happen to know the answer to the other question? How do I use something else in 4.5 for <cfsavecontent> so I don't need to write twice or the server won't need to run twice for the online report and downloadable report?

Thanks,
lyrix
0
 
LVL 35

Expert Comment

by:mrichmon
ID: 10727549
Unfortunately I am not familiar with CF 4.5 so I am not sure what would work in place of <cfsavecontent>
0
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
LVL 9

Assisted Solution

by:shooksm
shooksm earned 125 total points
ID: 10728027
On 4.5, I would suggest you break out the portion of your code into three files.  The first file is the one that builds the table.  The second file is used to display the report to the browser and the third is the file used to download to excel.

ReportTable.cfm would contain the following:

<cfoutput>
<table border="1" width="100%"><cfloop index="i" from="0" to="95" step="5">
     <tr>
          <td>#i+1#</td>
          <td>#i+2#</td>
          <td>#i+3#</td>
          <td>#i+4#</td>
          <td>#i+5#</td>
     </tr></cfloop>
</table>
</cfoutput>

ShowReport.cfm would look like this:

<html>
     <head>
          <title>Your Custom Query</title>
     </head>
<body>
<h3>You custom sites template stuff starts here</h3>
<cfinclude template="ReportTable.cfm">
<p><a href="DownloadReport.cfm">Download to excel</a></p>
<h4>custom site template ends here</h3>
</body>
</html>

Then in your DownloadReport:

<cfheader name="content-disposition" value="attachment;filename=#Left(CreateUUID(), 8)#.xls">
<cfcontent type="application/msexcel">
<html>
     <head>
          <title>Your Custom Query</title>
     </head>
<body>
<cfinclude template="ReportTable.cfm">
</body>
</html>

This will allow your to write your report once but show is as html or download it as excel.  Hope this helps
0
 

Author Comment

by:lyrix1999
ID: 10728113
Hi, Shooksm, thanks for your kind help. I don't know how to split the points otherwise I must give  you some. Even though I just figure out a way around it, your method is also cool.
0
 
LVL 35

Expert Comment

by:mrichmon
ID: 10728143
I will request that a moderator split the points between us.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

A web service (http://en.wikipedia.org/wiki/Web_service) is a software related technology that facilitates machine-to-machine interaction over a network. This article helps beginners in creating and consuming a web service using the ColdFusion Ma…
One of the typical problems I have experienced is when you have to move a web server from one hosting site to another. You normally prepare all on the new host, transfer the site, change DNS and cross your fingers hoping all will be ok on new server…

831 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