In the past few days, many people have been asking about how to use CF to generate an EXCEL file. A demonstration program was written last night for your reference.
I suggest you try it out first. The program itself is not complicated. If you don't understand it, you 'd better send a question if you don't even read it...
All right, I will use my CCF Forum database for an experiment to open an EXCEL file online and generate an EXCEL file online. The file content is the name and number of all Forum sections of the CCF forum. By the way, I made a code to generate a WORD file and changed it. Let's take a closer look.
To open the OFFICE file online, you must have the OFFICE installed on your system. Remember this, or you will be prompted to download it.
<! --- Application. cfm File Code --->
<Cfapplication name = "CfToOffice"
Sessionmanagement = "Yes">
<CFSET request. DSN = "CCF"> <! --- Database DSN --->
<! ------ Index. cfm File Code ------->
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
<Cfquery name = "viewlist" datasource = "# request. dsn #">
Select boardID, boardNAME from board
Order by boardID
</Cfquery>
<Html>
<Head>
<Title> overview of CCForum columns </title>
</Head>
<Body>
<Center> <Table border = "1" align = "center">
<Tr> <td align = "center"> <B> NO. </B> </td> <td align = "center"> <B> name </B> </td> </tr>
<Cfoutput query = "viewlist">
<Tr> <td align = "center"> # boardID # </td> <td align = "left"> # boardname # </td> </tr>
</Cfoutput>
</Table>
<P>
<Center>
<A href = "web. cfm" target = "_ blank"> open in EXCEL on the WEB </a> <br>
<A href = "doc. cfm" target = "_ blank"> open in word on the WEB </a> <br>
<A href = "file. cfm" target = "_ blank"> Generate and download an EXCEL file </a> <br>
CopyRight (C) wait hunanet.com
</Center>
</Body>
</Html>
<! ----- This file is used to open the code online in the form of an EXCEL file ---->
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
<Cfsetting enablecfoutputonly = "Yes">
<Cfquery name = "getboard" datasource = "# request. dsn #">
Select boardID, boardNAME from board
Order by boardID
</Cfquery>
<Cfcontent type = "application/msexcel">
<Cfheader name = "Content-Disposition" value = "filename=board.xls">
<Cfset tabchar = chr (9)>
<Cfset newline = chr (13) & chr (10)>
<Cfloop query = "getboard">
<Cfoutput> # boardID # tabchar # boardname # newline # </cfoutput>
</Cfloop>
<! -------- Add an example of Generating WORD... note that only one of them is changed. --------->
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
<Cfsetting enablecfoutputonly = "Yes">
<Cfquery name = "getboard" datasource = "# request. dsn #">
Select boardID, boardNAME from board
Order by boardID
</Cfquery>
<Cfcontent type = "application/msword">
<Cfheader name = "Content-Disposition" value = "filename=board.doc">
<Cfset tabchar = chr (9)>
<Cfset newline = chr (13) & chr (10)>
<Cfloop query = "getboard">
<Cfoutput> # boardID # tabchar # boardname # newline # </cfoutput>
</Cfloop>
<! --- Everyone is concerned about it. Next we will make an important part ---->
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
<Cfsetting enablecfoutputonly = "Yes"> <! -- Delete blank space to improve performance -->
<Cfparam name = "filebody" default = "">
<Cfset tabchar = chr (9)> <! -- Set the TAB variable -->
<Cfset newline = chr (13) & chr (10)> <! -- Set the line feed variable -->
<Cfset filepath = expandpath ("file/")> <! -- Change relative path to absolute path -->
<Cfset writepath = "Your filepath={session.cfid=.xls"> <! -- Absolute path and file name -->
<! -- Query data -->
<Cfquery name = "getboard" datasource = "# request. dsn #">
Select boardID, boardNAME from board
Order by boardID
</Cfquery>
<! -- Set all information to a variable filebody -->
<Cfloop query = "getboard">
<Cfoutput>
<Cfset filebody = filebody & "# boardID # tabchar # boardname #" & newline>
</Cfoutput>
</Cfloop>
<! -- Write filebody to a file -->
<Cffile action = "WRITE" file = "# writepath #" output = "# filebody #" addnewline = "No">
<Cfoutput> the information you need has been generated into an EXCEL file,
File address: <br>
<A href = "file/#session.cfid#.xls"> http <cfif CGI. HTTPS neq 'off'> s </cfif>
: // # CGI. HTTP_HOST #/CF/Excel/file/#session.cfid#.xls </a>
</Cfoutput>
It is worth noting that I store dynamically generated XLS files in a folder named FILE. You must first create a FILE folder when running this code. Otherwise, an error may occur.