[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2004-03-31
6
Medium Priority
?
1,352 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 500 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 9

Assisted Solution

by:shooksm
shooksm earned 500 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Lease-to-own eliminates the expenditure of hardware replacement and allows you to pay off the server over time. Usually, this is much cheaper than leasing servers. Think of lease-to-own as credit without interest.
Integration Management Part 2
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

834 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