ASP+模板生成Word、Excel、html
当前位置:点晴教程→知识管理交流
→『 技术文档交流 』
[p]用模板生成excel、word最大优点: [br][br] word、excel文档样式易于控制和调整,以往用excel.application来生成excel、word,需要写很多代码来控制排版的样式,用模版几乎不受任何限制,只需要打开word或excel,编辑文档,选择"文件->另存为web页",即可方便的做好模板 ,用office生成的模板要比直接在dw中做好模板更加符合office偏好,生成后文件样式可与原word、excel格式99%一样,因此建议大家用office(office97~office2003)直接来生成模板框架。 [br][br]主要的代码 [br]function.asp[/p]
[p][/p]
<%[br]'欢迎与我交流和学习[br]'作者:幸福的子弹[br]'blog:http://mysheji.com/blog[br]'e-mail:zhaojiangang@gmail.com[br]'qq:37294812[br]'-----------------------------------------------------------------------------[br]'开启容错机制 [br]on error resume next [br]'功能,检测服务器是否支持指定组件[br]function object_install(strclassstring)[br]on error resume next[br]object_install=false[br]dim xtestobj[br]set xtestobj=server.createobject(strclassstring)[br]if -2147221005 <> err then object_install=true[br]set xtestobj=nothing[br]end function[br]if object_install("scripting.filesystemobject")=false then[br] response.write "
对不起,您的空间不支持fso组件,请与管理员联系! "[br] response.end[br]end if[br]if object_install("adodb.stream")=false then[br] response.write "对不起,您的空间不支持adodb.stream功能,请与管理员联系! "[br] response.end[br]end if[br]'-----------------------------------------------------------------------------[br]'函数名称:readtextfile[br]'作用:利用adodb.stream对象来读取文本文件[br]'参数:fileurl文件相对路径,filecharset:文件编码[br]function readfromtextfile (fileurl,filecharset)'函数[br] dim str[br] set stm=server.createobject("adodb.stream")[br] stm.type=2 '指定或返回的数据类型,[br] stm.mode=3 '指定打开模式,现在为可以读写模式,类似于word的只读或锁定功能[br] stm.charset=filecharset[br] stm.open[br] stm.loadfromfile server.mappath(fileurl)[br] str=stm.readtext[br] readfromtextfile=str[br]end function[br]'-----------------------------------------------------------------------------[br]'函数名称:writetotextfile[br]'作用:利用adodb.stream对象来写入文本文件[br]sub writetotextfile(fileurl,str,filecharset) '方法[br] set stm=server.createobject("adodb.stream")[br] stm.type=2 [br] stm.mode=3[br] stm.charset=filecharset[br] stm.open[br] stm.writetext str[br] stm.savetofile server.mappath(fileurl),2 [br] stm.flush[br]end sub[br]'-----------------------------------------------------------------------------[br]'功能:自动创建文件夹[br]'创建一级或多级目录,可以创建不存在的根目录[br]'参数:要创建的目录名称,可以是多级[br]'返回逻辑值,true成功,false失败[br]'创建目录的根目录从当前目录开始[br]function createmultifolder(byval cfolder)[br]dim objfso,phcreatefolder,createfolderarray,createfolder[br]dim i,ii,createfoldersub,phcreatefoldersub,blinfo[br]blinfo = false[br]createfolder = cfolder[br]on error resume next[br]set objfso = server.createobject("scripting.filesystemobject")[br]if err then[br]err.clear()[br]exit function[br]end if[br]createfolder = replace(createfolder,"","/")[br]if left(createfolder,1)="/" then[br]createfolder = right(createfolder,len(createfolder)-1)[br]end if[br]if right(createfolder,1)="/" then[br]createfolder = left(createfolder,len(createfolder)-1)[br]end if[br]createfolderarray = split(createfolder,"/")[br]for i = 0 to ubound(createfolderarray)[br]createfoldersub = ""[br]for ii = 0 to i[br]createfoldersub = createfoldersub & createfolderarray(ii) & "/"[br]next[br]phcreatefoldersub = server.mappath(createfoldersub)[br]if not objfso.folderexists(phcreatefoldersub) then[br]objfso.createfolder(phcreatefoldersub)[br]end if[br]next[br]if err then[br]err.clear()[br]else[br]blinfo = true[br]end if[br]createmultifolder = blinfo[br]end function[br]'点击下载提示[br]function downloadfile(strfile)[br] strfilename = server.mappath(strfile)[br] response.buffer = true[br] response.clear[br] set s = server.createobject("adodb.stream")[br] s.open[br] s.type = 1[br] on error resume next[br] set fso = server.createobject("scripting.filesystemobject")[br] if not fso.fileexists(strfilename) then[br] response.write("error:" & strfilename & " does not exist")[br] response.end[br] end if[br] set f = fso.getfile(strfilename)[br] intfilelength = f.size[br] s.loadfromfile(strfilename)[br] if err then[br] response.write(" error:" & err.description & "")[br] response.end[br] end if[br] response.addheader "content-disposition", "attachment; filename=" & f.name[br] response.addheader "content-length", intfilelength[br] response.charset = "utf-8"[br] response.contenttype = "application/octet-stream"[br] response.binarywrite s.read[br] response.flush[br] s.close[br] set s = nothing[br]end function[br]'-----------------------------------------------------------------------------[br]if err then[br] err.clear[br] set conn = nothing[br] response.write " 网站异常出错,请与管理员联系,谢谢! "[br] response.end[br]end if[br]%>
[p][br]生成word文档:[/p]
[p][/p]
[p]<%[br]'创建文件[br]dim templatename,templatechar,filepath,filename,filecharset,templatecontent[br] templatename="template/template_word.htm" '模板名字,支持带路径,如"/moban/moban1.htm"或"temp/moban1.htm"[br] templatechar="gb2312" '模板文本的编码[br] filepath="files/word/" '生成文件保存的路径,当前目录请留空,其他目录,路径必须以“/”结尾[br] filename="doc1.doc" '即将生成的文件名[br] createmultifolder(filepath) '这一句用来判断文件夹是否存在,没有则自动创建,支持n级目录[br] filecharset="gb2312" '打算生成的文本编码[br]'读取指定的模板内容[br]templatecontent=readfromtextfile(templatename,templatechar) [br]'以下就交给你来替换模板内容了[br]templatecontent=replace(templatecontent,"{$websitename}","蓝色理想")[br]templatecontent=replace(templatecontent,"{$username}","幸福的子弹")[br]templatecontent=replace(templatecontent,"{$now}",now())[br]'其他内容......[br]'最终调用函数来生成文件 [br]call writetotextfile(filepath&filename,templatecontent,filecharset) [br]'最后关闭adodb.stream对象[br]stm.flush[br]stm.close[br]set stm=nothing[br]downloadfile(filepath&filename)[br]%>[/p]
[p]生成excel文档:[/p]
<%[br]'创建文件[br]dim templatename,templatechar,filepath,filename,filecharset,templatecontent[br] templatename="template/template_excel.htm" '模板名字,支持带路径,如"/moban/moban1.htm"或"temp/moban1.htm"[br] templatechar="gb2312" '模板文本的编码[br] filepath="files/excel/" '生成文件保存的路径,当前目录请留空,其他目录,路径必须以“/”结尾[br] filename="book1.xls" '即将生成的文件名[br] createmultifolder(filepath) '这一句用来判断文件夹是否存在,没有则自动创建,支持n级目录[br] filecharset="gb2312" '打算生成的文本编码[br]'读取指定的模板内容[br]templatecontent=readfromtextfile(templatename,templatechar) [br]'以下就交给你来替换模板内容了[br]templatecontent=replace(templatecontent,"{$websitename}","蓝色理想")[br]templatecontent=replace(templatecontent,"{$username}","幸福的子弹")[br]templatecontent=replace(templatecontent,"{$now}",now())[br]'其他内容......[br]'最终调用函数来生成文件 [br]call writetotextfile(filepath&filename,templatecontent,filecharset) [br]'最后关闭adodb.stream对象[br]stm.flush[br]stm.close[br]set stm=nothing[br]downloadfile(filepath&filename)[br]%>
[p][br]生成.htm静态页面[/p]
<%[br]'创建文件[br]dim templatename,templatechar,filepath,filename,filecharset,templatecontent[br]templatename="template/template_html.htm" '模板名字,支持带路径,如"/moban/moban1.htm"或"temp/moban1.htm"[br]templatechar="gb2312" '模板文本的编码[br]filepath="files/html/" '生成文件保存的路径,当前目录请留空,其他目录,路径必须以“/”结尾[br]filename="untitled-1.htm" '即将生成的文件名[br]createmultifolder(filepath) '这一句用来判断文件夹是否存在,没有则自动创建,支持n级目录[br]filecharset="gb2312" '打算生成的文本编码[br]'读取指定的模板内容[br]templatecontent=readfromtextfile(templatename,templatechar) [br]'以下就交给你来替换模板内容了[br]templatecontent=replace(templatecontent,"{$websitename}","蓝色理想")[br]templatecontent=replace(templatecontent,"{$username}","幸福的子弹")[br]templatecontent=replace(templatecontent,"{$now}",now())[br]'其他内容......[br]'最终调用函数来生成文件 [br]call writetotextfile(filepath&filename,templatecontent,filecharset) [br]'最后关闭adodb.stream对象[br]stm.flush[br]stm.close[br]set stm=nothing[br]response.write("恭喜您,"&filename&"已经生成,点击查看")[br]%>
[p][/p]
[p][url=http://sosuo8.com/article/upimages3/aspcreate.rar]打包下载[/url][upload=rar]201073141210-2.rar[/upload][/p]
该文章在 2010/7/3 14:12:15 编辑过 |
关键字查询
相关文章
正在查询... |