Solved

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

Posted on 2004-03-31
6
1,331 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

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…
If you don't have the right permissions set for your WordPress location in IIS, you won't be able to perform automatic updates. Here's how to fix the problem.
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…

724 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