Skip to main content
Known Participant
April 17, 2013
Question

cfsavecontent

  • April 17, 2013
  • 3 replies
  • 2886 views

Hi,,

I am query from my table and want to save this query results into excel file in to specific location.

1.  Is there the way to overrite the file each time the page is run?

2. when open the myReport.xls, i got message: The file you are trying to open "myReport.xls" is in diffrent format than specific the file extension.

3. I then change myReport.xls to myReport.xlsx then run the page to get new file and sitll got diffrent error when open it : This file can't be previewed because of error in MS Excel previewever.

4. If i change myReport.xls to myReport.csv then create the file again, and re-open and it show no mesage but it shows all HTML attribute (td, tr, table).  How can i save the resust into csv file?

What can i do not having either one of message ablove when i open the file? i am using MX excel 2010 version. Woul you pls advice this?

Thanks


<cfsavecontent variable="saveContent1">

<cfcontent type="application/msexcel">

<cfheader name="Content-Disposition" value="filename=report/myReport.xls">

    <table cols="4" border="1">

        <tr>

            <td>sale</td>

            <td>delivery</td>

        </tr>

        <cfloop query="q_sale">

            <tr>

                <td>#sale#</td>

                <td>#del#</td>

            </tr>

        </cfloop>

    </table>

</cfoutput>

</cfsavecontent>

<cffile action="write" mode="777" output="#saveContent1#" file="D:Inetpub/myapps/report/myReport.xls">

This topic has been closed for replies.

3 replies

p_sim
Participating Frequently
July 12, 2013

Use Ben Nadel's POI as Adam suggested.

To answer your last question, you might want to experiment with Date/Time functions. Here: csv.append( '"' & query[cols] & '",' );, you dump the date value; and see why generates zeros.

Inspiring
April 17, 2013

What you are attempting worked quite well until Microsoft introduced Office 2007.  There are better options available now.  If you are on CF 9 or higher, there are ColdFusion tags and functions for this.  Adam gave you a link.  If you are on a lower version, download the apache poi and learn how to use it.  That's what we did when Office 2007 came out.  We were using CF 7 at the time.

newcfAuthor
Known Participant
April 17, 2013

i am using CF 7. i've try apache poi but never been sucess cause always got error for poi.classes is undefined even i have all files from lib.

if i want to export results into csv file, is there the way to do it?

thanks

Inspiring
April 17, 2013

Ben Nadel wrote a wrapper for POI which is fairly straight forward: http://www.bennadel.com/projects/poi-utility.htm. Maybe give that ago.

But if you want a CSV, why are you even trying to involve Excel or POI? Just create a CSV. There are even UDFs out there already to do so: http://www.cflib.org/index.cfm?event=page.udfbyid&udfid=1197

--

Adam

Inspiring
April 17, 2013

What version of ColdFusion are you using? You should be using spreadsheet operations to create Excel files.

All you are doing here is saving some HTML to file, and sving it with an XLS extension. That doesn't make it an XLS file, it just makes it incorrectly named.

--

Adam