简体   繁体   中英

How can I download to Excel?

I'd like to go about offering a "download to excel" functionality for several different sets of data available on different parts of my coldfusion website. I'm using coldfusion and would love to use freely available custom tags / libraries that can help me accomplish this instead of coding it all myself from scratch. I've been pointed to cflib.org but am not sure how to get started. Anyone have any ideas, comments, or resources about downloading coldfusion website data to excel?

An approach that I have used in the past for an intranet with a load of tabular statistical data is to take advantage of the fact that Excel can read HTML.

On pages that I intend to provide Excel links for, I write out the core table as HTML to a file and include a link to that file in the rendered page. You can then serve this page with an .xls extension, use meta headers or even use an intermediary page with CFCONTENT in order to have your browser fire up Excel to display the content.

This approach can be extended for an arbitrary number of tables per page. You would have to write a tag to contain the table which will then take care of writing the file and supplying the download link for all HTML content contained within.

Sorry for the lack of example code. If my description of the process has left you scratching your head then I'll provide some examples for you.

You can also use an html table, as in:

<cfquery name="data" datasource="whatever">
...
</cfquery>

<cfheader name="Content-Disposition" value="inline; filename=fileName.xls">
<cfcontent type="application/x-msexcel" reset="true">

<table border="0" cellspacing="0" cellpadding="0">
    <thead>
        <tr>
            <td>Col1</td>
            <td>Col2</td>
        </tr>
    </thead>
    <tbody>
        <cfoutput query="data">
            <tr>
                <td>#data.Col1#</td>
                <td>#data.Col2#</td>
            </tr>
        </cfoutput>
    </tbody>
</table>

hth,

larry

Building on @Ciaran Archer's answer, I usually use CSV to accomplish this, which Excel will open up from the web if installed on a client's PC. Assuming the data you're working with is coming out of a database, use the QueryToCSV () from Ben Nadel's site as such:

<cfquery name="data" datasource="">
...
</cfquery>
<cfinclude template="fun_queryToCSV.cfm">
<cfheader name="Content-Disposition" value="filename=my-file-name.xls">
<cfcontent type="test/csv" reset="true">
<cfoutput>#querytoCSV(data)#</cfoutput>

The "reset" on cfcontent clears the response buffer so that the only thing inside of it is what comes after. Alternately, if you already have the data as file, cfcontent has a "file" attribute that will serve its contents up directly.

最后,我们使用了cflib.org中的Query2Excel函数,并对其进行了微调,以满足我们的特定需求。

对于Coldfusion 9+以及能够使用cfspreadsheet的人,您可以利用SpreadsheetReadBinary()使电子表格可以下载,而无需先将电子表格写入文件。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM