asp+ template generates word, Excel, HTML code 1th/2 Page _ Application Tips

Source: Internet
Author: User
Tags flush
Most of the Excel.Application (http://www.blueidea.com/tech/program/2006/3547.asp) components are used to generate
Find error prone, and for most of the rookie like me, more trouble, considering the previous days with asp+ template +adodb.stream generation static page method, after many attempts, finally mastered a asp+ template to generate Excel and Word, the new way to share the following:

Use templates to build Excel, Word's greatest advantages:

Word, Excel document style easy to control and adjust, used Excel.Application to build Excel, Word, need to write a lot of code to control the style of typesetting, with templates almost unlimited, just open Word or Excel, edit the document, select File-> Save as a Web page ", you can easily do the template, using Office-generated templates than directly in the DW to do the template more in line with Office preference, the generated file style can be the original word, Excel format 99%, It is therefore recommended that you use Office (office97~office2003) to generate the template framework directly.

The main code
Function.asp
Copy Code code as follows:

<%
' Welcome to communicate with me and learn
' Author: Happy Bullets
' Blog:http://mysheji.com/blog
' E-mail:zhaojiangang@gmail.com
' qq:37294812
'-----------------------------------------------------------------------------
' Open fault tolerance mechanism
On Error Resume Next
' function to detect whether the server supports specified components
Function Object_install (strclassstring)
On Error Resume Next
Object_install=false
Dim xtestobj
Set Xtestobj=server.createobject (strclassstring)
if-2147221005 <> ERR then object_install=true
Set xtestobj=nothing
End Function
If Object_install ("Scripting.FileSystemObject") =false Then
Response.Write "<div style= ' font-size:12px;color: #333; height:20px;line-height:20px;border:1px solid #DDCF8F; Padding:6px;background: #FFFFED Font-family:verdana ' > Sorry, your space does not support the FSO component, please contact the Administrator!</div> "
Response.End
End If
If Object_install ("ADODB.stream") =false Then
Response.Write "<div style= ' font-size:12px;color: #333; height:20px;line-height:20px;border:1px solid #DDCF8F; Padding:6px;background: #FFFFED Font-family:verdana ' > Sorry, your space does not support ADODB.stream function, please contact the Administrator!</div> "
Response.End
End If
'-----------------------------------------------------------------------------
' Function name: ReadTextFile
' Function: Use ADODB.stream object to read text files
' Parameter: FileURL file relative path, Filecharset: File encoding
function Readfromtextfile (fileurl,filecharset) ' Functions
Dim str
Set Stm=server. CreateObject ("ADODB.stream")
Stm. type=2 ' The data type specified or returned,
Stm.mode=3 ' specifies open mode, now read-write mode, similar to Word's read-only or lock function
Stm.charset=filecharset
Stm.open
Stm.loadfromfile server. MapPath (FILEURL)
Str=stm.readtext
Readfromtextfile=str
End Function
'-----------------------------------------------------------------------------
' Function name: writetotextfile
' Function: Write text file with ADODB.stream object
Sub Writetotextfile (Fileurl,str,filecharset) ' method
Set Stm=server. CreateObject ("ADODB.stream")
Stm. type=2
Stm.mode=3
Stm.charset=filecharset
Stm.open
Stm. WRITETEXT Str
Stm. SaveToFile server. MapPath (FILEURL), 2
Stm.flush
End Sub
'-----------------------------------------------------------------------------
' Function: Create folder automatically
' Create a level or multilevel directory to create a non-existent root directory
' Parameter: The name of the directory to create, which can be multilevel
' Return logical value, true success, False failed
' Create the root directory of the directory starting from the current directory
Function Createmultifolder (ByVal cfolder)
Dim Objfso,phcreatefolder,createfolderarray,createfolder
Dim I,ii,createfoldersub,phcreatefoldersub,blinfo
Blinfo = False
CreateFolder = Cfolder
On Error Resume Next
Set objFSO = Server.CreateObject ("Scripting.FileSystemObject")
If ERR Then
Err.Clear ()
Exit Function
End If
CreateFolder = Replace (CreateFolder, "", "/")
If Left (createfolder,1) = "/" Then
CreateFolder = Right (Createfolder,len (CreateFolder)-1)
End If
If Right (createfolder,1) = "/" Then
CreateFolder = Left (Createfolder,len (CreateFolder)-1)
End If
Createfolderarray = Split (CreateFolder, "/")
For i = 0 to UBound (createfolderarray)
Createfoldersub = ""
For II = 0 to I
Createfoldersub = createfoldersub & Createfolderarray (ii) & "/"
Next
Phcreatefoldersub = Server.MapPath (createfoldersub)
If not objfso.folderexists (phcreatefoldersub) Then
Objfso.createfolder (Phcreatefoldersub)
End If
Next
If ERR Then
Err.Clear ()
Else
Blinfo = True
End If
Createmultifolder = Blinfo
End Function
' Click the download prompt
function DownloadFile (strfile)
strFileName = Server. MapPath (strfile)
Response.Buffer = True
Response.Clear
Set s = Server.CreateObject ("ADODB.") Stream ")
S.open
S.type = 1
On Error Resume Next
Set FSO = Server.CreateObject ("Scripting.FileSystemObject")
If not FSO. FileExists (strFileName) Then
Response.Write ("Response.End
End If
Set f = fso. GetFile (strFileName)
Intfilelength = F.size
S.loadfromfile (strFileName)
If Err Then
Response.Write ("Response.End
End If
Response.AddHeader "Content-disposition", "attachment"; Filename= "& F.name
Response.AddHeader "Content-length", intfilelength
Response.Charset = "UTF-8"
Response.ContentType = "Application/octet-stream"
Response.BinaryWrite S.read
Response.Flush
S.close
Set s = Nothing
End Function
'-----------------------------------------------------------------------------
If ERR Then
Err. Clear
Set conn = Nothing
Response.Write "<div style= ' font-size:12px;color: #333; height:20px;line-height:20px;border:1px solid #DDCF8F; Padding:6px;background: #FFFFED font-family:verdana ' > website abnormal error, please contact the administrator, thank you! </div> "
Response.End
End If
%>

To build a Word document:
Copy Code code as follows:

<%
' Create a file
Dim templatename,templatechar,filepath,filename,filecharset,templatecontent
Templatename= "template/template_word.htm" template name, support with path, such as "/moban/moban1.htm" or "temp/moban1.htm"
templatechar= encoding of "gb2312" template text
Filepath= "files/word/" ' Generate file save path, current directory please leave blank, other directory, path must end with "/"
Filename= "Doc1.doc" ' the file name that will be generated
Createmultifolder (filepath) ' This sentence to determine whether the folder exists, not automatically created, support n-level directory
Filecharset= "gb2312" the text encoding to be generated
' Read the specified template content
Templatecontent=readfromtextfile (Templatename,templatechar)
' The following is for you to replace the template content
Templatecontent=replace (TemplateContent, "{$websiteName}", "Blue Ideal")
Templatecontent=replace (TemplateContent, "{$userName}", "Happy Bullets")
Templatecontent=replace (TemplateContent, "{$now}", now ())
' Other content ...
' Final call function to generate file
Call Writetotextfile (Filepath&filename,templatecontent,filecharset)
' Finally close the ADODB.stream object
Stm.flush
Stm. Close
Set stm=nothing
DownloadFile (Filepath&filename)
%>

Current 1/2 page 12 Next read the full text
Related Article

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.