Solved

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

Posted on 2004-03-31
6
1,274 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Article by: kevp75
Hey folks, 'bout time for me to come around with a little tip. Thanks to IIS 7.5 Extensions and Microsoft (well... really Windows 8, and IIS 8 I guess...), we can now prime our Application Pools, when IIS starts. Now, though it would be nice t…
Introduction This article explores the design of a cache system that can improve the performance of a web site or web application.  The assumption is that the web site has many more “read” operations than “write” operations (this is commonly the ca…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now