介绍一种用ASP+模板生成Word、Excel、静态页面一种简单、灵活多变的办法
admin
2010年8月3日 22:19
本文热度 8102
hmilyheart [楼主]
幸福的子弹
银牌会员
认证
帖子
1479
体力
2057
威望
4
当前
广东 佛山
个人网站
发短消息
作品 15
加为好友
打分 2
专长 php,asp
1# 大 中 小 发表于 2008-6-16 00:41
由于工作的需要,我需要为客户做一个在线生成excel及word报表程序,参考了网上很多办法,大多数都是采用excel.application(
https://oa22.cn/bbs.asp?id=38095 )组件来生成,发现容易出错,而且对于大多数和我一样的菜鸟来说,比较麻烦,考虑到前些天用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 编辑 ]
佛山地产街
top
西部数码港台主机新上线即买即用 | 211工程高起专、高起本、专升本
hmilyheart [楼主]
幸福的子弹
银牌会员
认证
帖子
1479
体力
2057
威望
4
当前
广东 佛山
个人网站
发短消息
作品 15
加为好友
打分 2
专长 php,asp
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
佛山地产街
top
psd网页模板下载,高端实用 | 微软 visual studio 2010 专题
hmilyheart [楼主]
幸福的子弹
银牌会员
认证
帖子
1479
体力
2057
威望
4
当前
广东 佛山
个人网站
发短消息
作品 15
加为好友
打分 2
专长 php,asp
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) %>
佛山地产街
hmilyheart [楼主]
幸福的子弹
银牌会员
认证
帖子
1479
体力
2057
威望
4
当前
广东 佛山
个人网站
发短消息
作品 15
加为好友
打分 2
专长 php,asp
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>") %>
佛山地产街
lingling8
经典零零八
银牌会员
认证
帖子
1257
体力
1035
威望
1
当前
浙江 金华
离线
1 天
专长 网页设计,前端制作
5# 大 中 小 发表于 2008-6-16 08:42
来生还做兄弟
hmilyheart [楼主]
幸福的子弹
银牌会员
认证
帖子
1479
体力
2057
威望
4
当前
广东 佛山
个人网站
发短消息
作品 15
加为好友
打分 2
专长 php,asp
6# 大 中 小 发表于 2008-6-16 08:45
谢谢楼上这些兄台,发现发错位置了, 版主有时间帮我转移到“后台数据库编程”板块~
佛山地产街
livecoffee
冈萨蕾丝
初级会员
帖子
70
体力
142
威望
0
当前
湖北 宜昌
离线
156 天
专长 网页设计
7# 大 中 小 发表于 2008-6-16 09:00
ljlyy
中级会员
帖子
116
体力
285
威望
1
当前
广东 广州
离线
41 天
专长 js,asp
8# 大 中 小 发表于 2008-6-16 09:26
想法很好!我以前也是这样,不过不是用模板,直接把office文件另存为html,再改为asp,这样通过程序生成word。楼主的这种方式真的很好!!
博客
skybot
size
钻石会员
认证
帖子
3192
体力
7351
威望
7
当前
上海 闵行
离线
18 天
专长 js,c#,mssql
9# 大 中 小 发表于 2008-6-16 09:27
http://www.qlili.com 个人站帮点啊
togoog
天使共舞
银牌会员
帖子
664
体力
1361
威望
0
当前
北京 海淀区
专长 网页设计,asp,搜索
10# 大 中 小 发表于 2008-6-16 09:38
免费杀毒软件下载
dnaliang
金牌会员
帖子
1486
体力
3900
威望
0
当前
上海 闸北
离线
12 天
11# 大 中 小 发表于 2008-6-16 09:54
好不错的东西,我之前是 直接 在 asp里写...里面的样式都很乱...测试了这个,基本一样
www.idc0001.cn 找些人帮忙。。。
faeng220
小秦
版主
认证
帖子
2361
体力
4325
威望
6
当前
河南 平顶山
专长 前端制作,js,asp
12# 大 中 小 发表于 2008-6-16 10:16
引用:
原帖由 skybot 于 2008-6-16 09:27 发表 要是楼主开发的是.net 版就好了.,顶. 没想到,钻石级的会员,也会跟着瞎灌水...
说白了.生成的doc文件,其实还是一个html文件..
你把生成的doc的文件 用 记事本打开看看你就知道了...
将下面的代码保存为doc文件,你用word打开.看看和html页面的有多大区别.
<textarea style="font-family: 'courier new', courier, monospace" rows=12 cols=95 name=runcode0><!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>untitled document</title>
<style type="text/css" media="screen">
td{
width: 120px;
height: 40px;
}
.title {
text-align: center;
}
</style>
</head>
<body>
<table border="1">
<tr>
<td colspan="3" class="title">test</td>
</tr>
<tr>
<td align="center" bgcolor="#cccccc"> </td>
<td align="center"> </td>
<td align="center" bgcolor="#339966"> </td>
</tr>
<tr>
<td align="center"> </td>
<td align="center" bgcolor="#666666"> </td>
<td align="center"> </td>
</tr>
</table>
</body>
</html></textarea> 提示:您可以先修改部分代码再运行
总之.楼主所说的方法生成的并不是一个word文件,而是一个html文件,只不过扩展名是doc而已,html文件把扩展名改成doc,也可以用word打开..
而用word.application 控件生成的doc文件才是真正意义上的word文件...
生成成xsl文件也是如此.
[
本帖最后由 faeng220 于 2008-6-16 10:22 编辑 ]
偶系小秦!
hmilyheart [楼主]
幸福的子弹
银牌会员
认证
帖子
1479
体力
2057
威望
4
当前
广东 佛山
个人网站
发短消息
作品 15
加为好友
打分 2
专长 php,asp
13# 大 中 小 发表于 2008-6-16 10:30
呵呵,用楼上这种dw做的模板我也试过,发现生成的excel基本上改变了excel最初单元格的样式,除了表格地方地方之外,excel其他地方会是一片白,也就是覆盖了默认其他一个个单元格的样式,另外生成表格边框也很不美观。当然,我说的这种方法也是模板的一种灵活应用,只不过模板是office来制作。
用excel制作的模板生成excel文档再好不过了
<textarea style="font-family: 'courier new', courier, monospace" rows=12 cols=95 name=runcode1><html xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns="http://www.w3.org/tr/rec-html40">
<head>
<meta http-equiv=content-type content="text/html; charset=gb2312">
<meta name=progid content=excel.sheet>
<meta name=generator content="microsoft excel 9">
<link rel=file-list href="./template_excel.files/filelist.xml">
<link rel=edit-time-data href="./template_excel.files/editdata.mso">
<link rel=ole-object-data href="./template_excel.files/oledata.mso">
<!--[if gte mso 9]><xml>
<o:documentproperties>
<o:author>zjg</o:author>
<o:lastauthor>zjg</o:lastauthor>
<o:created>2008-06-15t14:27:20z</o:created>
<o:lastsaved>2008-06-15t15:58:53z</o:lastsaved>
<o:company>graceadv</o:company>
<o:version>9.2812</o:version>
</o:documentproperties>
<o:officedocumentsettings>
<o:downloadcomponents/>
<o:locationofcomponents href="file:///f:/软件/microsoft%2520office%25202000%2520简体中文专业/office2000/msowc.cab"/>
</o:officedocumentsettings>
</xml><![endif]-->
<style>
<!--table
{mso-displayed-decimal-separator:"\.";
mso-displayed-thousand-separator:"\,";}
@page
{margin:1.0in .75in 1.0in .75in;
mso-header-margin:.5in;
mso-footer-margin:.5in;}
tr
{mso-height-source:auto;
mso-ruby-visibility:none;}
col
{mso-width-source:auto;
mso-ruby-visibility:none;}
br
{mso-data-placement:same-cell;}
.style0
{mso-number-format:general;
text-align:general;
vertical-align:bottom;
white-space:nowrap;
mso-rotate:0;
mso-background-source:auto;
mso-pattern:auto;
color:windowtext;
font-size:12.0pt;
font-weight:400;
font-style:normal;
text-decoration:none;
font-family:宋体;
mso-generic-font-family:auto;
mso-font-charset:134;
border:none;
mso-protection:locked visible;
mso-style-name:常规;
mso-style-id:0;}
td
{mso-style-parent:style0;
padding-top:1px;
padding-right:1px;
padding-left:1px;
mso-ignore:padding;
color:windowtext;
font-size:12.0pt;
font-weight:400;
font-style:normal;
text-decoration:none;
font-family:宋体;
mso-generic-font-family:auto;
mso-font-charset:134;
mso-number-format:general;
text-align:general;
vertical-align:bottom;
border:none;
mso-background-source:auto;
mso-pattern:auto;
mso-protection:locked visible;
white-space:nowrap;
mso-rotate:0;}
.xl24
{mso-style-parent:style0;
color:white;
font-weight:700;
font-family:"times new roman", serif;
mso-font-charset:0;
text-align:center;
border:.5pt solid windowtext;
background:#3366ff;
mso-pattern:auto none;}
.xl25
{mso-style-parent:style0;
color:white;
font-weight:700;
text-align:center;
border-top:.5pt solid windowtext;
border-right:.5pt solid windowtext;
border-bottom:.5pt solid windowtext;
border-left:none;
background:#3366ff;
mso-pattern:auto none;}
.xl26
{mso-style-parent:style0;
font-family:"times new roman", serif;
mso-font-charset:0;
text-align:center;
border-top:none;
border-right:.5pt solid windowtext;
border-bottom:.5pt solid windowtext;
border-left:.5pt solid windowtext;}
.xl27
{mso-style-parent:style0;
font-family:"times new roman", serif;
mso-font-charset:0;
border-top:none;
border-right:.5pt solid windowtext;
border-bottom:.5pt solid windowtext;
border-left:none;}
.xl28
{mso-style-parent:style0;
font-family:"times new roman", serif;
mso-font-charset:0;
text-align:center;
border-top:none;
border-right:.5pt solid windowtext;
border-bottom:.5pt solid windowtext;
border-left:.5pt solid windowtext;
background:#ffffcc;
mso-pattern:auto none;}
.xl29
{mso-style-parent:style0;
font-family:"times new roman", serif;
mso-font-charset:0;
border-top:none;
border-right:.5pt solid windowtext;
border-bottom:.5pt solid windowtext;
border-left:none;
background:#ffffcc;
mso-pattern:auto none;}
.xl30
{mso-style-parent:style0;
font-family:"times new roman", serif;
mso-font-charset:0;
text-align:center;
border-top:none;
border-right:.5pt solid windowtext;
border-bottom:.5pt solid windowtext;
border-left:.5pt solid windowtext;
background:#ccffff;
mso-pattern:auto none;}
.xl31
{mso-style-parent:style0;
font-family:"times new roman", serif;
mso-font-charset:0;
border-top:none;
border-right:.5pt solid windowtext;
border-bottom:.5pt solid windowtext;
border-left:none;
background:#ccffff;
mso-pattern:auto none;}
ruby
{ruby-align:left;}
rt
{color:windowtext;
font-size:9.0pt;
font-weight:400;
font-style:normal;
text-decoration:none;
font-family:宋体;
mso-generic-font-family:auto;
mso-font-charset:134;
mso-char-type:none;
display:none;}
-->
</style>
<!--[if gte mso 9]><xml>
<x:excelworkbook>
<x:excelworksheets>
<x:excelworksheet>
<x:name>sheet1</x:name>
<x:worksheetoptions>
<x:defaultrowheight>285</x:defaultrowheight>
<x:selected/>
<x:panes>
<x:pane>
<x:number>3</x:number>
<x:activerow>4</x:activerow>
<x:activecol>1</x:activecol>
</x:pane>
</x:panes>
<x:protectcontents>false</x:protectcontents>
<x:protectobjects>false</x:protectobjects>
<x:protectscenarios>false</x:protectscenarios>
</x:worksheetoptions>
</x:excelworksheet>
<x:excelworksheet>
<x:name>sheet2</x:name>
<x:worksheetoptions>
<x:defaultrowheight>285</x:defaultrowheight>
<x:protectcontents>false</x:protectcontents>
<x:protectobjects>false</x:protectobjects>
<x:protectscenarios>false</x:protectscenarios>
</x:worksheetoptions>
</x:excelworksheet>
<x:excelworksheet>
<x:name>sheet3</x:name>
<x:worksheetoptions>
<x:defaultrowheight>285</x:defaultrowheight>
<x:protectcontents>false</x:protectcontents>
<x:protectobjects>false</x:protectobjects>
<x:protectscenarios>false</x:protectscenarios>
</x:worksheetoptions>
</x:excelworksheet>
</x:excelworksheets>
<x:windowheight>11145</x:windowheight>
<x:windowwidth>21435</x:windowwidth>
<x:windowtopx>0</x:windowtopx>
<x:windowtopy>90</x:windowtopy>
<x:protectstructure>false</x:protectstructure>
<x:protectwindows>false</x:protectwindows>
</x:excelworkbook>
</xml><![endif]-->
</head>
<body link=blue vlink=purple>
<table x:str border=0 cellpadding=0 cellspacing=0 width=709 style='border-collapse:
collapse;table-layout:fixed;width:533pt'>
<col width=72 style='width:54pt'>
<col width=144 style='mso-width-source:userset;mso-width-alt:4608;width:108pt'>
<col width=153 style='mso-width-source:userset;mso-width-alt:4896;width:115pt'>
<col width=186 style='mso-width-source:userset;mso-width-alt:5952;width:140pt'>
<col width=154 style='mso-width-source:userset;mso-width-alt:4928;width:116pt'>
<tr height=21 style='height:15.75pt'>
<td height=21 class=xl24 width=72 style='height:15.75pt;width:54pt'>id</td>
<td class=xl25 width=144 style='width:108pt'>网站名称</td>
<td class=xl25 width=153 style='width:115pt'>会员姓名</td>
<td class=xl25 width=186 style='width:140pt'>时间</td>
<td class=xl25 width=154 style='width:116pt'>最近回帖</td>
</tr>
<tr height=21 style='height:15.75pt'>
<td height=21 class=xl26 style='height:15.75pt'>1</td>
<td class=xl27>{$websitename}</td>
<td class=xl27>{$username}</td>
<td class=xl27>{$now}</td>
<td class=xl27>无</td>
</tr>
<tr height=21 style='height:15.75pt'>
<td height=21 class=xl28 style='height:15.75pt' x:num>2</td>
<td class=xl29>{$websitename}</td>
<td class=xl29>{$username}</td>
<td class=xl29>{$now}</td>
<td class=xl29>无</td>
</tr>
<tr height=21 style='height:15.75pt'>
<td height=21 class=xl26 style='height:15.75pt' x:num>3</td>
<td class=xl27>{$websitename}</td>
<td class=xl27>{$username}</td>
<td class=xl27>{$now}</td>
<td class=xl27>无</td>
</tr>
<tr height=21 style='height:15.75pt'>
<td height=21 class=xl30 style='height:15.75pt' x:num>4</td>
<td class=xl31>{$websitename}</td>
<td class=xl31>{$username}</td>
<td class=xl31>{$now}</td>
<td class=xl31>无</td>
</tr>
<tr height=21 style='height:15.75pt'>
<td height=21 class=xl26 style='height:15.75pt' x:num>5</td>
<td class=xl27>{$websitename}</td>
<td class=xl27>{$username}</td>
<td class=xl27>{$now}</td>
<td class=xl27>无</td>
</tr>
<![if supportmisalignedcolumns]>
<tr height=0 style='display:none'>
<td width=72 style='width:54pt'></td>
<td width=144 style='width:108pt'></td>
<td width=153 style='width:115pt'></td>
<td width=186 style='width:140pt'></td>
<td width=154 style='width:116pt'></td>
</tr>
<![endif]>
</table>
</body>
</html></textarea> 提示:您可以先修改部分代码再运行
[
本帖最后由 hmilyheart 于 2008-6-16 10:34 编辑 ]
佛山地产街
5do8
老农
荣誉管理团队
认证
帖子
3826
体力
5437
威望
105
当前
上海 浦东
个人网站
发短消息
作品 1
加为好友
打分 18
专长 php,asp,c#
14# 大 中 小 发表于 2008-6-16 10:39
自动化不够高,思路不错。 如果模板支持一些简单的asp语法实际中可以大力推广啦。
23555455(两年以上工作经验的web程序员生活q群)-博客
yafan99
初级会员
帖子
50
体力
101
威望
0
当前
江苏 南京
离线
325 天
专长 网页设计,前端制作,asp
15# 大 中 小 发表于 2008-6-16 13:14
溜溜拍:66pai.net
77576760
中级会员
帖子
1745
体力
432
威望
0
当前
广东 深圳
专长 前端制作,asp,mssql
16# 大 中 小 发表于 2008-6-16 14:01
那你不如直接asp直接操作excel word方便些.. 在用fso就可以 支持 原创的东西..
hnwind
新手上路
帖子
7
体力
7
威望
0
当前
河南 郑州
离线
747 天
17# 大 中 小 发表于 2008-6-17 11:29
收藏了,一直没有很好的办法进行到处exl 有时候还不兼容,只做了csv的文件格式!谢谢!
cyc308
高级会员
帖子
343
体力
692
威望
0
离线
517 天
18# 大 中 小 发表于 2008-6-17 13:35
cyc308
asp0000
中级会员
帖子
198
体力
433
威望
0
离线
77 天
专长 asp,.net,seo
19# 大 中 小 发表于 2008-6-18 16:40
思路不错。在一些不严格要求word或者excel格式的地方可以运用。
feng003
初级会员
帖子
27
体力
98
威望
0
离线
776 天
20# 大 中 小 发表于 2008-6-18 17:33
wtywin
子丑寅卯
钻石会员
帖子
5230
体力
5902
威望
18
当前
上海 浦东
专长 网页设计,前端制作,jsp
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 %>
淘宝袜品专营店!
ugxxx
高级会员
帖子
319
体力
679
威望
0
当前
广东 广州
专长 js,php,asp
22# 大 中 小 发表于 2008-6-18 18:16
<阿龍> donaldsu.cn
hmilyheart [楼主]
幸福的子弹
银牌会员
认证
帖子
1479
体力
2057
威望
4
当前
广东 佛山
个人网站
发短消息
作品 15
加为好友
打分 2
专长 php,asp
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 编辑 ]
佛山地产街
ww3312
新手上路
帖子
2
体力
6
威望
0
离线
251 天
24# 大 中 小 发表于 2009-11-25 13:35
gjr2
初级会员
帖子
53
体力
116
威望
0
离线
126 天
25# 大 中 小 发表于 2009-11-25 22:47
楼主的思路很好, fso生成excel,没有样式,楼主的思路赞!
加入标准化
monfs
新手上路
帖子
1
体力
2
威望
0
离线
35 天
26# 大 中 小 发表于 2010-6-22 12:37
帖子很好,可是如果要用asp生成excel,这样替换出来的是文本,怎么样可以替换出来是数字。
cream84
新手上路
认证
帖子
1
体力
7
威望
0
当前
上海 黄浦
27# 大 中 小 发表于 2010-6-23 13:42
好厉害 谢谢楼楼 马上去试试效果 可以直接导出报表了~~
该文章在 2026/3/18 10:07:32 编辑过