手机
当前位置:查字典教程网 >编程开发 >ASP教程 >ASP+模板生成Word、Excel、html的代码第1/2页
ASP+模板生成Word、Excel、html的代码第1/2页
摘要:大多数都是采用Excel.Application(http://www.blueidea.com/tech/program/2006/354...

大多数都是采用Excel.Application(http://www.blueidea.com/tech/program/2006/3547.asp)组件来生成

发现容易出错,而且对于大多数和我一样的菜鸟来说,比较麻烦,考虑到前些天用ASP+模板+adodb.stream生成静态页面的办法,经过多次尝试,终于掌握了一种用ASP+模板生成Excel和word的新的办法,先分享如下:

用模板生成Excel、Word最大优点:

Word、Excel文档样式易于控制和调整,以往用Excel.Application来生成Excel、Word,需要写很多代码来控制排版的样式,用模版几乎不受任何限制,只需要打开word或Excel,编辑文档,选择"文件->另存为web页",即可方便的做好模板,用office生成的模板要比直接在DW中做好模板更加符合office偏好,生成后文件样式可与原word、Excel格式99%一样,因此建议大家用office(office97~office2003)直接来生成模板框架。

主要的代码

function.asp

复制代码 代码如下:

<%

'欢迎与我交流和学习

'作者:幸福的子弹

'BLOG:http://mysheji.com/blog

'E-mail:zhaojiangang@gmail.com

'QQ:37294812

'-----------------------------------------------------------------------------

'开启容错机制

onerrorresumenext

'功能,检测服务器是否支持指定组件

Functionobject_install(strclassstring)

onerrorresumenext

object_install=false

dimxtestobj

setxtestobj=server.createobject(strclassstring)

if-2147221005<>Errthenobject_install=true

setxtestobj=nothing

endfunction

ifobject_install("Scripting.FileSystemobject")=falsethen

Response.Write"<divstyle='font-size:12px;color:#333;height:20px;line-height:20px;border:1pxsolid#DDCF8F;padding:6px;background:#FFFFED;font-family:verdana'>对不起,您的空间不支持FSO组件,请与管理员联系!</div>"

Response.End

endif

ifobject_install("adodb.stream")=falsethen

Response.Write"<divstyle='font-size:12px;color:#333;height:20px;line-height:20px;border:1pxsolid#DDCF8F;padding:6px;background:#FFFFED;font-family:verdana'>对不起,您的空间不支持adodb.stream功能,请与管理员联系!</div>"

Response.End

endif

'-----------------------------------------------------------------------------

'函数名称:ReadTextFile

'作用:利用AdoDb.Stream对象来读取文本文件

'参数:FileUrl文件相对路径,FileCharSet:文件编码

FunctionReadFromTextFile(FileUrl,FileCharSet)'函数

dimstr

setstm=server.CreateObject("adodb.stream")

stm.Type=2'指定或返回的数据类型,

stm.mode=3'指定打开模式,现在为可以读写模式,类似于word的只读或锁定功能

stm.charset=FileCharSet

stm.open

stm.loadfromfileserver.MapPath(FileUrl)

str=stm.readtext

ReadFromTextFile=str

EndFunction

'-----------------------------------------------------------------------------

'函数名称:WriteToTextFile

'作用:利用AdoDb.Stream对象来写入文本文件

subWriteToTextFile(FileUrl,Str,FileCharSet)'方法

setstm=server.CreateObject("adodb.stream")

stm.Type=2

stm.mode=3

stm.charset=FileCharSet

stm.open

stm.WriteTextstr

stm.SaveToFileserver.MapPath(FileUrl),2

stm.flush

Endsub

'-----------------------------------------------------------------------------

'功能:自动创建文件夹

'创建一级或多级目录,可以创建不存在的根目录

'参数:要创建的目录名称,可以是多级

'返回逻辑值,True成功,False失败

'创建目录的根目录从当前目录开始

FunctionCreateMultiFolder(ByValCFolder)

DimobjFSO,PhCreateFolder,CreateFolderArray,CreateFolder

Dimi,ii,CreateFolderSub,PhCreateFolderSub,BlInfo

BlInfo=False

CreateFolder=CFolder

OnErrorResumeNext

SetobjFSO=Server.CreateObject("Scripting.FileSystemObject")

IfErrThen

Err.Clear()

ExitFunction

EndIf

CreateFolder=Replace(CreateFolder,"","/")

IfLeft(CreateFolder,1)="/"Then

CreateFolder=Right(CreateFolder,Len(CreateFolder)-1)

EndIf

IfRight(CreateFolder,1)="/"Then

CreateFolder=Left(CreateFolder,Len(CreateFolder)-1)

EndIf

CreateFolderArray=Split(CreateFolder,"/")

Fori=0toUBound(CreateFolderArray)

CreateFolderSub=""

Forii=0toi

CreateFolderSub=CreateFolderSub&CreateFolderArray(ii)&"/"

Next

PhCreateFolderSub=Server.MapPath(CreateFolderSub)

IfNotobjFSO.FolderExists(PhCreateFolderSub)Then

objFSO.CreateFolder(PhCreateFolderSub)

EndIf

Next

IfErrThen

Err.Clear()

Else

BlInfo=True

EndIf

CreateMultiFolder=BlInfo

EndFunction

'点击下载提示

functiondownloadFile(strFile)

strFilename=server.MapPath(strFile)

Response.Buffer=True

Response.Clear

Sets=Server.CreateObject("ADODB.Stream")

s.Open

s.Type=1

onerrorresumenext

Setfso=Server.CreateObject("Scripting.FileSystemObject")

ifnotfso.FileExists(strFilename)then

Response.Write("<h1>Error:</h1>"&strFilename&"doesnotexist<p>")

Response.End

endif

Setf=fso.GetFile(strFilename)

intFilelength=f.size

s.LoadFromFile(strFilename)

iferrthen

Response.Write("<h1>Error:</h1>"&err.Description&"<p>")

Response.End

endif

Response.AddHeader"Content-Disposition","attachment;filename="&f.name

Response.AddHeader"Content-Length",intFilelength

Response.CharSet="UTF-8"

Response.ContentType="application/octet-stream"

Response.BinaryWrites.Read

Response.Flush

s.Close

Sets=Nothing

EndFunction

'-----------------------------------------------------------------------------

IfErrThen

err.Clear

Setconn=Nothing

Response.Write"<divstyle='font-size:12px;color:#333;height:20px;line-height:20px;border:1pxsolid#DDCF8F;padding:6px;background:#FFFFED;font-family:verdana'>网站异常出错,请与管理员联系,谢谢!</div>"

Response.End

EndIf

%>

生成Word文档:

复制代码 代码如下:

<%

'创建文件

dimtemplateName,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())

'其他内容......

'最终调用函数来生成文件

CallWriteToTextFile(filepath&filename,templateContent,fileCharset)

'最后关闭adodb.stream对象

stm.flush

stm.Close

setstm=nothing

downloadFile(filepath&filename)

%>

当前1/2页12下一页阅读全文

【ASP+模板生成Word、Excel、html的代码第1/2页】相关文章:

ASP实例教程:asp无限级显示分类代码

很有用的学习ASP常用到的代码第1/2页

ASP整理一些实用的速查代码

ASP输出生成Word 、Excel、Txt文件的方法

ASP读取IIS日志的小程序代码

使用模板实现ASP代码与页面分离

不用模板,只用ASP+FSO生成静态HTML页的一个方法

实例演练ASP+XML编程比较全的了第1/2页

超精华的asp代码大全第1/2页

asp编译成dll-图形化教程第1/2页

精品推荐
分类导航