LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

介绍一种用ASP+模板生成Word、Excel、静态页面一种简单、灵活多变的办法

admin
2010年8月3日 22:19 本文热度 4819
1# 发表于 2008-6-16 00:41 
由于工作的需要,我需要为客户做一个在线生成excel及word报表程序,参考了网上很多办法,大多数都是采用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)直接来生成模板框架

   演示:http://mysheji.com/aspcreate/
   主要的代码
function.asp
复制内容到剪贴板
代码:
<%
'欢迎与我交流和学习
'作者:幸福的子弹
'blog:http://mysheji.com/blog
'e-mail:zhaojiangang@gmail.com
'qq:37294812
'-----------------------------------------------------------------------------
'开启容错机制
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 "<div style='color:#333;height:20px;line-height:20px;border:1px solid #ddcf8f;padding:6px;background:#ffffed;font-family:verdana'>对不起,您的空间不支持fso组件,请与管理员联系!</div>"
    response.end
end if
if object_install("adodb.stream")=false then
    response.write "<div style='color:#333;height:20px;line-height:20px;border:1px solid #ddcf8f;padding:6px;background:#ffffed;font-family:verdana'>对不起,您的空间不支持adodb.stream功能,请与管理员联系!</div>"
    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("<h1>error:</h1>" & strfilename & " does not exist<p>")
         response.end
     end if
     set f = fso.getfile(strfilename)
     intfilelength = f.size
     s.loadfromfile(strfilename)
     if err then
         response.write("<h1>error: </h1>" & err.description & "<p>")
         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='color:#333;height:20px;line-height:20px;border:1px solid #ddcf8f;padding:6px;background:#ffffed;font-family:verdana'>网站异常出错,请与管理员联系,谢谢!</div>"
    response.end
end if
%>
[ 本帖最后由 hmilyheart 于 2008-6-16 09:49 编辑 ]
2# 发表于 2008-6-16 00:42 
生成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)
%>
程序源码:

[ 本帖最后由 hmilyheart 于 2008-6-16 00:45 编辑 ]

附件

aspcreate.rar (20.67 kb)

2008-6-16 00:45, 下载次数: 327

本帖最近评分记录
  • 帅青蛙 威望 +2 原创内容 2008-6-16 09:43
3# 发表于 2008-6-16 00:43 
生成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)
%>
4# 发表于 2008-6-16 00:43 
生成.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&"已经生成,<a href="""&filepath&filename&""" target=""_blank"">点击查看</a>")
%>

top

5# 发表于 2008-6-16 08:42 
我替我们程序员,收藏了。。。
来生还做兄弟

top

6# 发表于 2008-6-16 08:45 
谢谢楼上这些兄台,发现发错位置了, 版主有时间帮我转移到“后台数据库编程”板块~

top

7# 发表于 2008-6-16 09:00 
我替换我们单位的那些老头子收下了

top

8# 发表于 2008-6-16 09:26 
想法很好!我以前也是这样,不过不是用模板,直接把office文件另存为html,再改为asp,这样通过程序生成word。楼主的这种方式真的很好!!

top

9# 发表于 2008-6-16 09:27 
要是楼主开发的是.net 版就好了.,顶.
http://www.qlili.com 个人站帮点啊

top

10# 发表于 2008-6-16 09:38 
不错,正要找这方面的.收藏了.谢谢

top

11# 发表于 2008-6-16 09:54 
好不错的东西,我之前是 直接 在 asp里写...里面的样式都很乱...测试了这个,基本一样
www.idc0001.cn
找些人帮忙。。。

top

12# 发表于 2008-6-16 10:16 
引用:
原帖由 skybot 于 2008-6-16 09:27 发表
要是楼主开发的是.net 版就好了.,顶.
没想到,钻石级的会员,也会跟着瞎灌水...

说白了.生成的doc文件,其实还是一个html文件..

你把生成的doc的文件 用 记事本打开看看你就知道了...

将下面的代码保存为doc文件,你用word打开.看看和html页面的有多大区别.

 提示:您可以先修改部分代码再运行
总之.楼主所说的方法生成的并不是一个word文件,而是一个html文件,只不过扩展名是doc而已,html文件把扩展名改成doc,也可以用word打开..

而用word.application 控件生成的doc文件才是真正意义上的word文件...


生成成xsl文件也是如此.

[ 本帖最后由 faeng220 于 2008-6-16 10:22 编辑 ]

top

13# 发表于 2008-6-16 10:30 
呵呵,用楼上这种dw做的模板我也试过,发现生成的excel基本上改变了excel最初单元格的样式,除了表格地方地方之外,excel其他地方会是一片白,也就是覆盖了默认其他一个个单元格的样式,另外生成表格边框也很不美观。当然,我说的这种方法也是模板的一种灵活应用,只不过模板是office来制作。

用excel制作的模板生成excel文档再好不过了

 提示:您可以先修改部分代码再运行
[ 本帖最后由 hmilyheart 于 2008-6-16 10:34 编辑 ]

top

14# 发表于 2008-6-16 10:39 
自动化不够高,思路不错。

如果模板支持一些简单的asp语法实际中可以大力推广啦。
23555455(两年以上工作经验的web程序员生活q群)-博客

top

15# 发表于 2008-6-16 13:14 
  有没有生成xml(例如rss输出)的实例啊。
溜溜拍:66pai.net

top

16# 发表于 2008-6-16 14:01 
那你不如直接asp直接操作excel word方便些..
在用fso就可以

支持 原创的东西..

top

17# 发表于 2008-6-17 11:29 
收藏了,一直没有很好的办法进行到处exl 有时候还不兼容,只做了csv的文件格式!谢谢!

top

18# 发表于 2008-6-17 13:35 
真不错,感谢楼主

top

19# 发表于 2008-6-18 16:40 
思路不错。在一些不严格要求word或者excel格式的地方可以运用。

top

20# 发表于 2008-6-18 17:33 
思路挺不错的,收藏了!谢谢楼主!

top

21# 发表于 2008-6-18 17:58 
我感觉楼主的这个方法就是把cnbruce发的那个asp模版生成html的方法修改了下后缀而以...

就像12#所说...生成的并不是真正意义上的excel或word,只是格式差不多html而以

我测试过生成excel,通过下面的方法生成excel是空白的....

但是如果我把后缀.xls改成.csv就可以了,目前还没找到原因
复制内容到剪贴板
代码:
<%
on error resume next
dim db_path,conn,connstr
db_path = "data/data.mdb"
set conn= server.createobject("adodb.connection")
connstr = "provider=microsoft.jet.oledb.4.0;data source="&server.mappath(db_path)
    on error resume next
conn.open connstr
if err then
        err.clear
        set conn = nothing
        response.write "<script language=javascript> location.href='error.asp?msg=系统出错,请联系管理员!' </script>"
        response.end
    end if
set rs=server.createobject("adodb.recordset")
rs.open "select * from m_ss",conn,1,1
s=rs("s_main")
all=split(s,"|||")
for i=0 to 20
next
if not (rs.eof and rs.bof) then  
  dim ttxt,file,filepath,writefile
  ttxt="jb.csv"  '为要写入的文件取个文件名,后缀可以是txt,xls,这里我用csv,这种文件打开也是excel表
  set file = createobject("scripting.filesystemobject")
  application.lock
  '写入文件的存放路径,一定要开放这个路径下的读写权限
  filepath=server.mappath(ttxt)
  set writefile = file.createtextfile(filepath,true)
  '在表格中写入第一行,字段描述,这个根据你实际的数据表字段来写
  writefile.writeline "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,aa,ab,ac,ad,ae,af,ag,ah,ai,aj,ak"
  do while not rs.eof
  writefile.writeline rs("s_id")&","&rs("s_no")&","&rs("s_top")&","&rs("s_title")&","&rs("s_name")&","&rs("s_add")&","&rs("s_ajlx")&","&rs("s_fy1")&","&rs("s_fy2")&","&rs("s_fy3")&","&rs("s_fg1")&","&rs("s_fg2")&","&rs("s_fg3")&","&rs("s_yg")&","&rs("s_bg")&","&all(0)&","&all(1)&","&all(2)&","&all(3)&","&all(4)&","&all(5)&","&all(6)&","&all(7)&","&all(8)&","&all(9)&","&all(10)&","&all(11)&","&all(12)&","&all(13)&","&all(14)&","&all(15)&","&all(16)&","&all(17)&","&all(18)&","&all(19)&","&rs("s_ly")&","&rs("s_bz")&","&rs("s_sfz")&","&rs("s_time")   
  rs.movenext
  loop
  '以上三行作用是逐行将数据写入表中
  writefile.close
  application.unlock
  rs.close
  set rs=nothing
end if
rs.close
set rs=nothing
response.write "数据导出成功!"
response.end
%>

top

22# 发表于 2008-6-18 18:16 
不錯. .謝謝 樓主
<阿龍> donaldsu.cn

top

23# 发表于 2008-6-18 20:39 
谢谢大家的支持,其实我主要告诉大家的是借用模板来生成而非告诉大家用fso或adodb.stream可以生成excel(因为很多人已经知道fso或adodb.stream能生成excel或其他文本)
用fso或adodb.stream很久之前可以实现了,就想我刚开始做一样,也写了很多很多的代码,维护起来也不方便。
复制内容到剪贴板
代码:
<%
session.codepage=936                          '字符编码
response.charset="gb2312"
dim fso,excelcenter,filename,titlestyle
set fso=server.createobject("scripting.filesystemobject")
filename="carmod.xls"
titlestyle="style=""background:#0874ca;color:#fff;font-weight:bold;text-align:center"""
  strexcelfile=server.mappath(filename)
  if fso.fileexists(strexcelfile) then fso.deletefile strexcelfile
  set xslfile = fso.createtextfile(strexcelfile , true)
excelcenter="<table border=""1"" cellspacing=""1"" cellpadding=""1"" width=600>"&vblf&_
"<tr>"&vblf&_
"<td width=50 "&titlestyle&">id</td><td width=100 "&titlestyle&">车主姓名</td><td width=200 "&titlestyle&">车主电话</td><td width=150 "&titlestyle&">车牌号</td><td width=150 "&titlestyle&">车架号</td></tr>"&vblf&_
"<tr>"&vblf&_
"<td>121</td><td>幸福的子弹</td><td>13630026616</td><td>粤745125</td><td>jiadfsdfjsdfsd</td></tr>"&vblf&_
  "</table>"
  xslfile.writeline(excelcenter)
  xslfile.close
  set fso=nothing
  response.write("ok")
%>
以上生成的excel背景变成了全白色,只有表格地方才显示边框,不符合我们的要求。
另外一种方法,背景及空白单元格显示正常,但也是懒得去写代码
复制内容到剪贴板
代码:
……
excelcenter="id" & vbtab & "username" & vbtab &"password" & vbtab& "works" &vblf&_
            "12" & vbtab & "小王" & vbtab &"123456" & vbtab& "网页设计"
             ……
xslfile.writeline(excelcenter)
……
楼上这位和12#都说并非是真正意义上的excel,哈哈,我也不是很懂,希望有空能给我和大家解释一下“真正意义上的excel”是什么一种概念
在此也希望大家能找到其他更好的办法。

[ 本帖最后由 hmilyheart 于 2008-6-18 20:50 编辑 ]

top

24# 发表于 2009-11-25 13:35 
刚搜到,谢啦!

top

25# 发表于 2009-11-25 22:47 
楼主的思路很好,

fso生成excel,没有样式,楼主的思路赞!
加入标准化

top

26# 发表于 2010-6-22 12:37 
帖子很好,可是如果要用asp生成excel,这样替换出来的是文本,怎么样可以替换出来是数字。

top

27# 发表于 2010-6-23 13:42 
好厉害 谢谢楼楼
马上去试试效果
可以直接导出报表了~~

该文章在 2010/8/3 22:19:58 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2024 ClickSun All Rights Reserved