用户名.doc
提交用户名: 密码: 登录
注册 主页博客相册|个人档案 |好友
查看文章
ASP+模板生成Word、Excel、静态页面一种简单、灵活多变的办法 2010-06-22 12:56
Function.asp文件代码:
<%
'----------------------------------------------------------------------------- '开启容错机制
on error resume next
'功能,检测服务器是否支持指定组件
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 "
对不起,您的空间不支持FSO组件,请与
管理员联系!
"
Response.End
end if
if object_install("adodb.stream")=false then
Response.Write "
对不起,您的空间不支持adodb.stream功
能,请与管理员联系!
"
Response.End
end if
'----------------------------------------------------------------------------- '函数名称:ReadTextFile
'作用:利用AdoDb.Stream对象来读取文本文件
'参数:FileUrl文件相对路径,FileCharSet:文件编码
Function ReadFromTextFile (FileUrl,FileCharSet)'函数
dim str
set stm=server.CreateObject("adodb.stream") stm.Type=2 '指定或返回的数据类型,
stm.mode=3 '指定打开模式,现在为可以读写模式,类似于word的只读或锁定功能
stm.charset=FileCharSet
stm.open
stm.loadfromfile server.MapPath(FileUrl) str=stm.readtext
ReadFromTextFile=str
End Function
'-----------------------------------------------------------------------------
'函数名称:WriteToTextFile
'作用:利用AdoDb.Stream对象来写入文本文件 sub WriteToTextFile(FileUrl,Str,FileCharSet) '
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
'-----------------------------------------------------------------------------
'功能:自动创建文件夹
'创建一级或多级目录,可以创建不存在的根目录 '参数:要创建的目录名称,可以是多级 '返回逻辑值,True成功,False失败
'创建目录的根目录从当前目录开始
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
'点击下载提示
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("
Error:
" & strFilename & " does not exist
")
Response.End
end if
Set f = fso.GetFile(strFilename)
intFilelength = f.size
s.LoadFromFile(strFilename)
if err then
Response.Write("
Error:
" & err.Description & "
")
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 "
网站异常出错,请与管理员联系,谢谢~
"
Response.End
End If
%>
生成Word文档:
<%
'创建文件
dim templateName,templatechar,filepath,filename,fileCharset,templateContent templateName="template/template_word.htm" '模板名字,支持带路径,如"/moban/moban1.htm"或
"temp/moban1.htm"
templatechar="gb2312" '模板文本的编码
filepath="files/word/" '生成文件保存的路径,当前目录请留空,其他目录,路径必须以“/”结尾
filename="Doc1.doc" '即将生成的文件名 CreateMultiFolder(filepath) '这一句用来判断文件夹是否存在,没有则自动创建,支持n级目录
fileCharset="gb2312" '打算生成的文本编码 '读取指定的模板内容
templateContent=ReadFromTextFile(templateName,templatechar) '以下就交给你来替换模板内容了
templateContent=replace(templateContent,"{$websiteName}","蓝色理想") templateContent=replace(templateContent,"{$userName}","幸福的子弹") templateContent=replace(templateContent,"{$now}",Now()) '其他内容......
'最终调用函数来生成文件
Call WriteToTextFile(filepath&filename,templateContent,fileCharset) '最后关闭adodb.stream对象
stm.flush
stm.Close
set stm=nothing
downloadFile(filepath&filename)
%>
生成Excel文档:
<%
'创建文件
dim templateName,templatechar,filepath,filename,fileCharset,templateContent templateName="template/template_excel.htm" '模板名字,支持带路径,如"/moban/moban1.htm"或
"temp/moban1.htm"
templatechar="gb2312" '模板文本的编码
filepath="files/excel/" '生成文件保存的路径,当前目录请留空,其他目录,路径必须以“/”结尾
filename="Book1.xls" '即将生成的文件名 CreateMultiFolder(filepath) '这一句用来判断文件夹是否存在,没有则自动创建,支持n级目录
fileCharset="gb2312" '打算生成的文本编码 '读取指定的模板内容
templateContent=ReadFromTextFile(templateName,templatechar) '以下就交给你来替换模板内容了
templateContent=replace(templateContent,"{$websiteName}","蓝色理想") templateContent=replace(templateContent,"{$userName}","幸福的子弹") templateContent=replace(templateContent,"{$now}",Now())
'其他内容......
'最终调用函数来生成文件
Call WriteToTextFile(filepath&filename,templateContent,fileCharset) '最后关闭adodb.stream对象
stm.flush
stm.Close
set stm=nothing
downloadFile(filepath&filename)
%>
生成.htm静态页面
<%
'创建文件
dim templateName,templatechar,filepath,filename,fileCharset,templateContent templateName="template/template_html.htm" '模板名字,支持带路径,如"/moban/moban1.htm"或
"temp/moban1.htm"
templatechar="gb2312" '模板文本的编码
filepath="files/html/" '生成文件保存的路径,当前目录请留空,其他目录,路径必须以“/”结尾
filename="Untitled-1.htm" '即将生成的文件名 CreateMultiFolder(filepath) '这一句用来判断文件夹是否存在,没有则自动创建,支持n级目录
fileCharset="gb2312" '打算生成的文本编码 '读取指定的模板内容
templateContent=ReadFromTextFile(templateName,templatechar)
'以下就交给你来替换模板内容了
templateContent=replace(templateContent,"{$websiteName}","蓝色理想") templateContent=replace(templateContent,"{$userName}","幸福的子弹") templateContent=replace(templateContent,"{$now}",now()) '其他内容......
'最终调用函数来生成文件
Call WriteToTextFile(filepath&filename,templateContent,fileCharset) '最后关闭adodb.stream对象
stm.flush
stm.Close
set stm=nothing
response.Write("恭喜您,"&filename&"已经生成,
点击
查看")
%>