Code to generate OFFICE files with ColdFusion _coldfusion

Source: Internet
Author: User
Tags chr dsn
These days in the classic, 5D met a lot of people in the question, how to use CF to generate Excel files, last night wrote a model program put up for everyone's reference.

I suggest that you understand first, the procedure itself is not complicated, if you do not understand the question again, it is best not to look at the question ...

OK, I'll use my CCF forum database to do experiments, to open Excel files online and generate Excel files online. The contents of the file is the name and number of all the CCF forum. I did it by the way. The code was changed in one place. Let's take a closer look.
Open Office files online must have Office installed on your system. Remember this, or you will be prompted to download.


<!---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>

<title>ccforum Column List </title>
<body>

<center><table border= "1" align= "Center" >
&LT;TR&GT;&LT;TD align= "center" ><b> number </b></td><td align= "center" ><b> name </b ></td></tr>
<cfoutput query= "Viewlist" >
&LT;TR&GT;&LT;TD align= "center" > #boardID #</td><td align= "left" > #boardname #</td></tr>
</cfoutput>
</table>
<p>
<center>
<a href= "web.cfm" target= "_blank" > Open with Excel on Web </a><br><br>
<a href= "doc.cfm" target= "_blank" > Open with Word on web </a><br><br>
<a href= "file.cfm" target= "_blank" > Generate Excel files and download </a><br><br><br>
CopyRight (C) Wait hunanet.com
</center>
</body>



<!-----This file is a code that is used to open online as 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 (&AMP;CHR) >

<cfloop query= "Getboard" >
<cfoutput> #boardID # #tabchar # #boardname # #newline #</cfoutput>
</cfloop>



<!--------Add a Build Word demo .... Note that only one place has been 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 (&AMP;CHR) >

<cfloop query= "Getboard" >
<cfoutput> #boardID # #tabchar # #boardname # #newline #</cfoutput>
</cfloop>



<!---We are concerned, we are going to do an important part---->

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
<cfsetting enablecfoutputonly= "Yes" ><!--remove whitespace and improve performance-->

<cfparam name= "Filebody" default= "" >
<cfset TABCHAR=CHR (9) ><!--set tab variable-->
<cfset NEWLINE=CHR (&AMP;CHR) ><!--set newline variable-->

<cfset Filepath=expandpath ("file/") ><!--Convert relative paths to absolute paths-->
<cfset writepath= "#filepath # #session. Cfid#.xls" ><!--absolute path plus filename-->

<!--query data-->
<cfquery name= "Getboard" datasource= "#request. dsn#" >
Select Boardid,boardname from board
ORDER BY Boardid
</cfquery>

<!--set all the information to a variable filebody-->
<cfloop query= "Getboard" >
<cfoutput>
<cfset Filebody=filebody & "#boardID # #tabchar # #boardname #" & newline>
</cfoutput>
</cfloop>

<!--write filebody to file-->
<cffile action= "WRITE" file= "#writepath #" output= "#filebody #" addnewline= "No" >


<cfoutput> The information you need has been generated in Excel format files,
File address is:<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>


Notably, I kept the dynamically generated XLS file in a folder called file. You must first create a file folder when you run this code .... Otherwise there will be an error.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.